0
$\begingroup$

I'm trying to roll my own graph visualization, and to do that, I need a 3D plot of a graph.

I have the following code for generating points and edges:

vertices = Table[{RandomReal[], RandomReal[], RandomReal[]}, 20]
edges = Table[{RandomInteger[{1, Length[vertices]}], 
   RandomInteger[{1, Length[vertices]}]}, 40]

This here code runs just fine to display the points:

Graphics3D[
 Map[Point, vertices]
 ]

and this code generates a list of lines:

lines = Map[
  Function[e, Line[
    Part[vertices, Part[e, 1]],
    Part[vertices, Part[e, 2]]]],
  edges
  ]

My question now is this: How do I add the lines to the 3D graphic? I tried

Graphics3D[{
  Map[Point, vertices],
  lines
  }]
$\endgroup$
  • $\begingroup$ You need Line[{pt1, pt2}], not Line[pt1, pt2]. Also, consider this: Graphics3D[ GraphicsComplex[vertices, {Point@Length@vertices, Line@edges}]]. $\endgroup$ – Kuba Jan 10 at 11:52
  • $\begingroup$ Thanks, that worked. $\endgroup$ – Joshua Moore Jan 10 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.