Дерево Пифагора

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск

Описание

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

Иполнители:

Лапшин Андрей

Эссам Нганг Жоан Флориан Кенни

Группа 13632/1 Кафедра Теоретической механики

Файл:[[1]]

Особенности

В классическом дереве Пифагора угол равен 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 	  function draw_square(x, y, r,R){
 9 		ctx.fillStyle= 'rgb('+Math.floor(255-R)+', 165, 150)';
10 		ctx.fillRect(x,y,r,r);
11 	}
12 function Pyth_fract(a,n){
13 	   var n_max= input1.value;
14 	     var alpha= Math.PI*(input2.value)/180;
15 		if (n_max < 1) console.log("n must be >= 1")
16 		if (n > n_max) return;
17 	var b = Math.round( a*Math.cos(alpha) );
18 	var c = Math.round( a*Math.sin(alpha) );
19 	ctx.translate ( -Math.round((b/2+c/2)*Math.sin(alpha)),Math.round (-a/2 - (b/2+c/2)*Math.cos(alpha) ));
20 	ctx.rotate( -alpha);
21 	draw_square(-b/2, -b/2, b, c);
22 	  Pyth_fract(b,n+1 ) ;
23 	  ctx.rotate(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.translate ( +Math.round((c/2 + b/2)*Math.cos(alpha)), Math.round (-a/2 -(c/2+b/2)*Math.sin(alpha) ));
26 	  ctx.rotate(Math.PI/2-alpha) ; 
27     draw_square(-c/2, -c/2, c, c);
28      Pyth_fract(c, n+1 ) ;
29 	 ctx.rotate(-Math.PI/2+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 }
32 	function DrawP(){
33 		draw_square(w/2, h/2, 50, 50);
34 	  ctx.translate(w/2 + 25, h/2 +25);
35 		Pyth_fract(50, 1);
36 	}
37 	draw.onclick=function(){
38 	ctx.clearRect(0,0,w,h);
39 	DrawP()
40 	ctx.translate(-w/2 - 25, -h/2-25 );	
41 }
42 }