# Finding intersection points of the Line command

Suppose I have two lists to which I apply the Line command for example

A = {{-4,0},{4,0}}
B = {{0,4},{0,-4}}


and I take Line[A] and Line[B]. Is there a way to get Mathematica to tell me the intersection points of the line? Of course this is a very simple example, in practice the lines would have many defining points to approximate a curve.

further questions: How about if I had $$n$$ lists? Could I ask to find the intersection points in some bounded region only?

Join each of your collections of lines using a RegionUnion into a single region, then intersect both regions with RegionIntersection like so:

redLines = {
Line[{{-2., 4.}, {-1.5, 2.25}, {-1., 1.}, {-0.5, 0.25}, {0., 0.}, {0.5, 0.25}, {1., 1.}, {1.5, 2.25}, {2., 4.}}]
, Line[{{2, -1}, {2.5, 2}}]
};
blueLines = {
Line[{{22., -3.}, {15.49, -2.3}, {9.96, -1.6}, {5.41, -0.9}, {1.84, -0.2}, {-0.75, 0.5}, {-2.36, 1.2}, {-2.99, 1.9}, {-2.64, 2.6}, {-1.31, 3.3}, {1., 4.}, {4.29, 4.7}, {8.56, 5.4}, {13.81, 6.1}, {20.04, 6.8}}]
, Line[{{-3, 1}, {2, 2}}]
};

intersections = RegionIntersection[RegionUnion@redLines, RegionUnion@blueLines];
isectCoordinates = Flatten[MeshPrimitives[intersections, 0] /. Point -> List, 1];
Graphics[{Red, redLines, Blue, blueLines, Black, PointSize[Large],intersections},
PlotRange -> {{-3, 3}, {-5, 5}}]


And if you want to extract the lines from a shape (e.g square, triangle etc.) then use MeshPrimitives[shape, 1].

• Thanks for this, I'll give it a go. Is there a way to extract the coordinates of the intersection using this method?
– math
Commented Jun 10, 2020 at 15:21
• You don't need to extract them - I've already calculated them and that's what this line does intersections = RegionIntersection[RegionUnion@redLines, RegionUnion@blueLines]; On the result, replace the Point heads with List if you want them in that form. Commented Jun 10, 2020 at 15:33
• Sorry, I don't get what you mean about replacing the Point heads with List. The output is a mesh and I have no experience with these in Mathematica.
– math
Commented Jun 10, 2020 at 16:38
• Sorry I made a mistake - this will get you the points MeshPrimitives[intersections, 0] - I've added isectCoordinates in my answer. Commented Jun 10, 2020 at 16:41
• Thanks for this!
– math
Commented Jun 10, 2020 at 16:50

### GraphicsMeshFindIntersections

GraphicsMeshMeshInit[];

findIntersections = Complement[GraphicsMeshFindIntersections[Join[##]],
Join @@ GraphicsMeshFindIntersections /@ {##}] &;


Using redLines and blueLines from flinty's answer:

intersections = findIntersections[redLines, blueLines]

{{-1.73595,3.07582}, {-0.648352,0.472527}, {0.385965,0.192982}, {-1.14815,1.37037},
{1.34783,1.86957}, {2.12405,-0.255696}}

Graphics[{Red, redLines, Blue, blueLines, Black, PointSize[Large], Point@intersections},
PlotRange -> {{-3, 3}, {-2, 5}}]


SeedRandom[1]
greenLines = {Line[RandomReal[{-3, 3}, {8, 2}]],
BezierCurve[RandomReal[{-2, 2}, {7, 2}]]};

intersections = findIntersections[redLines, blueLines, greenLines]

 {{-1.73595,3.07582}, {-1.63671,1.27266}, {-1.43717,0.798769},
{-1.214,1.3572}, {-1.14815,1.37037}, {-1.13457,1.33642},
{-0.648352,0.472527},{-0.601914,0.459977}, {-0.541359,0.312038},
{-0.487964,0.42918},{-0.444653,0.222327}, {-0.373226,0.398169},
{-0.295139,0.147569},{0.385965,0.192982}, {1.01337,0.0234133},
{1.16581,-0.0177874}, {1.34783,1.86957}, {2.12405,-0.255696},
{2.41381,1.48283}}

Graphics[{Red, redLines, Blue, blueLines, Green, greenLines,
Black, PointSize[Medium], Point@intersections},
PlotRange -> {{-3, 3}, {-3, 5}}]


• I'm not well acquainted with using these types of Graphics commands, is there some advantage this code has over flinty's?
– math
Commented Jun 15, 2020 at 7:42
• @math, one advantage I am aware of is that it can handle BezierCurve, BSplineCurve as well as Line primitives in the input lists without additional processing. Region/Mesh functionality do not work with bezier and b-spline curves yet.
– kglr
Commented Jun 15, 2020 at 8:31
• Thank you for this!
– math
Commented Jun 15, 2020 at 10:44