# Intersection point of two lines given starting points and ending points of both lines

I'm creating a program which requires me to get intersection point of two lines. I have tried using different formulas but none of them works perfectly for me. Following is one example which I found on math.stackexchange.

x = ((x1 - x2) * (u1 * v2 - u2 * v1) - (u2 - u1) * (x2 * y1 - x1 * y2)) / ((v1 - v2) * (x1 - x2) - (u2 - u1) * (y2 - y1))

y = (u1 * v2 * y1 - u1 * v2 * y2 - u2 * v1 * y1 + u2 * v1 * y2 - v1 * x1 * y2 + v1 * x2 * y1 + v2 * x1 * y2 - v2 * x2 * y1) / (-1 * u1 * y1 + u1 * y2 + u2 * y1 - u2 * y2 + v1 * x1 - v1 * x2 - v2 * x1 + v2 * x2)


given these points A = (0.0, 0.0), B = (0.0, 50.0), C = (20, 0), D = (70.0, 50) my lines are AB and CD. There is no intersection between above two lines. with above method it gives me following answer: (0.0, -20.0). Do you know of some method that makes sure that the line is not extended to infinity?

• You are looking for the intersection between line segments, rather than between (infinite) lines. See eg How do you detect where two line segments intersect? and "Intersections" on Wikipedia. Apr 8 at 1:36
• @MarcoB Thanks Sir! That helped alot. I tried restricting the program to look for points only between the given line and it gave me correct result. Apr 10 at 1:37

a = {0.0, 0.0}; b = {0.0, 50.0};
c = {20, 0}; d = {70.0, 50};
ab = Line[{a, b}]; cd = Line[{c, d}];
RegionIntersection[{ab, cd}]

(* Out: EmptyRegion *)

\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global*"]


To avoid conflict with built-in symbol names, do not use capital letters for (or to start) user-defined symbols. For example C and D have pre-defined meanings.

a = {0, 0}; b = {0, 50}; c = {20, 0}; d = {70, 50};


For infinite lines,

lineABinf = InfiniteLine[{a, b}];
lineCDinf = InfiniteLine[{c, d}];


The infinite lines intersect at

pt = RegionIntersection[lineABinf, lineCDinf]

(* Point[{0, -20}] *)


For finite lines,

lineAB = Line[{a, b}];
lineCD = Line[{c, d}];

RegionIntersection[lineAB, lineCD]

(* EmptyRegion *)


Graphically,

Graphics[{
Text["A", a, {-2, 0}],
Text["B", b, {-2, 0}],
Text["C", c, {-2, 1}],
Text["D", d, {-2, 1}],
Text["pt", pt[], {-2, 1}],
Dashed,
Lighter[Blue], lineABinf,
Green, lineCDinf,
Dashing[{}],
Darker[Blue], lineAB,
Darker[Green], lineCD,
Red, AbsolutePointSize, pt},
Frame -> True,
PlotRange -> {{-5, 75}, {-25, 55}}]
` 