Is a point inside regular hexagon

前端 未结 8 1670
心在旅途
心在旅途 2020-12-31 08:59

I\'m looking for advice on the best way to proceed. I\'m trying to find whether a given point A:(a, b) is inside a regular hexagon, defined with center O:(x, y) and diameter

8条回答
  •  萌比男神i
    2020-12-31 09:32

    You can use the equations for each of the sides of the hexagon; with them you can find out if a given point is in the same half-plane as the center of the hexagon.

    For example, the top-right side has the equation:

    -sqrt(3)x - y + sqrt(3)/2 = 0
    

    You plug in this the coordinates of the point and then the coordinates of the center. If the results have the same sign, then the point is in the bottom-left half-plane (so it may be inside the hexagon).

    You then repeat by using the equations of the others sides.
    Note that this algorithm will work for any convex polygon.

提交回复
热议问题