Skip to content

Instantly share code, notes, and snippets.

@behreajj
Last active November 18, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behreajj/1674e49ecea484340905483da73163ea to your computer and use it in GitHub Desktop.
Save behreajj/1674e49ecea484340905483da73163ea to your computer and use it in GitHub Desktop.
Rotation 06
PVector start = new PVector(),
end = new PVector(),
current0 = new PVector(),
current1 = new PVector();
float thetastart, thetaend, hw,
thetacurrent, magnitude, hh,
step, invwidth, prevdst, currdst;
void setup() {
size(384, 256);
background(0xffffffff);
hw = width * 0.5;
hh = height * 0.5;
invwidth = 1.0 / float(width);
magnitude = hh * 0.95;
initVecs();
}
void draw() {
// Manual control when right mouse is pressed.
step = mousePressed && mouseButton == RIGHT ?
constrain(mouseX * invwidth, 0.0, 1.0) :
abs(cos(frameCount * 0.01));
// Linear interpolation.
current0 = PVector.lerp(start, end, step);
// Angle interpolation.
thetacurrent = lerpangle(thetastart, thetaend, step);
PVector.fromAngle(thetacurrent, current1);
current1.mult(magnitude);
current1.add(hw, hh);
prevdst = currdst;
currdst = current0.dist(current1);
surface.setTitle(String.format("%.3f delta %.3f",
current0.dist(current1), currdst - prevdst));
// Translucent background.
noStroke();
fill(0x3fffffff);
rect(0, 0, width, height);
// Start and end points.
strokeWeight(12.0);
stroke(0xffff7f00);
point(start.x, start.y);
stroke(0xff007fff);
point(end.x, end.y);
// Traveling points.
strokeWeight(7.5);
stroke(0xff7f00ff);
point(current0.x, current0.y);
stroke(0xffff007f);
point(current1.x, current1.y);
}
void mousePressed() {
if (mouseButton == LEFT) {
initVecs();
}
}
void initVecs() {
PVector.random2D(start);
PVector.random2D(end);
thetastart = start.heading();
thetaend = end.heading();
// Scale and translate vectors.
start.mult(magnitude);
end.mult(magnitude);
start.add(hw, hh);
end.add(hw, hh);
// Flash construction lines.
strokeWeight(1.5);
stroke(0xff000000);
line(hw, hh, start.x, start.y);
line(hw, hh, end.x, end.y);
line(start.x, start.y, end.x, end.y);
noFill();
ellipse(hw, hh, magnitude * 2, magnitude * 2);
}
float lerpangle(float a, float b, float step) {
// Prefer shortest distance,
float delta = b - a;
if (delta == 0.0) {
return a;
} else if (delta < -PI) {
b += TWO_PI;
} else if (delta > PI) {
a += TWO_PI;
}
return (1.0 - step) * a + step * b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment