def peekAhead(lst):
"""
Look ahead one in a list:
>>> l=[(1,2),(3,4),(5,6)]
>>> for i in peek(l): print i
...
((1, 2), (3, 4))
((3, 4), (5, 6))
((5, 6), None)
"""
i = iter(lst)
first = prev = i.next()
for nxt in i:
yield prev, nxt
prev = nxt
yield nxt,
def thick_path(self, color, thickness):
"""
Uses cairo to get the points along a curve.
Then uses voodoo to get a series of x,y points at 'thickness'
away from that curve -- going around it.
Then uses cogl to draw and fill that shape.
Used for pick testing.
"""
cb=self.cb
## Prep a temporary cairo surface to draw the curve
tmpsurface = cairo.ImageSurface (cairo.FORMAT_ARGB32, self.w, self.h)
context = cairo.Context (tmpsurface)
context.set_source_rgb( 1,1,1 )
context.move_to(cb.sx, cb.sy)
context.rel_curve_to( cb.c1x,cb.c1y, cb.c2x,cb.c2y, cb.ex,cb.ey)
## Steal the path data
self.path_copy = context.copy_path_flat()
## Now the voodoo
nodes=[]
node={}
pc = [n[1] for n in self.path_copy] #strip the first value, and make a list.
def makenode( twotup ):
## twotup = [ (0.0, 0.0), (1.1875, 0.26171875) ]
## ends with twotup = [(blah,...)]
node = twotup[0]
if len(twotup) > 1:
lookahead = twotup[1]
dx=node[0]-lookahead[0]
dy=node[1]-lookahead[1]
else:
dx,dy=1,1 # a little vague on this last distance here.
return {"x":node[0], "y":node[1], "dx":dx,"dy":dy }
## Go through my list and make node dict objects:
for twotup in peekAhead( pc ):
nodes.append(makenode(twotup))
context.stroke()
l1=[]
l2=[]
for node in nodes:
## algorithm from stackoverflow! Something to do with normals..
dx,dy=node["dx"], node["dy"]
dist = sqrt(node["dx"]*node["dx"] + node["dy"]*node["dy"])
dx /= dist
dy /= dist
x3 = node["x"] + (thickness/2)*dy
y3 = node["y"] - (thickness/2)*dx
x4 = node["x"] - (thickness/2)*dy
y4 = node["y"] + (thickness/2)*dx
l1.append((x3,y3))
l2.append((x4,y4))
## We now have two lists of x,y points 'thickness' away from the curve
## on both sides.
l2.reverse() # x4 y4 points must run back along the line
l1.extend(l2) # glue them to x3 y3 points.
## Now draw this list with cogl. (OR CAIRO)
## it ain't pretty on the caps. I suppose some kind of arc
## could be employed.
cogl.set_source_color( color )
cogl.path_move_to(cb.sx, cb.sy)
for tup in l1:
x=tup[0]
y=tup[1]
cogl.path_line_to(x,y)
cogl.path_fill()