Created
February 6, 2015 16:50
-
-
Save rogerallen/a0258038c7f05aac36f9 to your computer and use it in GitHub Desktop.
Spinning Dots Triangles Circle -- processing code that created https://www.youtube.com/watch?v=4LWCJvquuvo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int W=1280; | |
int H=720; | |
int N=16; | |
boolean SAVE_FRAMES=false; | |
float DT = 1.0/30.0; | |
float time; | |
int frame; | |
float[] dots_speed; | |
float[] dots_rad; | |
float dots_size; | |
PVector[] dots_loc; | |
PVector center; | |
void start() { | |
size(W, H); | |
frameRate(30); | |
smooth(4); | |
dots_speed = new float[N]; | |
dots_rad = new float[N]; | |
dots_loc = new PVector[N]; | |
for (int i = 0; i < N; i++) { | |
dots_speed[i] = 0.5 + i*0.1; | |
dots_rad[i] = 25 + i*25; | |
dots_loc[i] = new PVector(0, 0); | |
} | |
center = new PVector(W/2, H/2); | |
dots_size = 3.0; | |
time = 0; | |
} | |
void draw() { | |
background(0); | |
color(255); | |
strokeWeight(1.5); | |
updateDots(); | |
drawDots(); | |
drawLines(); | |
drawCircles(); | |
if (SAVE_FRAMES) { // && ((frame % 5) == 0)) { | |
saveFrame("a-####.png"); | |
} | |
time += DT; | |
frame += 1; | |
} | |
void updateDots() { | |
for (int i = 0; i < N; i++) { | |
dots_loc[i].x = center.x + dots_rad[i]*cos(time*dots_speed[i]); | |
dots_loc[i].y = center.y + dots_rad[i]*sin(time*dots_speed[i]); | |
} | |
} | |
void drawDots() { | |
fill(255); | |
for (int i = 0; i < N; i++) { | |
ellipse(dots_loc[i].x, dots_loc[i].y, 2*dots_size, 2*dots_size); | |
} | |
} | |
void drawLines() { | |
fill(0, 0); | |
stroke(255); | |
for (int i = 0; i < N-2; i++) { | |
beginShape(); | |
vertex(dots_loc[i].x, dots_loc[i].y); | |
vertex(dots_loc[i+1].x, dots_loc[i+1].y); | |
vertex(dots_loc[i+2].x, dots_loc[i+2].y); | |
endShape(CLOSE); | |
} | |
} | |
void drawCircles() { | |
fill(0, 0); | |
stroke(255, 255, 0); | |
for (int i = 0; i < N - 2; i++) { | |
PVector c = circleCenter(dots_loc[i+0], dots_loc[i+1], dots_loc[i+2]); | |
float r = PVector.dist(c, dots_loc[i+0]); | |
ellipse(c.x, c.y, 2*r, 2*r); | |
} | |
} | |
PVector circleCenter(PVector A, PVector B, PVector C) { | |
float yDelta_a = B.y - A.y; | |
float xDelta_a = B.x - A.x; | |
float yDelta_b = C.y - B.y; | |
float xDelta_b = C.x - B.x; | |
PVector center = new PVector(0, 0); | |
float aSlope = yDelta_a/xDelta_a; | |
float bSlope = yDelta_b/xDelta_b; | |
center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x) | |
- aSlope*(B.x+C.x) )/(2* (bSlope-aSlope) ); | |
center.y = -1*(center.x - (A.x+B.x)/2)/aSlope + (A.y+B.y)/2; | |
return center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment