Check if a point is in a rotated rectangle (C#)

前端 未结 7 1518
囚心锁ツ
囚心锁ツ 2021-01-07 22:20

I have a program in C# (Windows Forms) which draws some rectangles on a picturebox. They can be drawn at an angle too (rotated).

I know each of the rectangles\' star

7条回答
  •  自闭症患者
    2021-01-07 22:38

    See the rectangle edges as a list of vectors linking a corner to the next, sorting corners clockwise. If the point is in the square, it must be to the right with respect to all of the edge vectors.

    This can be solved by vector products, but it boils down to the following:

    Iterate over rectangle corners:

    • the point to be checked is P=[px,py]
    • the current corner is C=[cx,cy] and the next corner is N=[nx,ny]
    • if px*ny+cx*py+nx*cy, the point is outside the square.

    this would actually work for every convex polygon.

提交回复
热议问题