First Page
News
Forum
In a Nutshell
About OGDF
FAQs
Key Features
Publications
Documentation
Basic Data Structures
Graph Classes
How-Tos
Developer Resources
Reference Documentation
Get OGDF
System Requirements
Download
Installation (Linux/Mac)
Installation (Windows)
Team & Contact
Imprint
Create a graph and its layout manually.
#include <ogdf/basic/Graph.h> #include <ogdf/basic/GraphAttributes.h> using namespace ogdf; int main() { Graph G; GraphAttributes GA(G, GraphAttributes::nodeGraphics | GraphAttributes::edgeGraphics ); const int LEN = 11; for(int i = 1; i<LEN; ++i) { node left = G.newNode(); GA.x(left) = -5*(i+1); GA.y(left) = -20*i; GA.width(left) = 10*(i+1); GA.height(left) = 15; node bottom = G.newNode(); GA.x(bottom) = 20*(LEN-i); GA.y(bottom) = 5*(LEN+1-i); GA.width(bottom) = 15; GA.height(bottom) = 10*(LEN+1-i); edge e = G.newEdge(left,bottom); DPolyline &p = GA.bends(e); p.pushBack(DPoint(10,-20*i)); p.pushBack(DPoint(20*(LEN-i),-10)); } GA.writeGML("manual_graph.gml"); return 0; }
We first declare a Graph G. The class Graph only stored structural data of a graphs, i.e., edges and nodes, but not attrobutes like x/y positions, etc. Such information can be maintained in GraphAttributes, of which we generate an instance GA, associated to Graph G. The constructor of GraphAttributes allows to select which attributes shall be stored. We will use node position and sizes (nodeGraphics) and bend points for edges (edgeGraphics).
Within the loop, we generate 2 nodes per iteration, placing the first on the left-hand side of the drawing (from bottom to top), and the second node on the bottom of the drawing (from right to left). Note that in OGDF the coordinate systems’ origin is in the top-left corner, hence the coordinated increase towards the right and towards the bottom. Furthermore, we give the nodes differing sizes, and note that the x/y coordinates of a node are specifying that node’s center.
We also see that, as a general rule in GraphAttributes, we always obtain references to the different entries, hence we can get and set an attribute using virtually the same function (only differentiated by the use of const, which is transarent to the user).
Finally, we add an edge in each iteration of the loop, and set bend points for that edge.
In the end, GA.writeGML(”manual_graph.gml”) writes all information saved in G and GA to file “manual_graph.gml”.
