Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

algorithm - determine if line segment is inside polygon

suppose we have convex polygon with vertices

(v0,v1,....vn)

my aim is to determine if for given point p(x,y) any line segment connecting this point and any vertices of polygon is inside polygon or even for given two point

p(x0,y0)  `p(x1,y1)`

line segment connecting these two point is inside polygon? i have searched many sites about this ,but i am still confused,generally i think we have to compare coordinates of vertices and by determing coordinates of which point is less or greater to another point's coordinates,we could determine location of any line segment,but i am not sure how correct is this,please help me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assume a point P and a convex polygon with n vertices V_1 to V_n (n > 2).

Sort the vertices of the polygon by their angle relative to a selected vertex, so that they are in clock-wise or counter-clockwise order. The edges of the polygon are then V_1 -> V_2, V_2 -> V_3, ..., V_(n-1) -> V_n, V_n -> V_1.

Now, for every edge, check the value of the cross product (V_(i+1) - V_i) x (P - V_i). Now P is inside the polygon iif all the values are >= 0 or all the values are <= 0.

There's a good tutorial on TopCoder for the more general problem where the polygon doesn't have to be convex. What they do is send a ray from the test point and check how many edges it intersects.

NOTE: The cross product used here is defined as (u1, u2) x (v1, v2) := u1*v2 - u2*v1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...