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

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

""" 

Plotting primitives 

""" 

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

# Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>, 

# William Stein <wstein@gmail.com>, 

# 2008 Mike Hansen <mhansen@gmail.com>, 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from sage.misc.fast_methods import WithEqualityById 

from sage.structure.sage_object import SageObject 

from sage.misc.misc import verbose 

 

class GraphicPrimitive(WithEqualityById, SageObject): 

""" 

Base class for graphics primitives, e.g., things that knows how to draw 

themselves in 2D. 

 

EXAMPLES: 

 

We create an object that derives from GraphicPrimitive:: 

 

sage: P = line([(-1,-2), (3,5)]) 

sage: P[0] 

Line defined by 2 points 

sage: type(P[0]) 

<class 'sage.plot.line.Line'> 

 

TESTS:: 

 

sage: hash(circle((0,0),1)) # random 

42 

""" 

def __init__(self, options): 

""" 

Create a base class GraphicsPrimitive. All this does is 

set the options. 

 

EXAMPLES: 

 

We indirectly test this function:: 

 

sage: from sage.plot.primitive import GraphicPrimitive 

sage: GraphicPrimitive({}) 

Graphics primitive 

""" 

self._options = options 

 

 

def _allowed_options(self): 

""" 

Return the allowed options for a graphics primitive. 

 

OUTPUT: 

 

- a reference to a dictionary. 

 

EXAMPLES:: 

 

sage: from sage.plot.primitive import GraphicPrimitive 

sage: GraphicPrimitive({})._allowed_options() 

{} 

""" 

return {} 

 

def plot3d(self, **kwds): 

""" 

Plots 3D version of 2D graphics object. Not implemented 

for base class. 

 

EXAMPLES:: 

 

sage: from sage.plot.primitive import GraphicPrimitive 

sage: G=GraphicPrimitive({}) 

sage: G.plot3d() 

Traceback (most recent call last): 

... 

NotImplementedError: 3D plotting not implemented for Graphics primitive 

""" 

raise NotImplementedError("3D plotting not implemented for %s" % self._repr_()) 

 

def _plot3d_options(self, options=None): 

""" 

Translate 2D plot options into 3D plot options. 

 

EXAMPLES:: 

 

sage: P = line([(-1,-2), (3,5)], alpha=.5, thickness=4) 

sage: p = P[0]; p 

Line defined by 2 points 

sage: q=p.plot3d() 

sage: q.thickness 

4 

sage: q.texture.opacity 

0.5 

""" 

if options is None: 

options = self.options() 

options_3d = {} 

if 'rgbcolor' in options: 

options_3d['rgbcolor'] = options['rgbcolor'] 

del options['rgbcolor'] 

if 'alpha' in options: 

options_3d['opacity'] = options['alpha'] 

del options['alpha'] 

 

for o in ('legend_color', 'legend_label', 'zorder'): 

if o in options: 

del options[o] 

 

if len(options) != 0: 

raise NotImplementedError("Unknown plot3d equivalent for {}".format( 

", ".join(options.keys()))) 

return options_3d 

 

def set_zorder(self, zorder): 

""" 

Set the layer in which to draw the object. 

 

EXAMPLES:: 

 

sage: P = line([(-2,-3), (3,4)], thickness=4) 

sage: p=P[0] 

sage: p.set_zorder(2) 

sage: p.options()['zorder'] 

2 

sage: Q = line([(-2,-4), (3,5)], thickness=4,zorder=1,hue=.5) 

sage: P+Q # blue line on top 

Graphics object consisting of 2 graphics primitives 

sage: q=Q[0] 

sage: q.set_zorder(3) 

sage: P+Q # teal line on top 

Graphics object consisting of 2 graphics primitives 

sage: q.options()['zorder'] 

3 

""" 

self._options['zorder'] = zorder 

 

def set_options(self, new_options): 

""" 

Change the options to `new_options`. 

 

EXAMPLES:: 

 

sage: from sage.plot.circle import Circle 

sage: c = Circle(0,0,1,{}) 

sage: c.set_options({'thickness': 0.6}) 

sage: c.options() 

{'thickness': 0.6...} 

""" 

if new_options is not None: self._options = new_options 

 

def options(self): 

""" 

Return the dictionary of options for this graphics primitive. 

 

By default this function verifies that the options are all 

valid; if any aren't, then a verbose message is printed with level 0. 

 

EXAMPLES:: 

 

sage: from sage.plot.primitive import GraphicPrimitive 

sage: GraphicPrimitive({}).options() 

{} 

""" 

from sage.plot.graphics import do_verify 

from sage.plot.colors import hue 

O = dict(self._options) 

if do_verify: 

A = self._allowed_options() 

t = False 

K = list(A) + ['xmin', 'xmax', 'ymin', 'ymax', 'axes'] 

for k in O.keys(): 

if not k in K: 

do_verify = False 

verbose("WARNING: Ignoring option '%s'=%s"%(k,O[k]), level=0) 

t = True 

if t: 

s = "\nThe allowed options for %s are:\n"%self 

K.sort() 

for k in K: 

if k in A: 

s += " %-15s%-60s\n"%(k,A[k]) 

verbose(s, level=0) 

 

 

if 'hue' in O: 

t = O['hue'] 

if not isinstance(t, (tuple,list)): 

t = [t,1,1] 

O['rgbcolor'] = hue(*t) 

del O['hue'] 

return O 

 

def _repr_(self): 

""" 

String representation of this graphics primitive. 

 

EXAMPLES:: 

 

sage: from sage.plot.primitive import GraphicPrimitive 

sage: GraphicPrimitive({})._repr_() 

'Graphics primitive' 

""" 

return "Graphics primitive" 

 

 

 

class GraphicPrimitive_xydata(GraphicPrimitive): 

def get_minmax_data(self): 

""" 

Returns a dictionary with the bounding box data. 

 

EXAMPLES:: 

 

sage: d = polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))[0].get_minmax_data() 

sage: d['ymin'] 

0.0 

sage: d['xmin'] 

1.0 

 

:: 

 

sage: d = point((3, 3), rgbcolor=hue(0.75))[0].get_minmax_data() 

sage: d['xmin'] 

3.0 

sage: d['ymin'] 

3.0 

 

:: 

 

sage: l = line([(100, 100), (120, 120)])[0] 

sage: d = l.get_minmax_data() 

sage: d['xmin'] 

100.0 

sage: d['xmax'] 

120.0 

 

""" 

from sage.plot.plot import minmax_data 

return minmax_data(self.xdata, self.ydata, dict=True)