Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/print_graphs.py
4056 views
1
##
2
## print-graphs.sage
3
##
4
## Craig Citro
5
## 07_06_12
6
##
7
## Copying old scheme print-graph methods over to Sage
8
##
9
10
#*****************************************************************************
11
# Copyright (C) 2007 Craig Citro
12
#
13
# Distributed under the terms of the GNU General Public License (GPL)
14
# http://www.gnu.org/licenses/
15
#*****************************************************************************
16
17
18
import time
19
from math import floor
20
from copy import copy
21
22
def print_header_ps(s):
23
"""
24
Give the header for a postscript file.
25
26
EXAMPLE:
27
sage: from sage.graphs.print_graphs import print_header_ps
28
sage: print print_header_ps('')
29
%% --- Auto-generated PostScript ---
30
%% Generated on:
31
%%...
32
33
"""
34
s += "%% --- Auto-generated PostScript ---\n\n\n"
35
s += "%% Generated on: \n"
36
s += "%%" + time.asctime() + "\n"
37
return s
38
39
def print_header_eps(s, xmin, ymin, xmax, ymax):
40
"""
41
Give the header for an encapsulated postscript file.
42
43
EXAMPLE:
44
sage: from sage.graphs.print_graphs import print_header_eps
45
sage: print print_header_eps('',0,0,1,1)
46
%!PS-Adobe-3.0 EPSF-3.0
47
%%BoundingBox: 0 0 1 1
48
49
"""
50
51
s += "%!PS-Adobe-3.0 EPSF-3.0\n"
52
s += "%" + "%" + "BoundingBox: %s %s %s %s \n"%(xmin, ymin, xmax, ymax)
53
54
return s
55
56
def print_functions(s):
57
"""
58
Define edge and point drawing functions.
59
60
EXAMPLE:
61
sage: from sage.graphs.print_graphs import print_functions
62
sage: print print_functions('')
63
/point %% input: x y
64
{ moveto
65
gsave
66
currentpoint translate
67
0 0 2 0 360 arc
68
fill
69
grestore
70
} def
71
/edge %% input: x1 y1 x2 y2
72
{ moveto
73
lineto
74
stroke
75
} def
76
77
"""
78
s += "/point %% input: x y\n"
79
s += "{ moveto\n"
80
s += " gsave\n"
81
s += " currentpoint translate\n"
82
s += " 0 0 2 0 360 arc\n"
83
s += " fill\n"
84
s += " grestore\n"
85
s += " } def\n\n\n"
86
s += "/edge %% input: x1 y1 x2 y2\n"
87
s += "{ moveto\n"
88
s += " lineto\n"
89
s += " stroke\n"
90
s += " } def\n\n"
91
92
return s
93
94
def print_graph_ps(vert_ls, edge_iter, pos_dict):
95
"""
96
Give postscript text for drawing a graph.
97
98
EXAMPLE:
99
sage: from sage.graphs.print_graphs import print_graph_ps
100
sage: P = graphs.PetersenGraph()
101
sage: print print_graph_ps(P.vertices(), P.edges(), sage.graphs.generic_graph_pyx.spring_layout_fast(P))
102
%% --- Auto-generated PostScript ---
103
%% Generated on:
104
%%...
105
/point %% input: x y
106
{ moveto
107
gsave
108
currentpoint translate
109
0 0 2 0 360 arc
110
fill
111
grestore
112
} def
113
/edge %% input: x1 y1 x2 y2
114
{ moveto
115
lineto
116
stroke
117
} def
118
... point
119
...
120
... point
121
... edge
122
...
123
... edge
124
125
"""
126
127
pos_dict = copy(pos_dict) # assumption: all pos's are -1 <= ... <= 1
128
129
s = ""
130
131
s = print_header_ps(s)
132
s = print_functions(s)
133
134
for v in vert_ls:
135
x,y = pos_dict[v]
136
pos_dict[v] = int(floor(50*x))+50, int(floor(50*y))+50
137
x,y = pos_dict[v]
138
s += "%s %s point\n"%(x,y)
139
140
for (u, v, l) in edge_iter:
141
ux, uy = pos_dict[u]
142
vx, vy = pos_dict[v]
143
s += "%s %s %s %s edge\n"%(ux, uy, vx, vy)
144
145
return s
146
147
def print_graph_eps(vert_ls, edge_iter, pos_dict):
148
"""
149
Give postscript text for drawing a graph.
150
151
EXAMPLE:
152
sage: from sage.graphs.print_graphs import print_graph_eps
153
sage: P = graphs.PetersenGraph()
154
sage: print print_graph_eps(P.vertices(), P.edges(), sage.graphs.generic_graph_pyx.spring_layout_fast(P))
155
%!PS-Adobe-3.0 EPSF-3.0
156
%%BoundingBox: 0 0 100 100
157
/point %% input: x y
158
{ moveto
159
gsave
160
currentpoint translate
161
0 0 2 0 360 arc
162
fill
163
grestore
164
} def
165
/edge %% input: x1 y1 x2 y2
166
{ moveto
167
lineto
168
stroke
169
} def
170
... point
171
...
172
... point
173
... edge
174
...
175
... edge
176
177
"""
178
179
pos_dict = copy(pos_dict) # assumption: all pos's are -1 <= ... <= 1
180
181
t = ""
182
s = ""
183
184
xmin = 0
185
ymin = 0
186
187
xmax = -1
188
ymax = -1
189
190
# n = len(vert_ls)
191
192
for v in vert_ls:
193
x,y = pos_dict[v]
194
pos_dict[v] = int(floor(50*x))+50, int(floor(50*y))+50
195
x,y = pos_dict[v]
196
s += "%s %s point\n"%(x,y)
197
198
for (u, v, l) in edge_iter:
199
ux, uy = pos_dict[u]
200
vx, vy = pos_dict[v]
201
s += "%s %s %s %s edge\n"%(ux, uy, vx, vy)
202
203
t = print_header_eps(t, 0, 0, 100, 100)
204
t = print_functions(t)
205
206
return t + s
207
208