-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle3.pde
348 lines (276 loc) · 6.45 KB
/
circle3.pde
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
int slices = 8;
int screenheight = 500;
int screenwidth = 750;
PFont largeFont;
PFont smallFont;
float buttonarea = 100;
float buttony = screenheight - buttonarea/2;
int nButtons = 7;
Button[] button = new Button[nButtons];
float diam = 300; // diameter
float edgewidth = 10;
float rad = diam/2; // radius
float circlex = screenwidth/2;
float circley = (screenheight-buttonarea)/2;
int explosion = 0;
int xmotion = 0;
int xms = 30;
int ymotion = 0;
int yms = 20;
boolean go = false;
int step;
void setup() {
noLoop();
// size() with variables doesn't work in JS mode
// size(screenwidth, screenheight);
size(750, 500);
frameRate(20);
smooth();
largeFont = loadFont("UMingCN-48.vlw");
smallFont = loadFont("UMingCN-32.vlw");
textAlign(CENTER, CENTER);
rectMode(CENTER);
ellipseMode(CENTER);
strokeCap(SQUARE);
// Button 0: plus
button[0] = new Button("+");
button[0].x = 420;
button[0].width = 40;
button[0].height = 40;
button[0].incr = 1;
// Button 1: minus
button[1] = new Button("-");
button[1].x = 470;
button[1].width = 40;
button[1].height = 40;
button[1].incr = -1;
// Button 2: go/stop/back
button[2] = new GoButton();
button[2].x = 100;
button[2].width = 100;
// Buttons 3, 4, 5: set number of slices
button[3] = new CircleButton("8");
button[3].x = 550;
button[3].value = 8;
button[4] = new CircleButton("16");
button[4].x = 620;
button[4].value = 16;
button[5] = new CircleButton("50");
button[5].x = 690;
button[5].value = 50;
// Button 6: help
button[6] = new HelpButton();
button[6].x = 690;
button[6].y = 50;
drawSlices();
}
void drawBasics() {
background(#FFFFFF);
strokeWeight(1);
for (int i=0; i<nButtons; i++) {
button[i].paint();
}
sliceLabel();
}
void sliceLabel() {
float slice_label_x = 350;
strokeWeight(1);
fill(#FFFFFF);
rect(slice_label_x, buttony, 80, 50);
fill(#711B1B);
textFont(largeFont);
text(slices, slice_label_x, buttony);
redraw();
}
void resetVars() {
noLoop();
resetMatrix();
go = false;
step = 1;
explosion = 0;
xmotion = 0;
ymotion = 0;
}
void reset (int n) {
resetVars();
if (n>1)
slices = n;
drawSlices();
}
void drawSlices() {
drawBasics();
strokeWeight(1);
fill(#000000);
for (int i=0; i<slices; i++)
drawSlice(i);
}
void moveSlices() {
explosion = 0;
loop();
}
void drawSlice(int i) {
pushMatrix();
boolean isTop = i>=(float)slices/2;
float startangle = TWO_PI * (i+.5)/slices;
float finalangle = isTop ? PI*3/2 : PI/2;
float midangle = ((xms-xmotion)*startangle + xmotion*finalangle) / xms;
// explode (step 3)
translate(explosion*cos(midangle), explosion*sin(midangle));
// move and rotate (step 4)
if (isTop) {
translate(sin(PI/slices)*diam*xmotion/xms*(i+.25-3*slices/4.0), 0);
} else {
translate(sin(PI/slices)*diam*xmotion/xms*(slices/4.0-i-.25), 0);
}
// converge (step 5)
// move up/down by diam/4 times cosine factor to avoid overlap when angles are large
translate(0, diam/4 * cos(PI/slices) * ymotion/yms * (isTop ? 1 : -1));
float angle1 = midangle - PI/slices;
float angle2 = midangle + PI/slices;
fill(#FABA00);
stroke(#711B1B);
strokeWeight(edgewidth);
arc(circlex, circley, diam-edgewidth, diam-edgewidth, angle1, angle2);
stroke(#711B1B);
strokeWeight(2);
line(circlex, circley, circlex+rad*cos(angle1), circley+rad*sin(angle1));
line(circlex, circley, circlex+rad*cos(angle2), circley+rad*sin(angle2));
popMatrix();
}
void draw() {
if (step == 6) {
drawSlices();
noLoop();
}
if (step == 5) {
ymotion ++;
drawSlices();
if (ymotion >= yms) {
step = 6;
}
}
if (step == 4) {
if (explosion>0) // unexplode
explosion --;
xmotion ++;
drawSlices();
if (xmotion >= xms) {
step = 5;
}
}
if (step == 3) {
explosion ++;
drawSlices();
if (explosion >= 10) {
step = 4;
}
}
}
void mouseClicked() {
//System.out.println("Mouse clicked: " + mouseX + ", " + mouseY);
noLoop();
for (int i=0; i<nButtons; i++) {
if (button[i].checkclick())
return;
}
}
////////////////////////////////////////////////////////////
class Button {
float x;
float y = buttony;
float width = 50;
float height = 50;
String txt;
// set if incrementing
int incr = 0;
// set if value should be simply set
int value = 0;
Button (String txt) {
this.txt = txt;
}
void paint() {
fill(#711B1B);
rect(x, y, width, height);
fill(#FFFFFF);
textFont(largeFont);
text(txt, x, y, width, height);
}
boolean checkclick() {
if (abs(mouseX-x)<width/2 && abs(mouseY-y)<height/2) {
click();
return true;
}
return false;
}
void click() {
if (value != 0)
reset (value);
else
reset(slices+incr);
}
}
//////////////////////////////////////////////////////////////////
class CircleButton extends Button {
CircleButton(String txt) {
super(txt);
}
void paint() {
fill(#711B1B);
ellipse(x, y, width, height);
fill(#FFFFFF);
textFont(smallFont);
text(txt, x, y, width, height);
}
boolean checkclick() {
if (sqrt((mouseX-x)*(mouseX-x) + (mouseY-y)*(mouseY-y))<width/2) {
click();
return true;
}
return false;
}
}
//////////////////////////////////////////////////////////////////
class GoButton extends Button {
GoButton() {
super("");
}
void paint() {
fill(#711B1B);
rect(x, y, width, height);
fill(#FFFFFF);
textFont(largeFont);
text(go? (step==6 ? "Back" : "Stop") : "Go", x, y, width, height);
}
void click() {
if (!go) {
go = true;
step = 3;
moveSlices();
} else {
go = false;
reset(slices);
}
}
}
//////////////////////////////////////////////////////////////////
class HelpButton extends CircleButton {
HelpButton() {
super("?");
}
void click() {
step = 1;
resetVars();
drawBasics();
textFont(smallFont);
textAlign(LEFT);
text("You can measure the radius and circumference of a circle. " +
"You'll find the circumference is 2·π·the radius.\n" +
"What is the area of the circle?\n" +
"Answer this by cutting the circle up\n" +
"and rearranging it into something like a rectangle.\n" +
"What is the width and height of the rectangle?" ,
screenwidth/2, screenheight/2,
screenwidth-100, screenheight-buttonarea-100);
textAlign(CENTER, CENTER);
}
}