Дерево пифагора
Материал из Department of Theoretical and Applied Mechanics
Описание[править]
Фрактальное дерево Пифагора на языке программирования JavaScript
Исполнитель: Торопова Виктория
Группа 3630103/90001 Кафедра Теоретической механики
Визуализация[править]
Код программы[править]
Код программы на языке JavaScript:
1 function program_code() {
2
3 var ctx = canvas_example.getContext('2d');
4 var w = canvas_example.width;
5 var h = canvas_example.height;
6
7
8
9 function draw_Rect(b, c, r){ //градиент
10
11 var gradient = ctx.createLinearGradient(600, 600, 300, 0);
12
13 gradient.addColorStop(0.6, '#4E1609');
14 gradient.addColorStop(1, '#0A4500');
15
16 ctx.fillStyle = gradient;
17 ctx.fillRect(b,c,r,r);
18
19 ctx.strokeStyle = '#002800';
20 ctx.strokeRect(b,c,r,r);
21 }
22
23 function Recyrs(a,n){
24 var n_max= glub.value; //глубина
25 var angle= Math.PI*(alpha.value)/180; //угол
26
27 if (n > n_max) return;
28
29
30 //перемещение точки(СК)
31 ctx.translate ( -Math.round((Math.round( a*Math.cos(angle) )/2+Math.round( a*Math.sin(angle) )/2)*Math.sin(angle)),Math.round (-a/2 - (Math.round( a*Math.cos(angle) )/2+Math.round( a*Math.sin(angle) )/2)*Math.cos(angle) ));
32 ctx.rotate(-angle);
33 draw_Rect(Math.round( -a*Math.cos(angle) )/2, Math.round( -a*Math.cos(angle) )/2, Math.round( a*Math.cos(angle) ), Math.round( a*Math.cos(angle) ));
34
35 Recyrs(Math.round( a*Math.cos(angle) ),n+1 ) ;
36
37 ctx.rotate(angle);//возвращение на начальное положение
38 ctx.translate ( Math.round((Math.round( a*Math.cos(angle) )/2 + Math.round( a*Math.sin(angle) )/2)*Math.sin(angle)),-Math.round (-a/2 - (Math.round( a*Math.cos(angle) )/2+Math.round( a*Math.sin(angle) )/2)*Math.cos(angle) ));
39
40 ctx.translate ( Math.round((Math.round( a*Math.sin(angle) )/2 + Math.round( a*Math.cos(angle) )/2)*Math.cos(angle)), Math.round (-a/2 -(Math.round( a*Math.sin(angle) )/2+Math.round( a*Math.cos(angle) )/2)*Math.sin(angle) ));
41 ctx.rotate(Math.PI/2-angle) ; //поворот на противоположный угол
42 draw_Rect(-Math.round( a*Math.sin(angle) )/2, Math.round( -a*Math.sin(angle) )/2, Math.round( a*Math.sin(angle) ), Math.round( a*Math.sin(angle) ));
43
44 Recyrs(Math.round( a*Math.sin(angle) ), n+1 ) ;
45
46 ctx.rotate(-Math.PI/2+angle);//позвращение на начальное положение
47 ctx.translate ( -Math.round((Math.round( a*Math.sin(angle) )/2 + Math.round( a*Math.cos(angle) )/2)*Math.cos(angle)), -Math.round (-a/2 -(Math.round( a*Math.sin(angle) )/2+Math.round( a*Math.cos(angle) )/2)*Math.sin(angle) ));
48
49
50
51 }
52
53 function DrawTree(){
54 draw_Rect(w/2, h/2+100, 80, 80);
55 ctx.translate(w/2 +40, h/2 +140);
56 Recyrs(80, 1);
57
58 }
59
60 alpha.oninput=function(){
61 ctx.clearRect(0,0,w,h);
62 DrawTree()
63 ctx.translate(-w/2 - 40, -h/2-140 );
64
65 }
66 glub.oninput=function(){
67 ctx.clearRect(0,0,w,h);
68 DrawTree()
69 ctx.translate(-w/2 - 40, -h/2-140 );
70
71 }
72 }