简体   繁体   English

轨迹与同一区域中的圆之间的交点

[英]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. 顺便说一句,我在python上醒来。 Thank you. 谢谢。 As I do not have enough reputation , I can not add a picture about the problem 由于我的信誉不足,因此无法添加有关该问题的图片

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 . 假设您有一个从( x1y1 )到( x2y2 )的线段,并希望与以( xcyc )为中心,半径为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 : 如果基于T你在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): 因此,您可以按以下方式(未经测试)在Python代码中编写此代码:

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. 但是这样做的机会是需要找到一些高阶多项式的根,而这只能通过数值来完成。

Let a be a number somewhere between the radius and diameter of the larger circles (if they have different radii). a数字,该数字介于大圆的半径和直径之间(如果半径不同)。

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) . 生成一个边长为a的正方形瓦片的grid(i,k) ,以使grid(i,k)是从(i*a,k*a)((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. 应该小于4。


Now to test the point (x,y) of the trajectory for circle intersections resp. 现在测试圆形相交点的轨迹点(x,y) containment inside the corresponding disk, you only need to test it against the list of circles in tile ((int)(x/a), (int)(y/a) . 包含在相应磁盘中的容器中,只需要针对tile ((int)(x/a), (int)(y/a)中的圆圈列表进行测试即可。

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. Python与一组精心选择的库相结合的速度会令您惊讶。

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 1.)安装一组使您的生活更轻松的库:-Matplotlib,用于二维绘制矩形,圆形和轨迹

2.) Numpy for general purpose array manipulation 2.)Numpy用于通用数组操作

3.) Optionally Scipy for its KDTree support (nearest neighbor search) 3.)Scipy为其KDTree支持(最近邻居搜索)(可选)

4.) Start implementing your problem a.) Create a rectangle and visualize it using Matplotlib 4.)开始解决您的问题a。)创建一个矩形并使用Matplotlib对其进行可视化

b.) Create a set of circles and plot them within the rectangular area of 4a b。)创建一组圆并将其绘制在4a的矩形区域内

c.) Create a trajectory and plot them within the rectangular area c。)创建轨迹并将其绘制在矩形区域内

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. 存在三种可能的解决方案:无相交,1个相交(直线接触圆)和2个相交。 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. 但是,对于如何识别3种可能的解决方案,您必须有点机灵,因为沿着轨迹的点是有限的。

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. 这将意味着该问题在很大程度上减少了您可以使用Scipy KDTree类的最近邻居搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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