Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

## 

## print-graphs.sage 

## 

## Craig Citro 

## 07_06_12 

## 

## Copying old scheme print-graph methods over to Sage 

## 

 

#***************************************************************************** 

# Copyright (C) 2007 Craig Citro 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

 

import time 

from math import floor 

from copy import copy 

 

def print_header_ps(s): 

""" 

Give the header for a postscript file. 

 

EXAMPLES:: 

 

sage: from sage.graphs.print_graphs import print_header_ps 

sage: print(print_header_ps('')) 

%% --- Auto-generated PostScript --- 

%% Generated on: 

%%... 

 

""" 

s += "%% --- Auto-generated PostScript ---\n\n\n" 

s += "%% Generated on: \n" 

s += "%%" + time.asctime() + "\n" 

return s 

 

def print_header_eps(s, xmin, ymin, xmax, ymax): 

""" 

Give the header for an encapsulated postscript file. 

 

EXAMPLES:: 

 

sage: from sage.graphs.print_graphs import print_header_eps 

sage: print(print_header_eps('',0,0,1,1)) 

%!PS-Adobe-3.0 EPSF-3.0 

%%BoundingBox: 0 0 1 1 

 

""" 

 

s += "%!PS-Adobe-3.0 EPSF-3.0\n" 

s += "%" + "%" + "BoundingBox: %s %s %s %s \n"%(xmin, ymin, xmax, ymax) 

 

return s 

 

def print_functions(s): 

""" 

Define edge and point drawing functions. 

 

EXAMPLES:: 

 

sage: from sage.graphs.print_graphs import print_functions 

sage: print(print_functions('')) 

/point %% input: x y 

{ moveto 

gsave 

currentpoint translate 

0 0 2 0 360 arc 

fill 

grestore 

} def 

/edge %% input: x1 y1 x2 y2 

{ moveto 

lineto 

stroke 

} def 

 

""" 

s += "/point %% input: x y\n" 

s += "{ moveto\n" 

s += " gsave\n" 

s += " currentpoint translate\n" 

s += " 0 0 2 0 360 arc\n" 

s += " fill\n" 

s += " grestore\n" 

s += " } def\n\n\n" 

s += "/edge %% input: x1 y1 x2 y2\n" 

s += "{ moveto\n" 

s += " lineto\n" 

s += " stroke\n" 

s += " } def\n\n" 

 

return s 

 

def print_graph_ps(vert_ls, edge_iter, pos_dict): 

""" 

Give postscript text for drawing a graph. 

 

EXAMPLES:: 

 

sage: from sage.graphs.print_graphs import print_graph_ps 

sage: P = graphs.PetersenGraph() 

sage: print(print_graph_ps(P.vertices(), P.edges(), sage.graphs.generic_graph_pyx.spring_layout_fast(P))) 

%% --- Auto-generated PostScript --- 

%% Generated on: 

%%... 

/point %% input: x y 

{ moveto 

gsave 

currentpoint translate 

0 0 2 0 360 arc 

fill 

grestore 

} def 

/edge %% input: x1 y1 x2 y2 

{ moveto 

lineto 

stroke 

} def 

... point 

... 

... point 

... edge 

... 

... edge 

""" 

 

pos_dict = copy(pos_dict) # assumption: all pos's are -1 <= ... <= 1 

 

s = "" 

 

s = print_header_ps(s) 

s = print_functions(s) 

 

for v in vert_ls: 

x,y = pos_dict[v] 

pos_dict[v] = int(floor(50*x))+50, int(floor(50*y))+50 

x,y = pos_dict[v] 

s += "%s %s point\n"%(x,y) 

 

for (u, v, l) in edge_iter: 

ux, uy = pos_dict[u] 

vx, vy = pos_dict[v] 

s += "%s %s %s %s edge\n"%(ux, uy, vx, vy) 

 

return s 

 

def print_graph_eps(vert_ls, edge_iter, pos_dict): 

""" 

Give postscript text for drawing a graph. 

 

EXAMPLES:: 

 

sage: from sage.graphs.print_graphs import print_graph_eps 

sage: P = graphs.PetersenGraph() 

sage: print(print_graph_eps(P.vertices(), P.edges(), sage.graphs.generic_graph_pyx.spring_layout_fast(P))) 

%!PS-Adobe-3.0 EPSF-3.0 

%%BoundingBox: 0 0 100 100 

/point %% input: x y 

{ moveto 

gsave 

currentpoint translate 

0 0 2 0 360 arc 

fill 

grestore 

} def 

/edge %% input: x1 y1 x2 y2 

{ moveto 

lineto 

stroke 

} def 

... point 

... 

... point 

... edge 

... 

... edge 

 

""" 

 

pos_dict = copy(pos_dict) # assumption: all pos's are -1 <= ... <= 1 

 

t = "" 

s = "" 

 

xmin = 0 

ymin = 0 

 

xmax = -1 

ymax = -1 

 

# n = len(vert_ls) 

 

for v in vert_ls: 

x,y = pos_dict[v] 

pos_dict[v] = int(floor(50*x))+50, int(floor(50*y))+50 

x,y = pos_dict[v] 

s += "%s %s point\n"%(x,y) 

 

for (u, v, l) in edge_iter: 

ux, uy = pos_dict[u] 

vx, vy = pos_dict[v] 

s += "%s %s %s %s edge\n"%(ux, uy, vx, vy) 

 

t = print_header_eps(t, 0, 0, 100, 100) 

t = print_functions(t) 

 

return t + s