-
-
Save jkwok91/509f0b730eea00ce8ae9 to your computer and use it in GitHub Desktop.
dot moving on a line
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
/* | |
rotation with translation | |
http://mathgifs.blogspot.com/2013/10/rotations-through-translations.html | |
dang son i can't get enough of this blog | |
april 14-16, 2014 | |
*/ | |
DotOnLine[] dots; | |
float theta, rad; | |
boolean showLines; | |
void setup() { | |
size(300,300); | |
reset(); | |
} | |
void reset() { | |
showLines = false; | |
theta = 0; | |
rad = width/2; | |
PVector[] ls; | |
ls = setPts(48); | |
makeLines(ls); | |
} | |
PVector[] setPts(int n){ | |
PVector[] ls = new PVector[n]; | |
for (int i = 0; i < n; i++) { | |
ls[i] = new PVector(width/2+(rad*sin(i*TWO_PI/n)), height/2+(rad*cos(i*TWO_PI/n))); | |
} | |
return ls; | |
} | |
void makeLines(PVector[] pts) { | |
int len = pts.length; | |
dots = new DotOnLine[len]; | |
for (int i = 0; i < len; i++) { | |
DotOnLine l = new DotOnLine(pts[i], pts[(i+(int)(len/2)+1)%len], i*TWO_PI/len); | |
dots[i] = l; | |
} | |
} | |
void draw() { | |
background(0); | |
fill(255); | |
text("click to show lines",0,20); | |
for (DotOnLine d : dots) { | |
d.oscillate(); | |
} | |
theta = (theta+0.05)%TWO_PI; | |
} | |
void mousePressed() { | |
showLines = !showLines; | |
} | |
class DotOnLine { | |
PVector start, end; | |
float slope, ycoeff; | |
float x, y; | |
float offset; | |
DotOnLine(PVector p0, PVector p1, float o) { | |
start = p0; | |
end = p1; | |
float dx = end.x-start.x; | |
float dy = end.y-start.y; | |
slope = dy/dx; | |
ycoeff = start.y - slope*start.x; | |
offset = o; | |
} | |
void oscillate() { | |
x = sin(theta+offset); | |
x = map(x, -1, 1, start.x, end.x); | |
y = x*slope+ycoeff; | |
display(); | |
} | |
void display() { | |
if (showLines) { | |
stroke(color(255,0,0)); | |
line(start.x,start.y,end.x,end.y); | |
} | |
stroke(color(255, 255, 0)); | |
fill(color(255, 255, 0)); | |
ellipse(x, y, 5, 5); | |
} | |
} |
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
/* | |
one line | |
*/ | |
PVector start, end; | |
float dx, dy, slope, ycoeff; | |
float x, y, theta; | |
void setup() { | |
size(300, 300); | |
background(0); | |
reset(); | |
} | |
void reset() { | |
theta = 0; | |
start = new PVector(random(width/2), random(height)); | |
end = new PVector(random(width/2, width), random(height)); | |
dx = (end.x-start.x); | |
dy = (end.y-start.y); | |
slope = dy/dx; | |
ycoeff = start.y - slope*start.x; | |
x = start.x; | |
} | |
void mousePressed() { | |
reset(); | |
} | |
void draw() { | |
background(0); | |
x = sin(theta); | |
x = map(x, -1, 1, start.x, end.x); | |
y = x*slope+ycoeff; | |
stroke(color(255, 0, 0)); | |
line(start.x, start.y, end.x, end.y); | |
stroke(color(255, 255, 0)); | |
fill(color(255, 255, 0)); | |
ellipse(x, y, 5, 5); | |
theta = (theta+0.05)%TWO_PI; | |
} |
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
/* | |
triangle-circle | |
april 15-16, 2014 | |
*/ | |
DotOnLine[] dots; | |
float theta, rad; | |
boolean triangle; | |
void setup() { | |
size(300, 300); | |
background(0); | |
triangle = false; | |
reset(); | |
} | |
void reset() { | |
theta = 0; | |
rad = width/5; | |
PVector[] ls; | |
if (triangle) { | |
ls = makeEqTriangle(); | |
} | |
else { | |
ls = makeCircle(); | |
} | |
makeLines(ls); | |
} | |
void mousePressed() { | |
triangle = !triangle; | |
reset(); | |
} | |
PVector[] makeCircle() { | |
int sectors = 12; | |
PVector[] ls = new PVector[sectors]; | |
for (int i = 0; i < sectors; i++) { | |
ls[i] = new PVector(width/2+(rad*sin(i*TWO_PI/sectors)), height/2+(rad*cos(i*TWO_PI/sectors))); | |
} | |
return ls; | |
} | |
PVector[] makeEqTriangle() { | |
PVector[] ls = new PVector[3]; | |
ls[0] = new PVector(width/2, height/2-rad); | |
ls[1] = new PVector(width/2+rad*(sin(PI/3)), height/2+(rad/2)); | |
ls[2] = new PVector(width/2-rad*(sin(PI/3)), height/2+(rad/2)); | |
return ls; | |
} | |
void makeLines(PVector[] pts) { | |
int len = pts.length; | |
dots = new DotOnLine[len]; | |
for (int i = 0; i < len; i++) { | |
DotOnLine l = new DotOnLine(pts[i], pts[(i+(int)(len/2)+1)%len]); | |
dots[i] = l; | |
} | |
} | |
void draw() { | |
background(0); | |
fill(255); | |
text("click to switch", 0, 20); | |
for (DotOnLine d : dots) { | |
d.oscillate(); | |
} | |
theta = (theta+0.05)%TWO_PI; | |
} | |
class DotOnLine { | |
PVector start, end; | |
float slope, ycoeff; | |
float x, y; | |
DotOnLine(PVector p0, PVector p1) { | |
start = p0; | |
end = p1; | |
float dx = end.x-start.x; | |
float dy = end.y-start.y; | |
slope = dy/dx; | |
ycoeff = start.y - slope*start.x; | |
} | |
void oscillate() { | |
x = sin(theta); | |
x = map(x, -1, 1, start.x, end.x); | |
y = x*slope+ycoeff; | |
display(); | |
} | |
void display() { | |
stroke(color(255, 255, 0)); | |
fill(color(255, 255, 0)); | |
ellipse(x, y, 5, 5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment