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 of circumscribing circle.
It seems like overkill to use Ray-casting, or Winding-number to determine this, for such a simple case, and I'm currently looking at the option of finding the angle (from horizontal) of the line OA, and "normalising" (probably not the right word) it into one of the 6 equilateral triangles and seeing if this new point lies within this triangle.
I get the feeling I'm missing something simple, and there's an easy way (or if I'm really lucky, a Java API) to do this simply and efficiently.
Thanks for your help.
Edit: The hexagon is oriented such that one of the sides is flat with the horizontal.
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:
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.
Subtract the position of the center of the hexagon from your point P to get a vector V. Then, take the dot product of V with the following vectors, which correspond to the three pairs of opposing hexagon edges:
If any of the dot products are greater in magnitude than the distance from the center of the hexagon to one of its edges, then the point is not inside the hexagon.
For reference, the dot product of vectors [a,b] and [c,d] is a*c+b*d.
The angle "30" above is in degrees ;)
What you want is the code to find out whether a point is inside a convex polygon, an hexagon being a particular case of that.
Here's a good answer: https://stackoverflow.com/a/34689268/516188
I did modify that function for my use, I find my version clearer. It's typescript (you just squint and it's javascript):
This is what I have been using:
px
andpy
are the coordinates ofx
andy
projected onto a coordinate system where it is much easier to check the boundaries.Looks like you know general solution: "It seems like overkill to use...". So here is my idea:
Calculate distance from point to center and let's call it
l
.Then you can compare it to inradius (
r
) and circumradius (R
). ifl < r
then point is inside hexagon, ifl > R
then outside. Ifr < l < R
then you have to check against each side respectively, but sinceR - r
is very small (13% of length of side of hex) so probability that you will have to do complex calculations is tiny.Formulas can be found here: http://mathworld.wolfram.com/Hexagon.html
If you reduce the problem down to checking
{x = 0, y = 0, d = 1}
in a single quadrant, you could make very simple.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)
.