Package pyfeyn :: Module points
[hide private]
[frames] | no frames]

Source Code for Module pyfeyn.points

  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  ## Point base class 
12 -class Point:
13 """Base class for all pointlike objects in Feynman diagrams."""
14 - def __init__(self, x, y, blob = None):
15 self.setXY(x, y) 16 self.setBlob(blob)
17 18 # def __plus__(self, point = None): 19 # if point: 20 # addx, addy = point.getX(), point.getY() 21 # self.setX(self.getX() + addx) 22 # self.setY(self.getY() + addy) 23 # else: 24 # raise Exception("Tried to add a null x or y component") 25 26 # def __minus__(self, point = None): 27 # if point: 28 # addx, addy = point.getX(), point.getY() 29 # self.setX(self.getX() - addx) 30 # self.setY(self.getY() - addy) 31 # else: 32 # raise Exception("Tried to subtract a null x or y component") 33
34 - def draw(self, canvas):
35 "Do nothing (abstract base class)." 36 pass
37
38 - def getPath(self):
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
45 - def midpoint(self, otherpoint):
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
50 - def distance(self, otherpoint):
51 "Calculate the distance between this point and the argument." 52 return math.hypot(self.x()-otherpoint.x(), self.y()-otherpoint.y())
53
54 - def intercept(self, otherpoint):
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
59 - def tangent(self,otherpoint):
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) ## An arbitrary large number to replace infinity
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 # this will be reset to 0 if the points are the same 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 ## Convert to degrees 91 argindegs = math.degrees(arg) 92 return argindegs
93 94
95 - def getBlob(self):
96 "Get the attached blob." 97 return self.blob
98 99
100 - def setBlob(self, blob):
101 "Set the attached blob." 102 self.blob = blob 103 return self
104 105
106 - def getX(self):
107 "Return the x-coordinate of this point." 108 return self.xpos
109
110 - def setX(self, x):
111 "Set the x-coordinate of this point." 112 self.xpos = x 113 return self
114
115 - def getY(self):
116 "Return the y-coordinate of this point." 117 return self.ypos
118
119 - def setY(self, y):
120 "Set the y-coordinate of this point." 121 self.ypos = y 122 return self
123
124 - def getXY(self):
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
134 - def x(self):
135 "Alias for getX()." 136 return self.getX()
137
138 - def y(self):
139 "Alias for getY()." 140 return self.getY()
141
142 - def xy(self):
143 "Alias for getXY()." 144 return self.getXY()
145 146 147 ## Decorated point class
148 -class DecoratedPoint(Point, Visible):
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 ) # lists are mutable -- 167 self.strokestyles = copy( stroke ) # hence make a copy! 168 ## Add this to the current diagram automatically 169 FeynDiagram.currentDiagram.add(self)
170
171 - def getPath(self):
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: # change shape of a true point? 184 self.radius = 4*unit.t_pt # probably want to use default size 185 return self
186
187 - def size(self, size):
188 self.radius = size 189 return self
190
191 - def getFillstyles(self):
192 return self.fillstyles
193
194 - def setFillstyles(self, styles):
195 self.fillstyles = styles 196 return self
197
198 - def addFillstyles(self, styles):
199 self.fillstyles.add(styles) 200 return self
201
202 - def addFillstyle(self, style):
203 self.fillstyles.append(style) 204 return self
205
206 - def getStrokestyles(self):
207 return self.strokestyles
208
209 - def setStrokestyles(self, styles):
210 self.strokestyles = styles 211 return self
212
213 - def addStrokestyles(self, styles):
214 self.strokestyles.add(styles) 215 return self
216
217 - def addStrokestyle(self, style):
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
227 -class Vertex(DecoratedPoint):
228 """Vertex is an alias for DecoratedPoint""" 229 pass
230 231 232 # A square marker 233 _square = lambda x, y, r : box.rect(x-r, y-r, 2*r, 2*r) 234 235 # A dictionary mapping feynML "mark" choices to marker classes 236 NamedMark = {"square": _square, "circle": path.circle} 237 MarkedName = {_square: "square", path.circle : "circle"} 238