3
$\begingroup$

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?

$\endgroup$
2

2 Answers 2

4
$\begingroup$
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[2] *)
$\endgroup$
3
$\begingroup$
$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[2] *)

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[[1]], {-2, 1}],
  Dashed,
  Lighter[Blue], lineABinf,
  Green, lineCDinf,
  Dashing[{}],
  Darker[Blue], lineAB,
  Darker[Green], lineCD,
  Red, AbsolutePointSize[6], pt},
 Frame -> True,
 PlotRange -> {{-5, 75}, {-25, 55}}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.