Skip to content

Instantly share code, notes, and snippets.

@nervoussystem
Created July 12, 2010 23:56
Show Gist options
  • Save nervoussystem/473236 to your computer and use it in GitHub Desktop.
Save nervoussystem/473236 to your computer and use it in GitHub Desktop.
PVector[] offsetCurve(PVector[] crv, float dist) {
PVector[] offset = new PVector[crv.length];
for(int i=1;i<offset.length-1;++i) {
PVector currPt = crv[i];
PVector prevPt = crv[i-1];
PVector nextPt = crv[i+1];
PVector v1 = PVector.sub(prevPt,currPt);
PVector v2 = PVector.sub(nextPt,currPt);
v2.normalize();
v1.normalize();
float angle = PVector.angleBetween(v1,v2);
PVector bisector;
if(angle < .0001) {
bisector = new PVector(-v2.y,v2.x); //rotate 90 degrees.
bisector.mult(dist);
} else {
angle /= 2.0;
float sinA = sin(angle);
float cosA = cos(angle);
bisector = PVector.add(v1,v2);
bisector.normalize();
bisector.mult(-Math.signum(v1.x*v2.y-v2.x*v1.y));
bisector.mult(dist/sinA);
}
offset[i] = PVector.add(crv[i], bisector);
}
//do first and last
PVector dir = PVector.sub(crv[1],crv[0]);
dir.normalize();
dir.mult(dist);
dir.set(-dir.y,dir.x,0);
offset[0] = PVector.add(crv[0],dir);
dir = PVector.sub(crv[crv.length-1],crv[crv.length-2]);
dir.normalize();
dir.mult(dist);
dir.set(-dir.y,dir.x,0);
offset[crv.length-1] = PVector.add(crv[crv.length-1],dir);
return offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment