Is a point inside regular hexagon
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)
.
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.