简体   繁体   中英

How can we find intersection of circle and polygon for no point of polygon in circle

Suppose i have one polygon and one circle of google drawingmanager (google maps). The scenario is that no point of polygon is in the circle but still intersecting. How can i check the intersection in this case? ![Circle and Polygon are intersecting and there is no point of polygon inside the circle][1]

Check each point of polygon for this condition:

dx^2+dy^2 < r^2

where dx = px[i] - cx, dy = py[i] - cy,
px[i], py[i] - i-point of polygon, cx,cy - circle center, r - radius

If its true for at least one i, then intersection have place.

Update:

In case if no point is directly in circle, it's getting harder. You'll need to check each line against circle for intersections.

To detect intersection of line with circle, use following check:

line equation is l(t) = [lx(t), ly(t)]
where lx = x0 + t*(x1-x0), ly = y0 + t*(y1-y0), and t here is variable between 0 and 1

if line intersects circle, there will be such value of t (say, t0), with which
l0x = lx(t0), l0y = ly(t0) fills condition

(l0x - cx)^2 + (l0y - cy)^2 < r^2

we need to find t0, and check if it's in range 0..1, here how we do it:

(x0 + t0*(x1-x0) - cx)^2 + (y0 + t0*(y1-y0) - cy)^2 = r^2

by solving this quadratic equation we will find 0, 1 or 2 solutions of t0
if it's 0 solutions - circle never intersects line
if it's 1 solution - circle touches line in 1 point
if it's 2 solution (t0a, t0b) - circle intersects line in 2 points, and additional check will be needed to check if range [t0a, t0b] intersects with range [0, 1]

to solve this equation we need normalize it:
(x0 + t0*(x1-x0) - cx)^2 + (y0 + t0*(y1-y0) - cy)^2 = r^2       equal to
((x0-cx) + t0*(x1-x0))^2 + ((y0 - cy) + t0*(y1-y0))^2 - r^2 = 0      equal to
(x0-cx)^2 + (t0*(x1-x0))^2 + 2*t0*(x1-x0)*(x0-cx) + (y0-cy)^2 + (t0*(y1-y0))^2 + 2*t0*(y1-y0)*(y0-cy) - r^2 = 0    equal to

t0^2 * A + t0 * B + C = 0, where
A = (x1-x0)^2 + (y1-y0)^2
B = 2*(x1-x0)*(x0-cx) + 2*(y1-y0)*(y0-cy)
C = (x0-cx)^2 + (y0-cy)^2 - r^2

I will not write here how to solve such standart quadratic equation, it's offtopic.

If you will have a solution with 2 values, say t0a and t0b, then you need to check it against range [0, 1].

For example:

t0a = -3.4, t1a = 2.1 - intersection occurs, 
t0a = -3.4, t1a = -2.1 - intersection not occurs, 
t0a = 0, t1a = 0.5 - intersection occurs 

Yeah, and probably you will need to sort t0a and t0b, there is no guaranty that t0a < t0b

You will need to run this check for each line of polygon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM