Drawing Petersen graph (Pic)
From LiteratePrograms
The Petersen graph is a graph possessing ten vertices, all of which have degree three. This article explains how to draw a Petersen graph using pic, a small language to create 'box-and-arrow' diagrams.
Contents |
How to convert PIC document to graphic file format
A pic document can be converted to various graphic file format such as EPS and PNG following these steps.
- Write a pic document.
- Convert it to TeX picture commands.
- Embed it into a LaTeX document.
- Compile it to a EPS document.
- Convert EPS document to any graphic file format.
The exact steps are omitted because a detailed explanation is available at Hello World (Pic).
Code
Main source code
Every pic source begins with .PS
and ends with .PE
.
<<petersen.pic>>= .PS init points lines .PE
We initialize several variables, draw ten vertices, and finally connect every vertices.
Initialization
We initialize several system variables that will be used throughout the code. The default unit in pic is inch.
-
circlerad
: the radius of a circle - used for each node in graph -
fillval
: the gray scale used to fill a shape. -
r1
: the distance from the center of a Petersen graph to five inner vertices. -
r2
: the distance from the center of a Petersen graph to five outer vertices. -
pi
: π
<<init>>= circlerad = .12 fillval = .95 r1 = .5 r2 = 2 * r1 pi = atan2(0, -1)
Drawing ten vertices
We will use a rectangular coordinate system to draw ten vertices in a Petersen graph. The drawing process is simply plotting five points in each pentagons.
We choose the center of a Petersen graph as origin for convenience.
<<points>>= O: (0,0)
Then we draw five circles for an outer pentagon.
<<points>>= P0: circle fill "0" at O + (0, r2) P1: circle fill "1" at O + (r2 * cos(9 * pi / 10), r2 * sin(9 * pi / 10)) P2: circle fill "2" at O + (r2 * cos(13 * pi / 10), r2 * sin(13 * pi / 10)) P3: circle fill "3" at O + (r2 * cos(17 * pi / 10), r2 * sin(17 * pi / 10)) P4: circle fill "4" at O + (r2 * cos(pi / 10), r2 * sin(pi / 10))
Finally, we draw five circles for an inner pentagon.
<<points>>= P5: circle fill "5" at O + (0, r1) P8: circle fill "8" at O + (r1 * cos(9 * pi / 10), r1 * sin(9 * pi / 10)) P6: circle fill "6" at O + (r1 * cos(13 * pi / 10), r1 * sin(13 * pi / 10)) P9: circle fill "9" at O + (r1 * cos(17 * pi / 10), r1 * sin(17 * pi / 10)) P7: circle fill "7" at O + (r1 * cos(pi / 10), r1 * sin(pi / 10))
Each circle is named for later use.
Connecting each vertex
The last step is connecting each circle. Thanks to the chop
command, we could easily chop off each line.
First, we draw an outer pentagon connecting every two contiguous vertices.
<<lines>>= line from P0 chop to P1 chop line from P1 chop to P2 chop line from P2 chop to P3 chop line from P3 chop to P4 chop line from P4 chop to P0 chop
Next, we connect each corresponding vertex between two pentagons.
<<lines>>= line from P0 chop to P5 chop line from P1 chop to P8 chop line from P2 chop to P6 chop line from P3 chop to P9 chop line from P4 chop to P7 chop
Finally, we draw a set of star-shaped lines for an inner pentagon.
<<lines>>= line from P5 chop to P6 chop line from P6 chop to P7 chop line from P7 chop to P8 chop line from P8 chop to P9 chop line from P9 chop to P5 chop
Download code |