How can I write code for displaying two lines in 3D space at random and connecting all points of line A with all points of line B and displaying the resulting surface in Mathematica (what would it's equation be).
Can someone please randomize the placement of the two lines A and B in 3D space several times and show me the different 3D lines and surface outputs?
I need this for a topological investigation I need to carry out exploring this concept first.
I am new to Mathematica and have never used it before.
I also do not have access to it because it is expensive (and I'm at home, not on a campus).
Can someone please help me with the visualization.
I would be extremely thankful to you if you could help me.
Thanks.
3 Answers 3
I think you're just trying to make a closed surface from four points? Here's a fast way to do that which you could easily run in any system (e.g. python with X3D)
First we make the triangulation
lines = RandomReal[{}, {2, 2, 3}];
tris = Flatten[
Table[
Append[lines[[i]], lines[[Mod[i + 1, 2, 1], j]]],
{i, 2},
{j, 2}
],
1
];
and then we'll visualize the faces
Graphics3D[
{
{Thick, Red, Line@lines[[1]]},
{Thick, Blue, Line@lines[[2]]},
MapThread[
{#, Triangle[#2]} &,
{
{Green, Orange, Purple, Pink},
tris
}
]
},
Boxed -> False,
Lighting -> "Neutral"
]
Maybe you want to discretize between endpoints and connect, though, here's what that would look like
subdivs = 100;
lines = RandomReal[{}, {2, 2, 3}];
linePoints = Subdivide[#, #2, subdivs] & @@@ lines;
Graphics3D[{
{
Red,
Point@linePoints[[1]]
},
{
Blue,
Point@linePoints[[2]]
},
MapThread[Line@*List, linePoints]
},
Boxed -> False
]
Here's another interpretation, taking the nearest points from one line to another. This might have an analytic form, but it's fast to discretize
subdivs = 100;
lines = RandomReal[{}, {2, 2, 3}];
linePoints = Subdivide[#, #2, subdivs] & @@@ lines;
lineFriends = Nearest[linePoints[[2]], linePoints[[1]]][[;; , 1]];
Graphics3D[{
{
Red,
Point@linePoints[[1]]
},
{
Blue,
Point@linePoints[[2]]
},
MapThread[Line@*List, {linePoints[[1]], lineFriends}]
},
Boxed -> False
]
Or who knows, maybe you want to get the planes defined by the nearest points between the lines, here's how you'd get that
u1 = Subtract @@ lines[[1]] // Normalize;
u2 = Subtract @@ lines[[2]] // Normalize;
n = Cross[u1, u2] // Normalize;
d = Inverse[{u1, -u2, n}] . (lines[[2, 1]] - lines[[1, 1]]);
mid1 = lines[[1, 1]] + d[[1]]*u1;
mid2 = lines[[2, 1]] + d[[2]]*u2;
planeNormal1 = Cross[u1, mid1 - mid2] // Normalize;
planeNormal2 = Cross[u2, mid2 - mid1] // Normalize;
which we visualize with Hyperplane
- We use parameter
uto represent the position of the lines and usesto join the lines, after that we useParametricPlot3D.
Clear["Global`*"];
{a1, b1} = RandomReal[{-10, 10}, {2, 3}];
{a2, b2} = RandomReal[{-10, 10}, {2, 3}];
f[u_, s_] := {1 - s, s} . {a1 + u*(b1 - a1), a2 + u*(b2 - a2)};
plot = ParametricPlot3D[f[u, s], {u, 0, 1}, {s, 0, 1}]; lines =
Graphics3D[{AbsoluteThickness[5], Red, Line[{a1, b1}], Blue,
Line[{a2, b2}]}];
Show[plot, lines, Boxed -> False,PlotRange -> All]
- Get four random pictures.
Table[({a1, b1} = RandomReal[{-10, 10}, {2, 3}];
{a2, b2} = RandomReal[{-10, 10}, {2, 3}];
f[u_, s_] := {1 - s, s} . {a1 + u*(b1 - a1), a2 + u*(b2 - a2)};
plot = ParametricPlot3D[f[u, s], {u, 0, 1}, {s, 0, 1}];
lines = Graphics3D[{AbsoluteThickness[3], Red, Line[{a1, b1}], Blue,
Line[{a2, b2}]}];
Show[plot, lines, Boxed -> False,PlotRange -> All]), 4]
- To get non-uniform grid, we could add two functions as factors.( two functions join
{0,0}and{1,1}, for exampleλ = #^2 &;μ = Sqrt[#] &;
Clear["Global`*"];
{a1, b1} = RandomReal[{-10, 10}, {2, 3}];
{a2, b2} = RandomReal[{-10, 10}, {2, 3}];
λ = #^2 &;
μ = Sqrt[#] &;
f[u_, s_] := {1 - s, s} . {a1 + λ@u*(b1 - a1),
a2 + μ@u*(b2 - a2)};
plot = ParametricPlot3D[f[u, s], {u, 0, 1}, {s, 0, 1}]; lines =
Graphics3D[{AbsoluteThickness[5], Red, Line[{a1, b1}], Blue,
Line[{a2, b2}]}];
Show[plot, lines, Boxed -> False,PlotRange -> All]
I may only guess what OP wants because of confusing description but when mentioning surface and its equation (not a solid object) then I think OP wanted the following.
lines = RandomReal[{}, {2, 2, 3}];
n = 25;
Graphics3D[{Thin, Opacity[0.5],
Table[Line[#1 + p (#2 - #1) & @@@ lines], {p, 0, 1, 1/n}], Thick,
Riffle[{Red, Blue}, Line /@ lines]}]
Explore related questions
See similar questions with these tags.
line1 = RandomReal[{}, {2, 3}]; line2 = RandomReal[{}, {2, 3}]; ConvexHullMesh[Join[line1, line2]]? $\endgroup$