Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created June 1, 2013 15:18
Show Gist options
  • Save kylemcdonald/5690723 to your computer and use it in GitHub Desktop.
Save kylemcdonald/5690723 to your computer and use it in GitHub Desktop.
Trying to determine which side of a contour a vertex lies on.
ofVec2f closestPointOnLine(const ofVec2f& p1, const ofVec2f& p2, const ofVec2f& p3) {
if(p1 == p2) {
return p1;
}
float u = (p3.x - p1.x) * (p2.x - p1.x);
u += (p3.y - p1.y) * (p2.y - p1.y);
float len = (p2 - p1).length();
u /= (len * len);
if(u > 1) {
u = 1;
} else if(u < 0) {
u = 0;
}
return p1.getInterpolated(p2, u);
}
bool isLeft(ofVec2f a, ofVec2f b, ofVec2f c){
return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) > 0;
}
bool sideTest(ofPolyline& polyline, ofVec2f position) {
ofVec2f a, b;
float bestDistance = 0;
for(int i = 0; i + 1 < polyline.size(); i++) {
ofVec2f v0 = polyline[i], v1 = polyline[i + 1];
ofVec2f closest = closestPointOnLine(v0, v1, position);
float distance = closest.distance(position);
if(i == 0 || distance < bestDistance) {
bestDistance = distance;
a = v0, b = v1;
}
}
if(bestDistance == 0) {
return true;
} else {
return isLeft(a, b, position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment