forked from xcont/fractals
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdraw2points.js
More file actions
140 lines (122 loc) · 4.85 KB
/
Copy pathdraw2points.js
File metadata and controls
140 lines (122 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function recurs2(context, arr, position, n, x0, y0, x1, y1, line){
if (n==0){
if(line==null){
context.fillRect(x1,y1, 1,1);
}else{
context.moveTo(x0,y0);
context.lineTo(x1,y1);
}
return position;
}else{
if (!position[n]) position[n]=0;
var xx=Math.cos(arr[position[n]])*((x1-x0)*Math.cos(arr[position[n]])-(y1-y0)*Math.sin(arr[position[n]]))+x0;
var yy=Math.cos(arr[position[n]])*((x1-x0)*Math.sin(arr[position[n]])+(y1-y0)*Math.cos(arr[position[n]]))+y0;
position[n]++;
if (position[n]==arr.length) position[n]=0;
switch(drawmode){
case 1:
position=recurs(context, arr, position, n-1, x0, y0, xx, yy, line);
position=recurs(context, arr, position, n-1, x1, y1, xx, yy, line);
break;
case 2:
position=recurs(context, arr, position, n-1, xx, yy, x0, y0, line);
position=recurs(context, arr, position, n-1, xx, yy, x1, y1, line);
break;
case 3:
position=recurs(context, arr, position, n-1, xx, yy, x0, y0, line);
position=recurs(context, arr, position, n-1, x1, y1, xx, yy, line);
break;
default:
position=recurs(context, arr, position, n-1, x0, y0, xx, yy, line);
position=recurs(context, arr, position, n-1, xx, yy, x1, y1, line);
}
return position;
}
}
function recurs(context, arr, position, n, x0, y0, x1, y1, line) {
if (n == 0) {
if (line == null) {
context.fillRect(x1, y1, 1, 1);
} else {
context.moveTo(x0, y0);
context.lineTo(x1, y1);
}
return position;
} else {
if (!position[n]) position[n] = 0;
// Get angles for both points
const angle1 = arr[position[n]];
position[n] = (position[n] + 1) % arr.length; // Cycle index
const angle2 = arr[position[n]];
position[n] = (position[n] + 1) % arr.length;
// Compute first point (xx, yy)
const dx = x1 - x0;
const dy = y1 - y0;
const cos1 = Math.cos(angle1);
const xx = x0 + cos1 * (dx * Math.cos(angle1) - dy * Math.sin(angle1));
const yy = y0 + cos1 * (dx * Math.sin(angle1) + dy * Math.cos(angle1));
// Compute second point (xx2, yy2)
const cos2 = Math.cos(angle2);
const xx2 = x0 + cos2 * (dx * Math.cos(angle2) - dy * Math.sin(angle2));
const yy2 = y0 + cos2 * (dx * Math.sin(angle2) + dy * Math.cos(angle2));
/*
// Recursively process all three segments
position = recurs(context, arr, position, n-1, x0, y0, xx, yy, line);
position = recurs(context, arr, position, n-1, xx, yy, xx2, yy2, line);
position = recurs(context, arr, position, n-1, xx2, yy2, x1, y1, line);
*/
switch(drawmode) {
// MODE 0: Sequential triad (default)
case 0:
position = recurs(context, arr, position, n-1, x0,y0, xx,yy, line);
position = recurs(context, arr, position, n-1, xx,yy, xx2,yy2, line);
position = recurs(context, arr, position, n-1, xx2,yy2, x1,y1, line);
break;
// MODE 1:
case 1:
position = recurs(context, arr, position, n-1, x1,y1, xx2,yy2, line);
position = recurs(context, arr, position, n-1, xx2,yy2, xx,yy, line);
position = recurs(context, arr, position, n-1, xx,yy, x0,y0, line);
break;
// MODE 2:
case 2:
position = recurs(context, arr, position, n-1, x0,y0, xx2,yy2, line);
position = recurs(context, arr, position, n-1, xx2,yy2, xx,yy, line);
position = recurs(context, arr, position, n-1, xx,yy, x1,y1, line);
break;
// MODE 3:
case 3:
position = recurs(context, arr, position, n-1, x1,y1, xx,yy, line);
position = recurs(context, arr, position, n-1, xx,yy, xx2,yy2, line);
position = recurs(context, arr, position, n-1, xx2,yy2, x0,y0, line);
break;
// MODE 3:
case 4:
position = recurs(context, arr, position, n-1, xx,yy, x0,y0, line);
position = recurs(context, arr, position, n-1, x0,y0, x1,y1, line);
position = recurs(context, arr, position, n-1, x1,y1, xx2,yy2, line);
break;
// MODE 3:
case 5:
position = recurs(context, arr, position, n-1, xx2,yy2, x1,y1, line);
position = recurs(context, arr, position, n-1, x1,y1, x0,y0, line);
position = recurs(context, arr, position, n-1, x0,y0, xx,yy, line);
break;
// MODE 3:
case 6:
position = recurs(context, arr, position, n-1, x0,y0, xx,yy, line);
position = recurs(context, arr, position, n-1, x0,y0, xx2,yy2, line);
position = recurs(context, arr, position, n-1, x0,y0, x1,y1, line);
break;
}
return position;
}
}
function draw(context, arr, position, n, x0, y0, x1, y1, line){
var arr2=[]
for(var i=0; i<arr.length; i++){
arr2[i]=arr[i]*Math.PI/180;
}
recurs(context, arr2, position, n, x0, y0, x1, y1, line);
console.log(drawmode);
}