-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathcircle_turtle_illusion.java
96 lines (84 loc) · 2.77 KB
/
circle_turtle_illusion.java
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
import processing.core.PApplet;
public class CircleDanceAnimation extends PApplet {
private int population;
private int resolution;
private int loops;
private int flip;
private int lines;
private float radius;
private Turtle[] turtles;
public static void main(String[] args) {
PApplet.main("CircleDanceAnimation");
}
public void settings() {
size(800, 600);
}
public void setup() {
population = args.length > 0 ? Integer.parseInt(args[0]) : 11;
resolution = args.length > 1 ? Integer.parseInt(args[1]) : 480;
loops = args.length > 2 ? Integer.parseInt(args[2]) : 1;
flip = args.length > 3 ? Integer.parseInt(args[3]) : 0;
lines = args.length > 4 ? Integer.parseInt(args[4]) : 0;
radius = 250;
surface.setTitle("Circle Dance Animation");
noLoop();
turtles = new Turtle[population];
for (int i = 0; i < population; i++) {
turtles[i] = new Turtle(i, population);
}
}
public void draw() {
background(0);
if (lines == 1) {
arrangeLines();
}
drawDancers();
loop();
}
private void arrangeLines() {
push();
stroke(255);
for (int n = 0; n < population; n++) {
float angle = n / (float) population * PI;
float x = width / 2 + cos(angle) * radius;
float y = height / 2 + sin(angle) * radius;
line(width / 2, height / 2, x, y);
}
pop();
}
private void drawDancers() {
for (int i = 0; i < population; i++) {
float phase = frameCount / (float) resolution * TWO_PI;
turtles[i].draw(phase, loops, flip, radius);
}
}
private class Turtle {
private float x;
private float y;
private float heading;
private float size;
private int color;
public Turtle(int index, int population) {
heading = index / (float) population * PI;
size = 2;
color = color(random(255 * 0.9f), 127 + random(128), random(255 * 0.7f));
}
public void draw(float phase, int loops, int flip, float radius) {
float individualPhase = (phase + heading * loops) % TWO_PI;
float distance = radius * sin(individualPhase);
x = width / 2 + cos(heading) * distance;
y = height / 2 + sin(heading) * distance;
push();
translate(x, y);
if (flip == 1) {
if (PI / 2 < individualPhase && individualPhase <= 3 * PI / 2) {
rotate(PI);
}
}
noStroke();
fill(color);
shape(TURTLE, 0, 0, size, size);
pop();
}
}
}