0

I'm a newbie at Mathematica. I'm trying to build a line in three dimensions. The best I can do is to build two planes and get a line on the intersection of those planes, like this:

Plot3D[{y = 2 + x, z = 2 - x}, {x, -2, 2}, {y, -2, 2},
  ColorFunction -> "RustTones"]

It would be nice if someone also can share code showing how to build a line using two vectors.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257

3 Answers3

8

If you want to draw a complicated line, take a look at ParametricPlot3D

ParametricPlot3D[{x, 3 Sin[x], x^2}, {x, -1, 2}]

enter image description here

If you just want to draw a straight line, take a look at InfiniteLine

Graphics3D[{InfiniteLine[{2, 1, 1}]}, Axes -> True, AxesLabel -> {x, y, z}, Ticks -> Automatic]

enter image description here

Mauricio Fernández
  • 5,463
  • 21
  • 45
5
eq1 = y - x - 2;
eq2 = z + x - 2;

One can solve a system of equations

sol = {x, y, z} /. Solve[{eq1 == 0, eq2 == 0}, {x, y, z}]

{{x, 2 + x, 2 - x}}

to get the intersection in parametric form:

ParametricPlot3D[Evaluate@sol, {x, -2, 2}, PlotStyle -> {Thick, Red}]

enter image description here


A different approach is to use Mesh functions:

ContourPlot3D[{eq1 == 0, eq2 == 0}, {x, -2, 2}, {y, -2, 2}, {z, -2, 
  6}, MeshFunctions -> {Function[{x, y, z, f}, eq1 - eq2]}, 
 MeshStyle -> {Thick, Red}, Mesh -> {{0}}, 
 ContourStyle -> Directive@{Opacity[0.5]}]

enter image description here

One can fade the surfaces completely to have only the highlighted intersection left:

ContourPlot3D[{eq1 == 0, eq2 == 0}, {x, -2, 2}, {y, -2, 2}, {z, -2, 
  6}, MeshFunctions -> {Function[{x, y, z, f}, eq1 - eq2]}, 
 MeshStyle -> {Thick, Red}, Mesh -> {{0}}, 
 ContourStyle -> Directive@{Opacity[0]}, BoundaryStyle -> Transparent]

enter image description here

corey979
  • 23,947
  • 7
  • 58
  • 101
4

You could use the equation to generate points and use ListPointPlot3D:

y[x_] := 2 + x;
z[x_] := 2 - x;
ListPointPlot3D[Table[{x, y[x], z[x]}, {x, -2, 2, 0.01}]]

Points

You could use a Graphics3D with Line (this is the one I would recommend, use Axes if you want Plot type axes):

Graphics3D[{Red, Line[{{-2, y[-2], z[-2]}, {2, y[2], z[2]}}]}]

graphics

lowriniak
  • 2,412
  • 10
  • 15