Is a point inside regular hexagon

前端 未结 8 1687
心在旅途
心在旅途 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条回答
  •  孤独总比滥情好
    2020-12-31 09:31

    If you reduce the problem down to checking {x = 0, y = 0, d = 1} in a single quadrant, you could make very simple.

    public boolean IsInsideHexagon(float x0, float y0, float d, float x, float y) {
        float dx = Math.abs(x - x0)/d;
        float dy = Math.abs(y - y0)/d;
        float a = 0.25 * Math.sqrt(3.0);
        return (dy <= a) && (a*dx + 0.25*dy <= 0.5*a);
    }
    
    • dy <= a checks that the point is below the horizontal edge.
    • a*dx + 0.25*dy <= 0.5*a checks that the point is to the left of the sloped right edge.

    For {x0 = 0, y0 = 0, d = 1}, the corner points would be (±0.25, ±0.43) and (±0.5, 0.0).

提交回复
热议问题