Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Could you let me know the formlua for finding intersection of two lines ASAP? I have gone through google search could not find useful one so far.

Line1: (x1,y1)-----------------------------------(x2,y2)

Line2: (x3,y3)-----------------------------------(x4,y4)

Line1: (x1,y1)-------------------

Line2: (x3,y3)-------------------

There are various ways to do it but there isn't really a straight formula...it's more of a process.

The segments could:

(1) Be parallel and never intersect.

(2) Be parallel and intersect in a point or overlap in a segment.

(3) Be parallel and not intersect, but would if they were extended.

(4) Be non-parallel and intersect in a point.

(5) Be non-parallel and not intersect, but you need to know where they would intersect if one or both segments were extrapolated.

I've used this method successfully in VB.Net:

http://www.cs.princeton.edu/algs4/91primitives/

Look in the third section called "Line segment intersection" which uses parameterized formulas and determinants. You have to handle the special cases with ugly code but this approach will give you ALL of the information I outlined in my previous post.

Lemme know if you want to see the VB.Net code...

Its just algebra.

1. First you need the equations of the two lines.

(a) a straight line will have the form of y = mx+b where m is the gradient, or slope, of the line and b is the intercept (the point where it crosses the y-axis). Since you only have two points for each line they are, by cynical definition straight)

(b) the gradient is m = (y2 - y1) / (x2 - x1) where x2 not equal x1

(c) you can test whether the two lines actually do intersect by the simple logic, that only parallel lines do not intersect (if extended forever in each direction). So, if parallel, they would both have the same gradient, otherwise they intersect, so continue.

(d) to get the intercept; after calculating the gradient, take a point and plug the x and y coordinates into the equation b = y - mx

now you have the equation of the first line, now do the second.

2. Then, since at the point of intersection, the two equations will have the same values of x and y, you set the two equations equal to each other. This gives an equation that you can solve for x

3. You substitute that x value in one of the line equations (it doesn't matter which) and solve it for y.

This gives you the x and y coordinates of the intersection.

I used the determinant method to solve the equations, error handling to find out if the lines are parallel. Also finds out whether the line segments need to be extended.

```
'Take values from text-boxes
X1 = Val(Text1.Text)
Y1 = Val(Text2.Text)
X2 = Val(Text3.Text)
Y2 = Val(Text4.Text)
X3 = Val(Text5.Text)
y3 = Val(Text6.Text)
X4 = Val(Text7.Text)
y4 = Val(Text8.Text)
'Intermediate variables
a = Y1 - Y2
b = X2 - X1
c = X2 * Y1 - X1 * Y2
d = y3 - y4
e = X4 - X3
f = X4 * y3 - X3 * y4
denom = (a * e - b * d)
'Get the point of intersection
If denom <> 0 Then
x = (c * e - b * f) / denom
y = (a * f - c * d) / denom
Else
Text9.Text = "Lines do not intersect"
End If
'Find out if the point lies in the segment
Text9.Text = "x = " & Format$(x, "#.###") & " y = " & Format$(y, "#.###")
If Not (((x > X1) And (x < X2)) Or ((x < X1) And (x > X2))) Then
Text9.Text = Text9.Text & " : Line1 needs to be extended"
End If
If Not (((x > X3) And (x < X4)) Or ((x < X3) And (x > X4))) Then
Text9.Text = Text9.Text & " : Line2 needs to be extended"
End If
```

If Not (((x >= X1) And (x <= X2)) Or ((x <= X1) And (x >= X2))) Then

Text9.Text = Text9.Text & " : Line1 needs to be extended"

End If

If Not (((x >= X3) And (x <= X4)) Or ((x <= X3) And (x >= X4))) Then

Text9.Text = Text9.Text & " : Line2 needs to be extended"

End If

There shoud be comparison with equality.

'Take values from text-boxes

X1 = Val(Text1.Text)

Y1 = Val(Text2.Text)

X2 = Val(Text3.Text)

Y2 = Val(Text4.Text)

X3 = Val(Text5.Text)

y3 = Val(Text6.Text)

X4 = Val(Text7.Text)

y4 = Val(Text8.Text)

'Intermediate variables

a = Y1 - Y2

b = X2 - X1

c = X2 * Y1 - X1 * Y2

d = y3 - y4

e = X4 - X3

f = X4 * y3 - X3 * y4

denom = (a * e - b * d)

'Get the point of intersection

If denom <> 0 Then

x = (c * e - b * f) / denom

y = (a * f - c * d) / denom

Text9.Text = "x = " & Format$(x, "#.###") & " y = " & Format$(y, "#.###")

'Find out if the point lies in the segment

If Not (((x >= X1) And (x <= X2)) Or ((x <= X1) And (x >= X2))) Then

Text9.Text = Text9.Text & " : Line1 needs to be extended"

End If

If Not (((x >= X3) And (x <= X4)) Or ((x <= X3) And (x >= X4))) Then

Text9.Text = Text9.Text & " : Line2 needs to be extended"

End If

Else

Text9.Text = "Lines do not intersect"

End If

All Courses

From novice to tech pro — start learning today.

Open in new window