Changeset 76 for nodebox/trunk/src/DrawingPrimitives.py
- Timestamp:
- 02/11/07 20:54:49 (2 years ago)
- Files:
-
- 1 modified
-
nodebox/trunk/src/DrawingPrimitives.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
nodebox/trunk/src/DrawingPrimitives.py
r50 r76 101 101 self.strokecolor = None 102 102 self.strokewidth = 1.0 103 self._segment_cache = None 103 104 104 105 def inheritFromContext(self): … … 139 140 140 141 def moveto(self, x, y): 142 self._segment_cache = None 141 143 self.path.moveToPoint_( (x, y) ) 142 144 143 145 def lineto(self, x, y): 146 self._segment_cache = None 144 147 self.path.lineToPoint_( (x, y) ) 145 148 146 149 def curveto(self, x1, y1, x2, y2, x3, y3): 150 self._segment_cache = None 147 151 self.path.curveToPoint_controlPoint1_controlPoint2_( (x3, y3), (x1, y1), (x2, y2) ) 148 152 149 153 def closepath(self): 154 self._segment_cache = None 150 155 self.path.closePath() 151 156 … … 179 184 180 185 def extend(self, pathElements): 186 self._segment_cache = None 181 187 for el in pathElements: 182 188 self.append(el) 183 189 184 190 def append(self, el): 191 self._segment_cache = None 185 192 if el.cmd == MOVETO: 186 193 self.moveto(el.x, el.y) … … 191 198 elif el.cmd == CLOSE: 192 199 self.closepath() 200 201 def _get_contours(self): 202 import bezier 203 return bezier.contours(self) 204 contours = property(_get_contours) 193 205 194 206 ### Drawing methods ### … … 223 235 self.path.stroke() 224 236 _restore() 237 238 ### Mathematics ### 239 240 def segment_lengths(self, percentual=False, n=10): 241 import bezier 242 if percentual: # Use the opportunity to store the segment cache. 243 if self._segment_cache is None: 244 self._segment_cache = bezier.segment_lengths(self, percentual=True, n=n) 245 return self._segment_cache 246 else: 247 return bezier.segment_lengths(self, percentual=True, n=n) 248 249 def _get_length(self, segmented=False, n=10): 250 import bezier 251 return bezier.length(self, segmented=segmented, n=n) 252 length = property(_get_length) 253 254 def point(self, t): 255 import bezier 256 return bezier.point(self, t) 257 258 def points(self, amount=100): 259 import bezier 260 if len(self) == 0: 261 raise NodeBoxError, "The given path is empty" 262 263 delta = 1.0/amount 264 for i in xrange(amount): 265 yield self.point(delta*i) 225 266 226 267 class PathElement(object): … … 246 287 elif cmd == CLOSE: 247 288 assert pts is None or len(pts) == 0 289 290 def __repr__(self): 291 if self.cmd == MOVETO: 292 return "PathElement(MOVETO, ((%s, %s),))" % (self.x, self.y) 293 elif self.cmd == LINETO: 294 return "PathElement(LINETO, ((%s, %s),))" % (self.x, self.y) 295 elif self.cmd == CURVETO: 296 return "PathElement(LINETO, ((%s, %s), (%s, %s), (%s, %s))" % \ 297 (self.ctrl1.x, self.ctrl1.y, self.ctrl2.x, self.ctrl2.y, self.x, self.y) 298 elif self.cmd == CLOSE: 299 return "PathElement(CLOSE)" 248 300 249 301 class Rect(BezierPath): … … 897 949 self.height = height 898 950 self.speed = None 899 print "canvas init"900 951 self.mousedown = False 901 952 … … 1212 1263 self._autoclosepath = close 1213 1264 1265 def findpath(self, points, curvature=1.0): 1266 import bezier 1267 path = bezier.findpath(points, curvature=curvature) 1268 path._ctx = self 1269 path.inheritFromContext() 1270 return path 1271 1214 1272 ### Clipping Commands ### 1215 1273 def beginclip(self, path): … … 1423 1481 k = KantGenerator(sourceFile) 1424 1482 return k.output() 1483 1484 def _test(): 1485 import doctest, DrawingPrimitives 1486 return doctest.testmod(DrawingPrimitives) 1487 1488 if __name__=='__main__': 1489 #import profile 1490 #profile.run(" 1491 1492 _test()
