Created
April 9, 2021 18:45
-
-
Save JoshData/08cdd028f332c550fce2d3ba40d94396 to your computer and use it in GitHub Desktop.
is_point_on_line_segment in C++
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
// 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