1 """Various types of points for vertices etc."""
2
3 from pyx import *
4 from copy import *
5 import math
6
7 from diagrams import FeynDiagram
8 from utils import Visible
9
10
11
13 """Base class for all pointlike objects in Feynman diagrams."""
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 - def draw(self, canvas):
35 "Do nothing (abstract base class)."
36 pass
37
39 "Return the path of the attached blob path, if there is one, otherwise None."
40 if self.getBlob() and hasattr(self.getBlob(), "getPath"):
41 return self.getBlob().getPath()
42 else:
43 return None
44
46 "Return the point midway between this point and the argument."
47 return Point( (self.getX() + otherpoint.getX()) / 2.0,
48 (self.getY() + otherpoint.getY()) / 2.0 )
49
51 "Calculate the distance between this point and the argument."
52 return math.hypot(self.x()-otherpoint.x(), self.y()-otherpoint.y())
53
55 "Return the y-intercept of the straight line defined by this point and the argument."
56 return self.y() - self.tangent(otherpoint) * self.x()
57
58
60 "Return the tangent of the straight line defined by this point and the argument."
61 if otherpoint.x() != self.x():
62 return (otherpoint.y() - self.y()) / (otherpoint.x() - self.x())
63 else:
64 return float(10000)
65
66
67 - def arg(self, otherpoint):
68 """Return the angle between the x-axis and the straight line defined
69 by this point and the argument (cf. complex numbers)."""
70 arg = None
71 if otherpoint.x() == self.x():
72 if otherpoint.y() > self.y():
73 arg = math.pi / 2.0
74 elif otherpoint.y() < self.y():
75 arg = 3 * math.pi / 2.0
76
77 if otherpoint.y() == self.y():
78 if otherpoint.x() < self.x():
79 arg = math.pi
80 else:
81 arg = 0.0
82
83 if otherpoint.x() != self.x() and otherpoint.y() != self.y():
84 arg = math.atan( (otherpoint.y() - self.y()) / (otherpoint.x() - self.x()) )
85 if otherpoint.x() < self.x():
86 arg += math.pi
87 elif otherpoint.y() < self.y():
88 arg += 2 * math.pi
89
90
91 argindegs = math.degrees(arg)
92 return argindegs
93
94
96 "Get the attached blob."
97 return self.blob
98
99
101 "Set the attached blob."
102 self.blob = blob
103 return self
104
105
107 "Return the x-coordinate of this point."
108 return self.xpos
109
111 "Set the x-coordinate of this point."
112 self.xpos = x
113 return self
114
116 "Return the y-coordinate of this point."
117 return self.ypos
118
120 "Set the y-coordinate of this point."
121 self.ypos = y
122 return self
123
125 "Return the x and y coordinates of this point as a 2-tuple."
126 return self.getX(), self.getY()
127
128 - def setXY(self, xpos, ypos):
129 "Set the x and y coordinates of this point."
130 self.setX(float(xpos))
131 self.setY(float(ypos))
132 return self
133
135 "Alias for getX()."
136 return self.getX()
137
139 "Alias for getY()."
140 return self.getY()
141
143 "Alias for getXY()."
144 return self.getXY()
145
146
147
149 "Class for a point drawn with a marker"
150 - def __init__(self, xpos, ypos,
151 mark = None,
152 size = 0.1,
153 fill = [color.rgb.black],
154 stroke = [color.rgb.black],
155 blob = None):
156 self.setXY(xpos, ypos)
157
158 if mark != None:
159 self.marker = NamedMark[mark]
160 self.radius = size
161 else:
162 self.marker = None
163 self.radius = 0
164
165 self.blob = blob
166 self.fillstyles = copy( fill )
167 self.strokestyles = copy( stroke )
168
169 FeynDiagram.currentDiagram.add(self)
170
172 if self.blob:
173 return self.blob.getPath()
174 elif self.marker:
175 return self.marker(self.xpos, self.ypos, self.radius).path()
176 else:
177 return None
178
179 - def mark(self, mark, size=None):
180 self.marker = mark
181 if size is not None:
182 self.radius = size
183 if size is None and self.radius == 0:
184 self.radius = 4*unit.t_pt
185 return self
186
187 - def size(self, size):
188 self.radius = size
189 return self
190
192 return self.fillstyles
193
195 self.fillstyles = styles
196 return self
197
199 self.fillstyles.add(styles)
200 return self
201
203 self.fillstyles.append(style)
204 return self
205
207 return self.strokestyles
208
210 self.strokestyles = styles
211 return self
212
214 self.strokestyles.add(styles)
215 return self
216
218 self.strokestyles.append(style)
219 return self
220
221 - def draw(self, canvas):
222 if self.getPath():
223 canvas.fill(self.getPath(), self.fillstyles)
224 canvas.stroke(self.getPath(), self.strokestyles)
225
226
228 """Vertex is an alias for DecoratedPoint"""
229 pass
230
231
232
233 _square = lambda x, y, r : box.rect(x-r, y-r, 2*r, 2*r)
234
235
236 NamedMark = {"square": _square, "circle": path.circle}
237 MarkedName = {_square: "square", path.circle : "circle"}
238