2

Given a circle with center (Xc,Yc) and radius r as well as a polygon with vertices inside of an array such that vertices[] = { (Xv1, Yv1) , ... , (Xvn, Yvn) } where n is the number of vertices.

I want to be able to figure out if the circle is inside of the polygon. I am assuming (and it is safe to assume) that there are no holes in the polygon.

The only polygons I am checking will be triangles and pentagons.

What I have done so far is calculate if the center of the circle is inside of the polygon. This function is called isInside().

How can I check if the circle is completely inside of the polygons I am checking? Touching is ok.

More specifically, I am having trouble with the math for the relationship of the circle and the polygon which is crucial to solve this problem. I understand how to find if the center of the circle is inside the polygon, but not if the complete circle is contained in the polygon.

Anything helps :)

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
Gabe
  • 105
  • 2
  • 9
  • This is not specific to `c` and what is your approach to this problem? – Gaurav Oct 10 '18 at 20:10
  • Do you understand the math? – Support Ukraine Oct 10 '18 at 20:12
  • @Observer Correct. I am making this solution in C, however. I will edit the question to remove the tag. My approach so far is to find if the center of the circle is inside of the polygon. After that, I do not know what to do. I am trying to find the relationship between the radius of the circle and the position of the circle such that it fits completely inside the polygon. – Gabe Oct 10 '18 at 20:13
  • @4386427 I do not understand the math for the relationship between the radious/position of the circle and the polygon. I understand the math for calculating if the center of the circle is inside of the polygon. Thank you. – Gabe Oct 10 '18 at 20:14
  • 1
    In that case your starting point is to seek help about the math. You can't write a program for something you don't understand. Programming ain't magic - it requires that you know what you want to do – Support Ukraine Oct 10 '18 at 20:17
  • Once you know that the centre is within the polygon, check that the distance from the centre to each line (side) of the polygon is less than the radius. This assumes simple polygons (triangles are inevitably simple; pentagons could be like a pentangle/star — not simple). – Jonathan Leffler Oct 10 '18 at 20:18
  • 2
    I'm voting to close this question as off-topic because belongs to math site – pmod Oct 10 '18 at 20:18
  • I added the statement that *touching is ok* to the title as it actually makes a bunch of coerner cases arise and is a key issue in the solution of that question. – Olivier Melançon Oct 11 '18 at 03:42

2 Answers2

1

The following assumes you already know that the center of the circle is inside the polygon. There are a few things you want to check as your definition that touching the vertices is ok adds some cornercases. This solution works for concave polygons as well.

Early check

For the circle to be fully inside the polygon, we need all edges to be outside the circle. In particular this ensures that the polygon is not fully inside the circle.

Given the circle of radius r and centered at c and the edges e0, e1, ..., en of the polygon, a necessary condition is thus that for all i < n:

d(c, ei) >= r

where d is th euclidian distance.

If the above does not hold for any edge, then either there is an intersection between the polygon and the circle or the polygon is itself fully inside the circle.

Does the circle intersects the polygon

The last check is a necessary condition for the circle to be inside, although it is not sufficient as it is possible that all edges be outside the circle, but that the circle still leaks out of a vertice.

Let's first remember some formulas we will need.

Equation for a circle of radius r centred at (x0, y0):

(x - x0)2 + (y - y0)2 = r2

Thus the intersection with a line y = ax + b is found by solving:

(x - x0)2 + (ax + b - y0)2 = r2

This is nothing but a quadratic equation that can be rewritten as:

(a2 + 1)x2 + (2ab - 2ay0 - 2x0)x + (x0 + (b - y0)2 - r2) = 0

You can solve that with the quadratic formula for each vertex. You then have three possibilities.

1) There is no solution

This indicates there exists no intersection with this vertex. With high-level languages, you can catch some kind of MathError exception to detect that. Otherwise, you can mathematically check the sign of the discriminant as this case happens if it is negative.

(2ab - 2ay0 - 2x0)2 - 4 (a2 + 1) (x0 + (b - y0)2 - r2) < 0

2) There is a unique solution

If the equation has a single solution, that is both solution are the same, then the circle may touch, but does not leak out of the edge. You stated this is still considered to be inside the polygon in your case.

Mathematically, this happens when the discriminant is zero.

(2ab - 2ay0 - 2x0)2 - 4 (a2 + 1) (x0 + (b - y0)2 - r2) = 0

3) There exist two solutions

If there exist two solutions, say xi and xj, then there might be an overlap. Although, this is not certain in the case of concave polygons.

To check if there actually was an overlap, you must check if the intersection happens on your line segment.

This is quite simply done. Suppose your vertex lies between the points (x1, y1) and (x2, y2), then there is an intersection if an only if...

x1 < xi < x2

or...

x1 < xj < x2

In any other case, the intersection happens on the continuation of the vertex, not on the vertex itself.

If one of the above condition holds true, then and only then do you know that your circle leaks outside the polygon.

Final cornercase: concave edges

As stated, touching the polygon is ok, and thus there is a final cornercase not covered by the above: touching a concave edge.

A concave edge is an edge which inner angle is bigger than 180°. Thus whenever there is an intersection with the polygon, you want to ignore it if the intersection happens on an edge that is concave.

All of the above works for any polygon, not only triangles and hexagons.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
  • Could you elaborate with what you mean with vertex span or provide an example? Is it the same as spans in linear algebra? – Gabe Oct 10 '18 at 21:10
  • @Gabe There it is, I also added information on some cornercases that might happen. This is mostly due to your condition that *touching* the polygon is ok. – Olivier Melançon Oct 11 '18 at 03:24
0

Approach 1: simple, but NOT precise

You have already implemented an algorithm for checking whether a point is inside a polygon. So, why not just to approximate a circle as a equilateral polygon? You just check 16 or 64 or 256 points of the circle.


Approach 2: more complex, but IS precise

  1. Find normal vector for each side of you polygon (you can easily calculate it).
  2. Find distance from circle center to the side by this normal vector (lines intersection is a simple task too).
  3. If the distance smaller than circle's radius, then the circle is outside the polygon.
  4. Otherwise, if each side's distance to the point by it's normal is larger or equal to the circle's radius, then the circle is inside.

image

AivanF.
  • 1,134
  • 2
  • 23
  • 52
  • 2
    That makes lots of sense, thank you! I will be taking the second approach as accuracy is important for my application, – Gabe Oct 10 '18 at 20:37
  • 1
    @Gabe The second option may be wrong for concave polygons, you need to find the intersection points and compare them to the vertex domain. See my answer. – Olivier Melançon Oct 10 '18 at 20:44