tags:

views:

244

answers:

4

I know there are many algorithms to verify whether two line segments are intersected.

The line segments I'm talking about are length line constructed by 2 end points.

But once they encountered parallel condition, they just tell the user a big "No" and
pretend there is no overlap, share end point, or end point collusion.

I know I can can calculate the distance between 2 lines segments.
If the distance is 0, check the end points located in the other line segments or not.
And this means I have to use a lot of if else and && || conditions.

This is not difficult, but my question is

"Is there a trick( or mathematics) method to calculate this special parallel case?"

I hope this picture clarify my question
http://judark.myweb.hinet.net/parallel.JPG

+2  A: 

Yes, given the formulas for both of the lines, test whether their slopes are equal. If they are, the lines are parallel and never intersect.

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

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

To deal with line segments, calculate the point of intersection, then determine if that point of intersection exists for both of the segments.

WhirlWind
Oh my. I guess I'd better fill the character requirement. I was mistaken. =P
Chris Cooper
Haha... *shuffles feet* =)
Chris Cooper
@Chris ahh, well, I've done it too ;)
WhirlWind
@Tyler: But I would add what my original answer says: I think determining if a given point is on both lines should be an easier way to distinguish the two cases than finding the distance bewteen two lines.
Chris Cooper
Make it simple, my line segments are lengthed lines, but not infinite straight lines. Excep the intersection or not, I have all other info like slope, distance, vector. That's for the other calcualtions :)
Judarkness
Your question is really confusing then. If two line segments are parallel, they don't intersect. Isn't that what you asked? That's what I thought, since you wrote: "Is there a trick( or mathematics) method to calculate this special parallel case?"
WhirlWind
@whirlwind he's talking line segments, not lines. The question is, for 2 (colinear) parallel line segments, how to determine if there is overlap?
David Gelhar
I didn't see the picture... or the word "colinear." His edit resolves that.
WhirlWind
+2  A: 

I assume the case you're interested in is where the two line segments are parallel (as determined by checking the slope, as Whirlwind says), and you're trying to determine whether the two segments overlap.

Rather than worrying about the distance between the lines, I would think the easiest way to do that would to if either endpoint of one segment lies within the other:

if (segment_contains_point(segment_A, segment_B.start) || 
    segment_contains_point(segment_A, segment_B.end)) {
        // lines overlap
}
David Gelhar
This is just what I want, I have my own segment_contains_points()(in other name)But It's way too inefficient.And I have to call it three times because segment B might be larger and contains the whole segment A.And I want to know if there is a better solution?
Judarkness
A: 

Let's assume that you have two lines described by formulas a.x + b.y + c = 0 and d.x + e.y + f = 0. The two lines are parallel when a = 0 and d = 0 or b/a = e/d. Perhaps instead of doing the division just make sure that b.d = a.e.

rmarimon
+1  A: 

i found this (modified a little by me to suit) it will return the intercetion x,y else if no intercetion found it will return -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

Origin

Robert