-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch4.js
241 lines (206 loc) · 6.65 KB
/
sketch4.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Hex Snowflake
// Cinda Heeren
// 12/10/2020
let saveButton;
let timer = 0;
let f;
let g;
function setup() {
createCanvas(600, 600, WEBGL);
angleMode(DEGREES);
background(25, 25, 112); // blue
saveButton = createButton('save');
saveButton.mousePressed(saveSnowflake);
g = createGraphics(600,600);
f = new flake();
}
function saveSnowflake() {
save('snowflake.png');
}
function draw() {
clear();
background(25, 25, 112); // blue
noStroke();
f.display();
texture(g);
plane(600);
}
class flake {
constructor(){
this.translatex = 0.5;
this.translatey = 0.5;
this.rotspeed = 2*random() - 1;
this.growthrate = random();
this.scale = 10;
let pts = [];
for (let i = 0; i < 6; i++){
pts.push({x: this.scale*cos(60*i), y: this.scale*sin(60*i)});
}
let h = new sixgon(pts,false,0);
this.hexes = this.makeFlake(h,10);
}
display(){
g.translate(600 * this.translatex, 600 * this.translatey);
//rotate(timer*this.rotspeed);
for (let i = 0; i < this.hexes.length; i++) {
for (let j = 0; j < 6; j++){
this.hexes[i].display();
rotate(60);
}
}
}
buildReflection(s, p){
let retHexList = [];
for (let i = 0; i < s.length; i++){
let verts = s[i].vertices;
let h = [];
for (let j = 0; j < 6; j++){
let a = -(p[2].x - p[1].x)/(p[2].y - p[1].y);
let c = -a * p[0].x + p[0].y;
let d = (verts[j].x + (verts[j].y - c)*a)/(1 + a*a);
let newX = 2*d - verts[j].x;
let newY = 2*d*a - verts[j].y + 2*c;
h.push({x:newX,y:newY});
}
let newHex = new sixgon(h, s[i].plate,s[i].level);
newHex.setColor(s[i].col);
retHexList.push(newHex);
}
return retHexList;
}
makeStick(v, ext, w, d){
// constants we will need:
let sidelength = dist(v[0].x, v[0].y, v[1].x, v[1].y);
let r = w/sidelength;
let xa = (v[1].x + v[2].x)/2;
let ya = (v[1].y + v[2].y)/2;
let D = ext + sidelength/2;
// make vertex 0:
let x0 = 2 * (D * v[0].x - ext * xa)/sidelength;
let y0 = 2 * (D * v[0].y - ext * ya)/sidelength;
// make vertex 1:
let x1 = ((2*D - w)*v[0].x - 2*ext*xa + w*v[1].x)/sidelength;
let y1 = ((2*D - w)*v[0].y - 2*ext*ya + w*v[1].y)/sidelength;
// make vertex 2:
let x2 = (1-r) * v[0].x + r * v[1].x;
let y2 = (1-r) * v[0].y + r * v[1].y;
// make vertex 3:
let x3 = r * (v[1].x + v[2].x) + (1 - (2*r)) * v[0].x;
let y3 = r * (v[1].y + v[2].y) + (1 - (2*r)) * v[0].y;
//returnVal.addVertex(x3,y3);
// make vertex 4:
let x4 = (1-r) * v[0].x + r * v[2].x;
let y4 = (1-r) * v[0].y + r * v[2].y;
// make vertex 5:
let x5 = ((2*D - w)*v[0].x - 2*ext*xa + w*v[2].x)/sidelength;
let y5 = ((2*D - w)*v[0].y - 2*ext*ya + w*v[2].y)/sidelength;
let h = []
h.push({x:x4,y:y4});
h.push({x:x5,y:y5});
h.push({x:x0,y:y0});
h.push({x:x1,y:y1});
h.push({x:x2,y:y2});
h.push({x:x3,y:y3});
return new sixgon(h,false,10-d);
}
makePlate(v, ext, w, d){
let sidelength = dist(v[0].x, v[0].y, v[1].x, v[1].y);
let r = w/sidelength;
let xa = (v[1].x + v[2].x)/2;
let ya = (v[1].y + v[2].y)/2;
let D = ext + sidelength/2;
// make vertex 0:
let x0 = 2 * (D * v[0].x - ext * xa)/sidelength;
let y0 = 2 * (D * v[0].y - ext * ya)/sidelength;
// make vertex 1:
let xtemp = (1-r) * v[0].x + r * v[1].x;
let ytemp = (1-r) * v[0].y + r * v[1].y;
let x1 = x0 - v[0].x + xtemp;
let y1 = y0 - v[0].y + ytemp;
// make vertex 3:
let x3 = 2*w*v[0].x/ext - (2*w-ext)*x0/ext;
let y3 = 2*w*v[0].y/ext - (2*w-ext)*y0/ext;
// make vertex 5:
xtemp = (1-r) * v[0].x + r * v[2].x;
ytemp = (1-r) * v[0].y + r * v[2].y;
let x5 = x0 - v[0].x + xtemp;
let y5 = y0 - v[0].y + ytemp;
// make vertex 2:
let x2 = 2*x1 - 2*x0+ x5;
let y2 = 2*y1 - 2*y0+ y5;
// make vertex 4:
let x4 = 2*x5 - 2*x0 + x1;
let y4 = 2*y5 - 2*y0 + y1;
let h = []
h.push({x:x4,y:y4});
h.push({x:x5,y:y5});
h.push({x:x0,y:y0});
h.push({x:x1,y:y1});
h.push({x:x2,y:y2});
h.push({x:x3,y:y3});
return new sixgon(h,true,10-d);
}
makeHex(p, d){
let sidelength = dist(p[0].x, p[0].y, p[1].x, p[1].y);
let w;
let extent = (sidelength/2) + (3*random()*sidelength/2);
if (sidelength > 2*this.scale) {
w = 2*random() * sidelength/3;
}
else {
w = 3*random()*sidelength/2;
}
if (w > sidelength) {
return this.makePlate(p,extent,w,d);
}
else {
return this.makeStick(p, extent, w, d);
}
}
makeFlake(curr, depth){ // curr is a hexagon
let retHexes = [];
retHexes.push(curr);
if (depth > 1){
let verts = curr.vertices;
let pilot1 = [];
pilot1.push(verts[2]);
pilot1.push(verts[3]);
pilot1.push(verts[1]);
let newFlake = this.makeHex(pilot1,depth-1);
retHexes = retHexes.concat(this.makeFlake(newFlake,depth-1));
if (curr.plate) {
let pilot2 = [];
pilot2.push(verts[1]);
pilot2.push(verts[2]);
pilot2.push(verts[0]);
let newBranchSeed = this.makeHex(pilot2, depth-1);
let newBranch = this.makeFlake(newBranchSeed, depth-1);
retHexes = retHexes.concat(newBranch);
let reflectBranch = this.buildReflection(newBranch,pilot1);
retHexes = retHexes.concat(reflectBranch);
}
}
//retHexes.push(curr);
return retHexes;
}
}
class sixgon {
constructor(verts, plate, level) {
this.col = color(255,255, 255, random(127));
this.vertices = verts;
this.plate = plate;
this.level = level;
}
setColor(c){
this.col = c;
}
display(){
g.noStroke();
g.fill(this.col);
g.beginShape();
for (let i = 0; i < this.vertices.length; i++) {
g.vertex(this.vertices[i].x, this.vertices[i].y);
}
g.endShape(CLOSE);
}
}