Overlapping line segments in 2D space

余生颓废 提交于 2019-12-08 17:39:57

问题


I need to find whether two lines overlap each other. I have the intersection code which returns 0, if two lines are parallel. But then I need to know if these two parallel lines overlap.

Edit:

A                    C       B                D
-----------------------------------------------

Line 1: A-B

Line 2: C-D

I need to find if Line 1 overlaps Line 2, but both lines can have a slope > 0.


回答1:


You can compare to find if there is no over lap. you will have less comparisons in this way thus very efficient. Do following comparisons

D < A

B < C

If either case is true the lines are not overlapping. otherwise there must an overlap. You will make least number of comparisons to determine if they are not overlapping. Otherwise there will be more comparisons to make.




回答2:


Since you know they're both parallel, then just check whether line segment CD contains either of the endpoints of the first line (point A and point B).




回答3:


For two co-linear line segments that are not necessarily axis-aligned:

  1. Sort the vertices in clockwise order around the origin.
  2. The lines overlap if the ordered vertices alternate between the two segments, e.g. Line1.Point1, Line2.Point1, Line1.Point2, Line2.Point2.



回答4:


It is sufficient to calculate the areas of triangles ACB and CBD. If the area is 0, then the points are collinear, and if both areas are zero then the lines are overlapping.

You can calculate the area of a triangle ABC by this formula:

2*Area(ABC)= (bx – ax)(cy – ay) – (cx – ax)(by – ay);




回答5:


Line equation is direction of line in infinite, by finding slope or intercept you wont be able do anything with them(even though horizontal line doesn't have slope), i suggest use point on line. so AB is your line [(x,y),(x,y)] and C is on of the point (x,y) and then you need to check if point on the line.
Check Point On a Line




回答6:


We are given two line segments

AB = line segment from (Ax,Ay) to (Bx,By)
CD = line segment from (Cx,Cy) to (Dx,Dy)

with the same slope.

  • Order the endpoints E1 < E2 < E3 < E4 such that Ei,x ≤ Ei+1,x and Ei,y ≤ Ei+1,y if Ei,x = Ei+1,x
  • If E1 and E2 are from different segments, the overlap is the segment from E2 to E3.

There are some degenerate cases:

  • A < B = C < D
  • A < C = D < B
  • A < B = C = D
  • A = B = C = D

These result in a single point of intersection. I'm not sure if any of those can occur in your system, but if so you'll have to decide whether or not you consider that "overlap" and add special case checks.




回答7:


The characteristic of two segments of being in the same lines is called collinearity and can be tested calculating the area of the 2 triangles formed by one segment endpoints and, respectively, the endpoints of the other segment. If the area is zero or close to zero below a threshold the segments are collinear.

    public static bool AreSegmentsCollinear(Segment a, Segment b, double epsilon)
    {
        return IsPointCollinear(a, b.Left, epsilon) && IsPointCollinear(a, b.Right, epsilon);
    }

    public static bool IsPointCollinear(Segment a, Vector2D p, double epsilon)
    {
        return Math.Abs(GetSignedTriangleArea2(a, p)) <= epsilon;
    }

    /// <returns>The squared signed area of the triangle formed with endpoints
    /// of segment a and point p</returns>
    public static double GetSignedTriangleArea2(Segment a, Vector2D p)
    {
        return (p - a.Left) ^ (a.Right - a.Left);
    }

   /// <summary>Cross product of vectors in 2D space. NB: it returns a
   /// magnitude (scalar), not a vector</summary>
   /// <returns>The area of the parallelogram formed with u, v as the edges</returns>
   public static Double operator ^(Vector2D u, Vector2D v)
   {
       return u.X * v.Y - u.Y * v.X;
   }


来源:https://stackoverflow.com/questions/17148839/overlapping-line-segments-in-2d-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!