The line through (Xr, Yr)
with slope S
has equation D(X, Y):= (Y - Yr) - S (X - Xr) = 0
.
Just check that D(Xa, Ya)
and D(Xb, Yb)
have opposite signs.
题
Essentially I need to return a boolean if a line and a line segment intersect. For the line the information I have is the slope, xy coords for a random point, and xy for a y intercept. For the line segment I have the line segment and the two end point xy coordinates. Any ideas?
解决方案
The line through (Xr, Yr)
with slope S
has equation D(X, Y):= (Y - Yr) - S (X - Xr) = 0
.
Just check that D(Xa, Ya)
and D(Xb, Yb)
have opposite signs.
其他提示
Implementing this is not terribly difficult, it's the conceptual part that seems the most difficult. I might code something up for this for fun later but this should be enough to get you started. Also, note that this is might be a really bad way to solve it (time/space wise) but it will definitely work.
If you want to find a good solution, use convert the line to vectors and use the implementation in the answer in the link below all my text.
This should be easier for you because you already have two xy coords for the first line and two end point coordinates for the line segment.
Check this answer out for a really nice solution with some examples in the comments: https://stackoverflow.com/a/565282/2142219