Check if coordinate in selected area

只愿长相守 提交于 2019-12-24 00:57:46

问题


I have 4 coordinates of area: x1,y1 ... etc. And have one more position x0,y0.

How to check if my coordinate in selected area?


回答1:


I will explain you how to check that (x0,y0) lies "below" the line through (x1,y1) and (x2,y2). Essentially, you want that the vector (x0-x1,y0-y1) points "to the right" of (x2-x1, y2-y1). This is equivalent to saying that the matrix

x0-x1      y0-y1

x2-x1      y2-y1

has a negative determinant. So your condition becomes

(x0-x1)(y2-y1) < (y0-y1)(x2-x1).

You get such a condition for any line bounding the area.




回答2:


Let

A = {x1, y1}
B = {x2, y2}
C = {x3, x3}
D = {x4, x4}

First, make sure that points form a polynomial and are not in straight line. This can be done by comparing the direction(AB) != direction(AC) != direction(AD) where AB, AC, AD are directional vectors.

To make sure that certain point P = {x0, y0} lies within the polygon ABCD, it is sufficient to check that sign(AC X AP) == sign(CD X CP) == sign(DB X DP) == sign(BA X BP).

AC: Directional vector A -> C
AP: Directional vector A -> P
.
. so on!
.
X: Cross product
sign: sign of cross product (+ or -)

It is only required to compare the sign of direction not the magnitude.



来源:https://stackoverflow.com/questions/14120015/check-if-coordinate-in-selected-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!