4
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ Try RegionIntersection? $\endgroup$
    – MarcoB
    May 23, 2020 at 15:12
  • 3
    $\begingroup$ 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. $\endgroup$ May 23, 2020 at 15:21
  • $\begingroup$ @J.M. ahh that makes sense, what would be the best way to just get the intersections with a hollow cylinder? $\endgroup$
    – Mark
    May 23, 2020 at 15:23
  • $\begingroup$ @Mark @jm just told you: use the RegionBoundary approach in his comment to get only the surface. $\endgroup$
    – MarcoB
    May 23, 2020 at 15:30

2 Answers 2

7
$\begingroup$

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[6], Point[pts]}},
 PlotRange -> {{-1.1, 1.1}, {-1.1, 1.1}, {-1.1, 1.1}}]

enter image description here

$\endgroup$
2
$\begingroup$

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.}]}
$\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.