Skip to content

Instantly share code, notes, and snippets.

@JoshData
Created April 9, 2021 18:45
Show Gist options
  • Save JoshData/08cdd028f332c550fce2d3ba40d94396 to your computer and use it in GitHub Desktop.
Save JoshData/08cdd028f332c550fce2d3ba40d94396 to your computer and use it in GitHub Desktop.
is_point_on_line_segment in C++
// See if pt is between pt0 and pt1.
// via stackoverflow https://stackoverflow.com/a/328122
auto is_between = [](VertexType pt, VertexType p0, VertexType p1) {
auto z = (pt.y - p0.y) * (p1.x - p0.x)
- (pt.x - p0.x) * (p1.y - p0.y);
if (abs(z) > .0001) return false;
auto d = (pt.x - p0.x) * (p1.x - p0.x)
+ (pt.y - p0.y) * (p1.y - p0.y);
if (d < 0) return false;
auto n2 = (p1.x - p0.x) * (p1.x - p0.x)
+ (p1.y - p0.y) * (p1.y - p0.y);
if (d > n2) return false;
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment