Two intersections of parallel segments

I know that there are many algorithms for checking the intersection of two line segments.

The line segments I'm talking about are a line of length drawn at two endpoints.

But as soon as they encounter a parallel state, they simply tell the user a big “No” and
pretend that there are no matches, no endpoint for shared access, or collusion at the endpoint.

I know that I can calculate the distance between two lines.
If the distance is 0, check for endpoints located in other segments of the line or not.
And that means I have to use a lot if else and && || terms.

It’s not difficult, but my question is

"Is there a computation method (or mathematics) to compute this special parallel case?"

Hope this picture clarifies my question
http://judark.myweb.hinet.net/parallel.JPG

+5
source share
5 answers

Yes, given the formulas for both lines, check if their slopes are equal. If they are, the lines are parallel and never intersect.

If you have dots on each of the lines, you can use the formula.

If both are perpendicular to the x axis, they will have infinite slopes, but they will be parallel. All points on each line will have the same x coordinates.

To process the line segments, calculate the intersection point, then determine if this intersection point exists for both segments.

+4

, , , - , ( , ), , .

, , , , :

if (segment_contains_point(segment_A, segment_B.start) || 
    segment_contains_point(segment_A, segment_B.end)) {
        // lines overlap
}
0

, , a.x + b.y + c = 0 d.x + e.y + f = 0. a = 0 and d = 0 b/a = e/d. , , b.d = a.e.

0

( , ) intercetion x, y else, intercetion , -1, -1

    Public Function intercetion(ByVal ax As Integer, ByVal ay As Integer, ByVal bx As Integer, ByVal by As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal dx As Integer, ByVal dy As Integer) As Point
    '//  Determines the intersection point of the line segment defined by points A and B
    '//  with the line segment defined by points C and D.
    '//
    '//  Returns YES if the intersection point was found, and stores that point in X,Y.
    '//  Returns NO if there is no determinable intersection point, in which case X,Y will
    '//  be unmodified.

    Dim distAB, theCos, theSin, newX, ABpos As Double

    '//  Fail if either line segment is zero-length.
    If ax = bx And ay = by Or cx = dx And cy = dy Then Return New Point(-1, -1)

    '//  Fail if the segments share an end-point.
    If ax = cx And ay = cy Or bx = cx And by = cy Or ax = dx And ay = dy Or bx = dx And by = dy Then Return New Point(-1, -1)

    '//  (1) Translate the system so that point A is on the origin.
    bx -= ax
    by -= ay
    cx -= ax
    cy -= ay
    dx -= ax
    dy -= ay

    '//  Discover the length of segment A-B.
    distAB = Math.Sqrt(bx * bx + by * by)

    '//  (2) Rotate the system so that point B is on the positive X axis.
    theCos = bx / distAB
    theSin = by / distAB
    newX = cx * theCos + cy * theSin
    cy = cy * theCos - cx * theSin
    cx = newX
    newX = dx * theCos + dy * theSin
    dy = dy * theCos - dx * theSin
    dx = newX

    '//  Fail if segment C-D doesn't cross line A-B.
    If cy < 0 And dy < 0 Or cy >= 0 And dy >= 0 Then Return New Point(-1, -1)

    '//  (3) Discover the position of the intersection point along line A-B.
    ABpos = dx + (cx - dx) * dy / (dy - cy)

    '//  Fail if segment C-D crosses line A-B outside of segment A-B.
    If ABpos < 0 Or ABpos > distAB Then Return New Point(-1, -1)

    '//  (4) Apply the discovered position to line A-B in the original coordinate system.
    '*X=Ax+ABpos*theCos
    '*Y=Ay+ABpos*theSin

    '//  Success.
    Return New Point(ax + ABpos * theCos, ay + ABpos * theSin)
End Function

0

: , , , : , ( ). A (A, B) . , ( , ), B. B, . , . , , , .

0

All Articles