Дерево Пифагора — различия между версиями

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск
Строка 57: Строка 57:
 
}
 
}
 
//Pyth_fract(100, 1, Math.PI*1/4);
 
//Pyth_fract(100, 1, Math.PI*1/4);
 +
}
  
}
+
</syntaxhighlight>
 +
 
 +
</div>

Версия 00:05, 26 мая 2019

Описание

Алгоритм построения дерева Пифагора с выбором углов и глубиной прорисовки. Файл:

Особенности

В классическом дереве Пифагора угол равен 45 градусам, также можно построить обобщённое дерево Пифагора при использовании других углов.

Визуализация

Код программы

Код программы на языке JavaScript:
 1 }
 2 window.addEventListener("load", program_code, false);
 3 function program_code() {
 4 	
 5 	var ctx = canvas_example.getContext('2d');
 6 	var w = canvas_example.width;
 7 	var h = canvas_example.height;
 8 	
 9 	//var n_max= input1.value;
10 	
11 //ctx.strokeRect(w/2, h/2, 100,100);
12 	  //ctx.translate(w/2 + 50, h/2 +50);
13 	  function draw_square(x, y, r,R){
14 		ctx.fillStyle= 'rgb('+Math.floor(255-R)+', 165, 150)';
15 		ctx.fillRect(x,y,r,r);
16 	}
17 function Pyth_fract(a,n){
18 	   var n_max= input1.value;
19 	     var alpha= Math.PI*(input2.value)/180;
20 		if (n_max < 1) console.log("n must be >= 1")
21 		if (n > n_max) return;
22 	var b = Math.round( a*Math.cos(alpha) );
23 	var c = Math.round( a*Math.sin(alpha) );
24 	ctx.translate ( -Math.round((b/2+c/2)*Math.sin(alpha)),Math.round (-a/2 - (b/2+c/2)*Math.cos(alpha) ));
25 	ctx.rotate( -alpha);
26 	draw_square(-b/2, -b/2, b, c);
27 	  Pyth_fract(b,n+1 ) ;
28 	  ctx.rotate(alpha);
29 	  ctx.translate ( Math.round((b/2 + c/2)*Math.sin(alpha)),-Math.round (-a/2 - (b/2+c/2)*Math.cos(alpha) ));
30 	  ctx.translate ( +Math.round((c/2 + b/2)*Math.cos(alpha)), Math.round (-a/2 -(c/2+b/2)*Math.sin(alpha) ));
31 	  ctx.rotate(Math.PI/2-alpha) ; 
32     draw_square(-c/2, -c/2, c, c);
33      Pyth_fract(c, n+1 ) ;
34 	 ctx.rotate(-Math.PI/2+alpha);
35 	 ctx.translate ( -Math.round((c/2 + b/2)*Math.cos(alpha)), -Math.round (-a/2 -(c/2+b/2)*Math.sin(alpha) ));
36 }
37 	function DrawP(){
38 		draw_square(w/2, h/2, 50, 50);
39 	  ctx.translate(w/2 + 25, h/2 +25);
40 		Pyth_fract(50, 1);
41 	}
42 	draw.onclick=function(){
43 	ctx.clearRect(0,0,w,h);
44 	DrawP()
45 	ctx.translate(-w/2 - 25, -h/2-25 );	
46 }
47 	//Pyth_fract(100, 1, Math.PI*1/4);
48 }