Algorithmic Solutions > LEDA > LEDA Guide > Graph Algorithms > Graph Drawing Algorithms > Spring Embedding > Example 3D

Example 3D Spring Embedding

Let G=(V,E) be a graph. D3_SPRING_EMBEDDING() computes a Spring Embedding of G in 3D. The following program shows how 3D_SPRING_EMBEDDING() can be used.

We first generate a 3D grid graph G with 3*3*3=27 nodes and 54 edges.

Then we define node_array<int> xpos, ypos, zpos for the x-, y-, and z-coordinates of the nodes.

We place the nodes randomly in the space defined by xmin, xmax, ymin, ymax, zmin, and zmax and call D3_SPRING_EMBEDDING().

We use d3_window to visualize the result. First we define a window as a basis for . After transforming the x-, y-, and z-coordinates of the nodes we define the d3_window and display the result of D3_SPRING_EMBEDDING().

#include <LEDA/graph/graph.h>
#include <LEDA/graph/graph_alg.h>
#include <LEDA/graphics/window.h>
#include <LEDA/graphics/d3_window.h>

using namespace leda;

int main()
{ 
  graph G; 
  d3_grid_graph(G,3);

  node_array< 
  double> xpos(G,0), ypos(G,0), zpos(G,0);
  
  double xmin=-180, xmax=180,
         ymin=-180, ymax=180,
         zmin=-180, zmax=360;
  int lb=static_cast<int>(xmin);
  int ub=static_cast<int>(xmax);
  random_source S(lb,ub);node v;
  
  int x; forall_nodes(v,G) {S >> x;xpos[v]=x;}
  lb=static_cast<int>(ymin);
  ub=static_cast<int>(ymax);
  S.set_range(lb,ub);
  
  int y; forall_nodes(v,G) {S >> y;ypos[v]=y;}
  lb=static_cast<int>(zmin);
  ub=static_cast<int>(zmax);
  S.set_range(lb,ub);
  int z; forall_nodes(v,G) {S >> z;zpos[v]=z;}

  D3_SPRING_EMBEDDING
    (G,xpos,ypos,zpos,xmin,xmax,ymin,ymax,zmin,zmax);

  int width=500, height=500;
  window W(width,height,"D3 Window");
  W.display();   
  W.init(xmin,xmax,ymin); 

  node_array<vector> pos(G);
  forall_nodes(v,G) {pos[v] = vector(xpos[v],ypos[v],zpos[v]);}  
   
  d3_window d3_win(W,G,pos);
  d3_win.set_draw_graph(true);
  d3_win.set_speed(16);
  d3_win.set_message
    ("\\bf\\blue left:  \\black zoom up   ~~~~~\
     \\bf\\blue middle:\\black zoom down ~~~~~\
     \\bf\\blue right: \\black exit");
  d3_win.set_node_color(red);
  d3_win.draw();     
  int but = 0;   
  while (but != MOUSE_BUTTON(3)) but = d3_win.move();
       
  return 0;
}

See also:

Spring Embedding

Node Arrays

d3_window

Windows and Panels


Graph Drawing Algorithms


Graph Algorithms

Graphs and Related Data Types


Manual Entries:

Manual Page Graph Drawing Algorithms

 



Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH