# Calculating and displaying intersection of cylinder and line

I am trying to compute and display the intersection of a line defined by two points and a cylinder centered around the $$z$$-axis defined by length and radius. So far I have

cyl = Cylinder[{{0, 0, -1}, {0, 0, 1}}, 1]
line = InfiniteLine[{{0, 0, 0}, {1, 0, 0}}]
pts = Solve[{x, y, z} ∈ cyl && {x, y, z} ∈ line, {x, y, z}, Reals]


But this returns y -> ConditionalExpression[0, -1 <= x <= 1], z -> ConditionalExpression[0, -1 <= x <= 1] instead of a single solution. Any hint why this is the case and how to display the solution with the intersection in a nice way?

New contributor
Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• – MarcoB May 23 at 15:12
• Third point under "Details" in the docs for Cylinder[]: "Cylinder represents a filled cylinder region..." (emphasis mine). So yes, the result you got is correct, and you indeed have a continuum of points. Use RegionBoundary[Cylinder[(* stuff *)]] if you only want the surface. – J. M.'s technical difficulties May 23 at 15:21
• @J.M. ahh that makes sense, what would be the best way to just get the intersections with a hollow cylinder? – Mark May 23 at 15:23
• @Mark @jm just told you: use the RegionBoundary approach in his comment to get only the surface. – MarcoB May 23 at 15:30

For the display

cyl = Cylinder[{{0, 0, -1}, {0, 0, 1}}, 1];
line = InfiniteLine[{{0, 0, 0}, {1, 0, 0}}];
pts = {x, y, z} /. Solve[{x, y, z} ∈
RegionIntersection[line, RegionBoundary[cyl]], {x, y, z}]

(* {{-1, 0, 0}, {1, 0, 0}} *)

Graphics3D[
{{Opacity[0.25], cyl},
{Thick, Red, line},
{Black, AbsolutePointSize, Point[pts]}},
PlotRange -> {{-1.1, 1.1}, {-1.1, 1.1}, {-1.1, 1.1}}] You can also use a composition of RegionIntersection, DiscretizeRegion and MeshCoordinates:

MeshCoordinates[DiscretizeRegion @ RegionIntersection[cyl, line]]

{{-1., 0., 0.}, {1., 0., 0.}}


Or combine the three steps in a function:

intersection = MeshCoordinates @* DiscretizeRegion @* RegionIntersection;
intersection[cyl, line]

{{-1., 0., 0.}, {1., 0., 0.}}


Alternatively, you can use a combination of RegionIntersection, DiscretizeRegion and MeshPrimitives:

MeshPrimitives[DiscretizeRegion@RegionIntersection[cyl, line], 0]

{Point[{-1., 0., 0.}], Point[{1., 0., 0.}]}