SoonSYJ
SoonSYJ

Reputation: 211

The intersection between a trajectory and the circles in the same area

I am new in coding. Now I have a question. I have an object who keep moving in an rectangle area. And I also have a lot of circle in this area too. I want to get all the intersection point between the trajectory and the all the circle. As the object is moving step by step, so was thinking that I can calculate the distance between the position of object and all the centre of each circle and compare the distance with radius of the circle. But I think that this will do a lot of computation as you need to calculate the distance at each step. Do you have any good idea or reference. By the way, I am woking on python. Thank you. As I do not have enough reputation , I can not add a picture about the problem

Upvotes: 0

Views: 1654

Answers (3)

MvG
MvG

Reputation: 61057

Unless your trajectory is already a straight line, you might want to compute a piecewise linear approximation of it. Then for each segment you can compute line-circle intersections using a quadratic equation, and check whether the points of intersection are real (as opposed to complex if the line passes by the circle and the term under the square root becomes negative) and whether they are on the segment (as opposed to the parts of the line beyond the endpoints).

Suppose you have a line segment from (x1,y1) to (x2,y2) and want to intersect that with a circle centered at (xc,yc) with radius r. Then you want to solve the equation

((1 - t)*x1 + t*x2 - xc)² + ((1 - t)*y1 + t*y2 - yc)² = r²

If you collect terms based on the power of t you get the following quadratic equation in t:

                      ((x1 - x2)² + (y1 - y2)²)*t²
+ 2*((x1 - x2)*(xc - x1) + (y1 - y2)*(yc - y1))*t
+                ((xc - x1)² + (yc - y1)² - r²)    = 0

So you could write this in Python code as follows (untested):

def circleSegmentIntersections(x1, y1, x2, y2, xc, yc, r):
    dx = x1 - x2
    dy = y1 - y2
    rx = xc - x1
    ry = yc - y1
    a = dx*dx + dy*dy
    b = dx*rx + dy*ry
    c = rx*rx + ry*ry - r*r
    # Now solve a*t^2 + 2*b*t + c = 0
    d = b*b - a*c
    if d < 0.:
        # no real intersection
        return
    s = math.sqrt(d)
    t1 = (- b - s)/a
    t2 = (- b + s)/a
    if t1 >= 0. and t1 <= 1.:
        yield ((1 - t1)*x1 + t1*x2, (1 - t1)*y1 + t1*y2)
    if t2 >= 0. and t2 <= 1.:
        yield ((1 - t2)*x1 + t2*x2, (1 - t2)*y1 + t2*y2)

If your trajectory is curved but has some nice mathematical description, like a free-fall parabola or a Bézier curve or something like that, then you might avoid the piecewise linear approximation and try to compute the intersection directly. But chances are that doing so would entail finding roots of some higher-order polynomial, which can only be done numerically.

Upvotes: 1

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

Let a be a number somewhere between the radius and diameter of the larger circles (if they have different radii).

Generate a grid of square tiles of side length a, so that grid(i,k) is the square from (i*a,k*a) to ((i+1)*a, (k+1)*a).

Each tile of the grid contains a list with pointers to circles or indices into the circle array.

For each circle, register it with each tile that it intersects with. Should be less than 4.


Now to test the point (x,y) of the trajectory for circle intersections resp. containment inside the corresponding disk, you only need to test it against the list of circles in tile ((int)(x/a), (int)(y/a).

Upvotes: 1

Marco Nawijn
Marco Nawijn

Reputation: 159

In general I would recommend to first make your algorithm work and then make it faster if you need to. You would be amazed by how fast Python in combination with a set of carefully selected libraries can be.

So for your problem, I would do the following:

1.) Install a set of libraries that makes your life easier: - Matplotlib for 2D plotting of the rectangle, the circle and the trajectory

2.) Numpy for general purpose array manipulation

3.) Optionally Scipy for its KDTree support (nearest neighbor search)

4.) Start implementing your problem a.) Create a rectangle and visualize it using Matplotlib

b.) Create a set of circles and plot them within the rectangular area of 4a

c.) Create a trajectory and plot them within the rectangular area

Now the more difficult part starts. The way forward depends a little on how your trajectory is defined. For example, if your trajectory consists of line segments, you could calculate the intersection point between a circle and a line segment analytically. Three possible solutions exist, no intersection, 1 intersection (line touches circle) and 2 intersections. If your trajectory is more complex, you could discretize it by generating many points along it and than calculate if this point is on the edge of one of the circles. You have to be a little clever though about how the 3 possible solutions can be identified, because the points along the trajectory are finite.

Another option would be to also discretize the points on the edges of the circles. This would mean that the problem reduces for a large part to nearest neighbor search for which you can use the Scipy KDTree class.

Upvotes: 0

Related Questions