4
$\begingroup$

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.

asked Aug 24 at 15:29
$\endgroup$
3
  • 2
    $\begingroup$ Maybe line1 = RandomReal[{}, {2, 3}]; line2 = RandomReal[{}, {2, 3}]; ConvexHullMesh[Join[line1, line2]]? $\endgroup$ Commented Aug 24 at 16:28
  • 1
    $\begingroup$ It's unclear exactly you mean by "connecting all points", do you want this to be by nearest distance? Do you want to just connect the end points? Do you want to somehow discretize and just linearly connect by the parametrization? $\endgroup$ Commented Aug 24 at 17:56
  • $\begingroup$ This exactly describes a hyperboloid of one sheet! $\endgroup$ Commented Aug 25 at 2:08

3 Answers 3

5
$\begingroup$

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"
 ]

enter image description here

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
 ]

enter image description here

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
 ]

enter image description here

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

enter image description here

answered Aug 24 at 17:32
$\endgroup$
7
$\begingroup$
  • We use parameter u to represent the position of the lines and use s to join the lines, after that we use ParametricPlot3D.
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]

enter image description here

  • 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]

enter image description here

  • 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]
answered Aug 25 at 1:13
$\endgroup$
5
$\begingroup$

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]}]

enter image description here

answered Aug 24 at 17:57
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.