How to find whether a line intercepted in a polygon
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- d3.js moving average with previous and next data v
- How to get a fixed number of evenly spaced points
相关文章
- What are the problems associated to Best First Sea
- ceil conterpart for Math.floorDiv in Java?
- Coin change DP solution to keep track of coins
- why 48 bit seed in util Random class?
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- Need help generating discrete random numbers from
You can read a reasonable answer from this implementation found in some webpage
}
Reference. C++ Example: Geometry Concepts Line Intersection and its Applications, 2D drawing By lbackstrom from site ucancode
The question is a little bit ambiguous but let's try anyway:
Assume the points (x,y) on the line are defined by the equation Ax + By + C = 0. Then we can obviously determine if a point (x,y) is on the line by evaluating Ax + By + C. If the point is not on the line then the sign of Ax + By + C tells us on which side of the line the point is. Hence by inspecting the signs of the expression Ax + By + C for each vertex (x,y) of the polygon, we can determine if all points of the polygon are on the same side of line or not.
(A slightly different problem would be to determine if a polygon intersects a line segment.)
Depending on what exactly you want (I'll assume a line segment as I just wrote that code this week) you can get it in two parts:
first of all I'd suggest encoding lines as
because that form has no corner cases for lines like
X=5
,Y=4
orX=3*Y
.You need the points of the polygon on a coordinate graph and the slope and x and y intercept of the line to find that information. From there it's simple math.