How can I check that a hexagon intersects a circle?

1.1k Views Asked by At

enter image description here

If I know the center positions and radius of a circle and hexagon, how can I determine whether are they touches or intersects each other?

Update: Also the orientation of the hexagon is know: enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

This is not easy because there are several configurations to consider. You could have an intersection along the line connecting the centers, but you can have intersections at other points as well. Think of shortening $D$ in the bottom figure, moving the hexagon nearer the circle. The corner below the line between the centers will hit the circle before the point on the line between the centers. You can't rely on the corners, either, because the hexagon could be much larger so the corner misses the circle but the edge hits.

My approach would be to find the side of the hexagon that crosses the line between the centers, then see if that segment intersects the circle. It is easier if we put the origin at the center of the hexagon with the $x$ axis running through a vertex. This corresponds to taking $\theta=0$. You can find the center of the circle in the new coordinate system first by subtracting the center of the hexagon to get the center as $(x_c',y_c')$, then rotating the system by $-\theta$ to get $x_c=x'_c \cos \theta+y'_c\sin \theta, y_c=-x_c \sin \theta+y'_c \cos \theta$

Now find the angle the center of the circle is at using $\arctan2(y_c,x_c)$. If it is in the range $0$ to $\frac \pi 3$ the side of interest will be the one that starts from the $x$ axis and goes up. Your diagram is like this. The equation of the line is $y=-\sqrt 3(x-R)$. The line segment extends from $x=R$ to $x=\frac R2$. Now you can check whether the two endpoints are inside the circle, plus find the perpendicular from the line segment to the center and find the intersection with the line the side is on. If that intersection is between the corners, you need to check that point, too.