Find the Intersection of Two Line Segments in 2D (Easy Method)
Вставка
- Опубліковано 10 лют 2025
- In this video, I will show you how to find the intersection of two line segments in 2D. This is a simple but important concept in geometry, and it can be used in many different applications. I will explain the method step-by-step, and I will also provide some examples.
Points:
A(x1,y1)
B(x2,y2)
C(x3,y3)
D(x4,y4)
Given two line segments AB and CD, find the point of intersection P(x0, y0).
Solution:
Vectors:
A = (x1, y1)
B = (x2, y2)
C = (x3, y3)
D = (x4, y4)
AB = (x2 - x1, y2 - y1)
CD = (x4 - x3, y4 - y3)
A + αAB = C + βCD
α = ((x4 - x3)(y3 - y1) - (y4 - y3)(x3 - x1)) / ((x4 - x3)(y2 - y1) - (y4 - y3)(x2 - x1)) = a / b
β = ((x2 - x1)(y3 - y1) - (y2 - y1)(x3 - x1)) / ((x4 - x3)(y2 - y1) - (y4 - y3)(x2 - x1)) = c / b
P = (x0, y0)
x0 = x1 + α(x2 - x1) = x3 + β(x4 - x3)
y0 = y1 + α(y2 - y1) = y3 + β(y4 - y3)
/ edgardocpu - Наука та технологія
Thank you for simple and fast explanation.
2:30 If a=b=0 means the line segments are collinear, does c=b=0 also mean the line segments are collinear?
Next video topic idea: If two line segments are collinear, how to find the overlap? Like defining the overlap as its own line segment and finding each endpoint.
Your video is very effective. Can you please tell me which tool you use to draw the diagrams and animate?
1:50 Can you go into more steps to work out equations for alpha? Each time I do, I get different order for numerator for alpha, like if the two multiplying parts were switched. As well as, I have the denominator for alpha and beta are different. I don't know where I am going wrong: (the workout below is from my handwork, the captions for each step were rewritten from chatGPT)
alpha*(x2-x1) - beta*(x4-x3) = x3-x1
alpha*(y2-y1) - beta*(y4-y3) = y3-y1
Solve for alpha:
# Solve for alpha in terms of beta:
beta = (alpha*(x2-x1) - (x3-x1)) / (x4-x3)
beta = (alpha*(y2-y1) - (y3-y1)) / (y4-y3)
# Since both equations equal alpha, they must be equal to each other:
[alpha*(x2-x1) - (x3-x1)] / (x4-x3) = [alpha*(y2-y1) - (y3-y1)] / (y4-y3)
# Cross-multiply to eliminate fractions:
(y4-y3)*(alpha*(x2-x1)-(x3-x1)) = (alpha*(y2-y1)-(y3-y1))*(x4-x3)
# Expand and rearrange the equation to isolate alpha:
(alpha*(x2-x1)*(y4-y3)) - (x3-x1)*(y4-y3) = (alpha*(x4-x3)*(y2-y1)) - (x4-x3)*(y3-y1)
# Now, isolate alpha:
(alpha*(x2-x1)*(y4-y3)) - (alpha*(x4-x3)*(y2-y1)) = (x3-x1)*(y4-y3) - (x4-x3)*(y3-y1)
# Factor out alpha:
alpha*[(x2-x1)*(y4-y3) - (x4-x3)*(y2-y1)] = (x3-x1)*(y4-y3) - (x4-x3)*(y3-y1)
# Finally, solve for alpha:
alpha = [(x3-x1)*(y4-y3) - (x4-x3)*(y3-y1)] / [(x2-x1)*(y4-y3) - (x4-x3)*(y2-y1)]
As you can see from alpha, the two products in the numerator and denominator are switched from the alpha formula in the video. Why?
Note 1: When I ask chatGPT, they just say my result is correct. Which to me seems wrong.
Note 2: The equations for alpha and beta in video appear in a different order than to t and u from the wiki of "Given two points on each line segment"
I got alpha as in the video by isolating alpha to the right side, rather than the left. But I got beta as in the video by isolating to the left. Weird, especially when doing by hand.
I got his results by setting it up as a matrix equation and using Cramer's Rule. I subtracted x1 and y1 to the right and β(x4 - x3) and β(y4 - y3) to the left. Cramer's Rule for α and β worked pretty slick. Might have to adjust some of the commutative order on a few things to make it look the same, but it worked out for me.