How to resolve the algorithm Pythagoras tree step by step in the M2000 Interpreter programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pythagoras tree step by step in the M2000 Interpreter programming language

Table of Contents

Problem Statement

The Pythagoras tree is a fractal tree constructed from squares. It is named after Pythagoras because each triple of touching squares encloses a right triangle, in a configuration traditionally used to represent the Pythagorean theorem.

Construct a Pythagoras tree of order 7 using only vectors (no rotation or trigonometric functions).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pythagoras tree step by step in the M2000 Interpreter programming language

Source code in the m2000 programming language

MODULE Pythagoras_tree {
	CLS 5, 0  ' MAGENTA, NO SPLIT SCREEN
	PEN 14   ' YELLOW
	\\ code from zkl/Free Basic
	LET w = scale.x, h = w * 11 div 16
	LET w2 = w div 2, diff = w div 12
	LET TreeOrder = 6
	pythagoras_tree(w2 - diff, h -10, w2 + diff, h -10, 0)
	
	SUB pythagoras_tree(x1, y1, x2, y2, depth)
	 
	    IF depth > TreeOrder THEN EXIT SUB
	 
	    LOCAL dx = x2 - x1, dy = y1 - y2
	    LOCAL x3 = x2 - dy, y3 = y2 - dx
	    LOCAL x4 = x1 - dy, y4 = y1 - dx
	    LOCAL x5 = x4 + (dx - dy) / 2
	    LOCAL y5 = y4 - (dx + dy) / 2
	    MOVE x1, y1
	    DRAW TO x2, y2
	    DRAW TO x3, y3
	    DRAW TO x4, y4
	    DRAW TO x1, y1
	    pythagoras_tree(x4, y4, x5, y5, depth +1)
	    pythagoras_tree(x5, y5, x3, y3, depth +1)
	 
	END SUB
}
Pythagoras_tree

MODULE Pythagoras_Example{
	CLS 5, 0  ' MAGENTA, split line = 0  
	PEN 14  ' YELLOW
	\\ Linux smoothing not work (we can use the statement but without effect)
	IF ISWINE ELSE SMOOTH ON
	\\ PYTHAGORAS TREE
	\\ by definition all variables ar type of a double
	GLOBAL p=7, p4=PI/4, p2=PI/2, s2=SQRT(2)/2
	MODULE center_p (r, t){
		MODULE pythagoras_tree (r, dx, depth) {
			r2=r-p2
			DRAW ANGLE r, dx
			DRAW ANGLE r2, dx
			DRAW ANGLE r, -dx
			DRAW ANGLE r2, -dx
			IF depth>10 THEN EXIT
			s3=dx*s2
			depth++
			STEP ANGLE r+p4, s3*2
			CALL pythagoras_tree r-p4,  s3, depth
			STEP ANGLE r, -dx-s3
			STEP ANGLE r, s3
			STEP ANGLE r+p4, -s3
			CALL pythagoras_tree r+p4,  s3, depth
			STEP ANGLE r-p4, s3		
		}	
		MOVE SCALE.X/2, SCALE.Y/2	
		STEP ANGLE PI-p4+r, t*s2
		CALL pythagoras_tree r, t, 1
	}
	r=PI/3
	pixels=100
	center_p r, 100*TWIPSX
	center_p r+PI, 100*TWIPSX
	CopyImageToClipboard()
	
	Sub CopyImageToClipboard()
		LOCAL Scr$=""
		MOVE 0,0
		COPY SCALE.X, SCALE.Y TO Scr$
		CLIPBOARD Scr$
	END SUB
}
Pythagoras_Example

  

You may also check:How to resolve the algorithm Square but not cube step by step in the Ring programming language
You may also check:How to resolve the algorithm Subleq step by step in the Java programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Fortran programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Red programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the Delphi programming language