Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active October 10, 2022 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/973b7e7af678c6a250c56486f85814c2 to your computer and use it in GitHub Desktop.
Save Hermann-SW/973b7e7af678c6a250c56486f85814c2 to your computer and use it in GitHub Desktop.
PostScript file with /vertex and /poly definitions useful for drawing graphs, with demo
%!
0 setlinewidth
/Times-Roman findfont 12 scalefont setfont
newpath 8 99 moveto 601 99 lineto 601 692 lineto 8 692 lineto closepath stroke
/vertex { 2 copy 5 4 roll dup 4 1 roll
newpath 0 360 arc closepath
gsave 1.0 setgray fill grestore
stroke
12 ge {
newpath moveto
gsave dup false charpath flattenpath pathbbox grestore
exch 4 1 roll exch sub -.5 mul 3 1 roll sub -.5 mul exch
rmoveto show
} { pop pop pop } ifelse
} def
/txt {
newpath moveto
gsave dup false charpath flattenpath pathbbox grestore
exch 4 1 roll exch sub -.5 mul 3 1 roll sub -.5 mul exch
rmoveto show
} def
/txtdistdeg {
newpath moveto
gsave dup false charpath flattenpath pathbbox grestore
exch 4 1 roll exch sub -.5 mul 3 1 roll sub -.5 mul exch
5 3 roll gsave
rotate 0 exch rmoveto
rmoveto show
grestore
} def
/poly {
newpath
moveto
{
count 0 eq { exit } if
lineto
} loop
} def
%% http://computer-programming-forum.com/36-postscript/3d1b79b93a578811.htm
/arrow %% angle /arrow -- draws arrowhead
{
gsave
currentpoint translate 90 sub rotate .5 .5 scale
newpath
0.1 setlinewidth
0 -15 moveto
-6 -18 lineto
-4 -15 -1 -9 0 0 curveto
1 -9 4 -15 6 -18 curveto
0 -15 lineto
fill
grestore
} def
/arrowto %% x1 y1 arrowto -- draws line with arrowhead
{
2 copy % x1 y1 x1 y1
currentpoint % x1 y1 x1 y1 x0 y0
3 -1 roll exch % x1 y1 x1 x0 y1 y0
sub % x1 y1 x1 x0 y1-y0
3 1 roll % x1 y1 y1-y0 x1 x0
sub % x1 y1 y1-y0 x1-x0
atan % x1 y1 theta
3 1 roll % theta x1 y1
2 copy lineto % theta x1 y1
2 copy stroke moveto % theta x1 y1
3 -1 roll arrow % x1 y1
moveto
} def
/rarrowto { %% dx dy rarrowto --
exch currentpoint % dy dx x0 y0
4 1 roll % y0 dy dx x0
add 3 1 roll % x0+dx y0 dy
add arrowto
} def
%% len dist cr cg cb angle x1 y1
/parrow {
gsave
newpath translate 0 0 moveto
rotate
setrgbcolor
0 exch rmoveto
0 rarrowto
grestore
} def
.75 setgray 300 400 200 400 200 300 275 325 poly fill
0 setgray 300 400 200 400 200 300 275 325 poly closepath stroke
(89) 18 300 400 vertex
(123) 12 200 400 vertex
(3) 6 200 300 vertex
(55) 9 275 325 vertex
(txt) 350 250 txt
() 0.3 390 270 vertex
12 60 (60 deg) 390 270 txtdistdeg
12 240 (240 deg) 390 270 txtdistdeg
() 0.3 380 350 vertex
30 6 0 1 0 60 380 350 parrow
30 6 0 0 1 240 380 350 parrow
showpage
@Hermann-SW
Copy link
Author

Added color arrow vectors (parallel to undirected graph edge), based on 2000 public domain arrow code (bottom):
http://computer-programming-forum.com/36-postscript/3d1b79b93a578811.htm

%% len dist cr cg cb angle x1 y1
/parrow {
...
}
...
() 0.3 380 350 vertex
30 6 0 1 0 60 380 350 parrow
30 6 0 0 1 240 380 350 parrow

@Hermann-SW
Copy link
Author

Hermann-SW commented Oct 10, 2022

Not part of ps_done.ps anymore, but nice automatic "arrow at vertex" can be generated from code by NodeJS "vertex_indicator()" function
https://github.com/Hermann-SW/planar_graph_playground/blob/main/ps.js#L250-L289
to visualize "traversing a face":

https://forums.raspberrypi.com/viewtopic.php?t=333342&start=25#p2010496

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment