Created
November 1, 2019 20:37
-
-
Save cansik/253797d779b00196d00c557c7554e0a8 to your computer and use it in GitHub Desktop.
Find curved position by 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
PVector start = new PVector(120, 300); | |
PVector end = new PVector(400, 230); | |
float curving = 100f; | |
int steps = 20; | |
void setup() { | |
size(500, 500, FX2D); | |
} | |
void draw() { | |
background(0); | |
noFill(); | |
// draw straight path | |
stroke(255, 0, 0); | |
line(start.x, start.y, end.x, end.y); | |
// draw | |
PVector lastStep = start; | |
for (int i = 1; i < steps; i++) { | |
PVector curvedPoint = getCurvedPoint(start, end, float(i) / steps, curving); | |
stroke(255, 0, 255); | |
line(lastStep.x, lastStep.y, curvedPoint.x, curvedPoint.y); | |
lastStep = curvedPoint; | |
} | |
} | |
PVector getCurvedPoint(PVector start, PVector end, float step, float curving) { | |
PVector linePos = PVector.lerp(start, end, step); | |
stroke(0, 255, 0); | |
ellipse(linePos.x, linePos.y, 20, 20); | |
float distancesOfPath = PVector.dist(start, end); | |
float distanceToLinePos = PVector.dist(start, linePos); | |
float curvePercentage = distanceToLinePos / (distancesOfPath * 0.5); | |
// switch in curve percentage | |
if (curvePercentage > 1.0) { | |
curvePercentage = 2.0 - curvePercentage; | |
} | |
text(nf(curvePercentage, 0, 2) + "%", linePos.x, linePos.y + 20); | |
float expCurvePercentage = easeInOutQuad(curvePercentage); | |
// calculate orthogonal vector | |
PVector rv = PVector.sub(linePos, end); | |
rv.rotate(radians(-90)); | |
rv.normalize(); | |
rv.mult(curving * expCurvePercentage); | |
PVector v = PVector.add(rv, linePos); | |
stroke(0, 255, 255); | |
line(linePos.x, linePos.y, v.x, v.y); | |
text(nf(expCurvePercentage, 0, 2) + "%", v.x, v.y + 10); | |
return v; | |
} | |
float easeInOutQuad(float t) { | |
return t < 0.5 ? 2*t*t : -1+(4-2*t)*t; | |
} | |
float easeInOutCubic(float t) { | |
return t < 0.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment