Skip to content

Commit 87e66f5

Browse files
committed
moved fast star codepath to fast_star, star works as originally designed
1 parent de142da commit 87e66f5

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

nodebox/graphics/context.py

+51-47
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def rect(x, y, width, height, **kwargs):
599599
glVertex2f(x+width, y+height)
600600
glVertex2f(x, y+height)
601601
glEnd()
602-
602+
603603
def triangle(x1, y1, x2, y2, x3, y3, **kwargs):
604604
""" Draws the triangle created by connecting the three given points.
605605
The current stroke, strokewidth and fill color are applied.
@@ -685,57 +685,61 @@ def gcd(a, b):
685685
return gcd(b, a % b) if b else a
686686

687687
_stars = {} #TODO: LRU?
688+
def fast_star(x, y, points=20, outer=100, inner=50, **kwargs):
689+
""" Draws a star with the given points, outer radius and inner radius.
690+
The current stroke, strokewidth and fill color are applied.
691+
"""
692+
scale = gcd(inner, outer)
693+
iscale = inner / scale
694+
oscale = outer / scale
695+
cached = _stars.get((points, iscale, oscale), [])
696+
if not cached:
697+
radii = [oscale, iscale] * int(points+1); radii.pop() # which radius?
698+
f = pi / points
699+
v = [(r*sin(i*f), r*cos(i*f)) for i, r in enumerate(radii)]
700+
cached.append(precompile(lambda:(
701+
glBegin(GL_TRIANGLE_FAN),
702+
glVertex2f(0, 0),
703+
[glVertex2f(vx, vy) for (vx, vy) in v],
704+
glEnd()
705+
)))
706+
cached.append(precompile(lambda:(
707+
glBegin(GL_LINE_LOOP),
708+
[glVertex2f(vx, vy) for (vx, vy) in v],
709+
glEnd()
710+
)))
711+
_stars[(points, iscale, oscale)] = cached
712+
713+
fill, stroke, strokewidth, strokestyle = color_mixin(**kwargs)
714+
for i, clr in enumerate((fill, stroke)):
715+
if clr is not None and (i == 0 or strokewidth > 0):
716+
if i == 1:
717+
glLineWidth(strokewidth)
718+
if strokestyle != _strokestyle:
719+
glLineDash(strokestyle)
720+
glColor4f(clr[0], clr[1], clr[2], clr[3] * _alpha)
721+
glPushMatrix()
722+
glTranslatef(x, y, 0)
723+
glScalef(scale, scale, 1)
724+
glCallList(cached[i])
725+
glPopMatrix()
726+
688727
def star(x, y, points=20, outer=100, inner=50, **kwargs):
689728
""" Draws a star with the given points, outer radius and inner radius.
690729
The current stroke, strokewidth and fill color are applied.
730+
This is about 20x slower than fast_star; use it only if you need the path returned.
691731
"""
692-
if kwargs.get("draw", True):
693-
scale = gcd(inner, outer)
694-
iscale = inner / scale
695-
oscale = outer / scale
696-
cached = _stars.get((points, iscale, oscale), [])
697-
if not cached:
698-
radii = [oscale, iscale] * int(points+1); radii.pop() # which radius?
699-
f = pi / points
700-
v = [(r*sin(i*f), r*cos(i*f)) for i, r in enumerate(radii)]
701-
cached.append(precompile(lambda:(
702-
glBegin(GL_TRIANGLE_FAN),
703-
glVertex2f(0, 0),
704-
[glVertex2f(vx, vy) for (vx, vy) in v],
705-
glEnd()
706-
)))
707-
cached.append(precompile(lambda:(
708-
glBegin(GL_LINE_LOOP),
709-
[glVertex2f(vx, vy) for (vx, vy) in v],
710-
glEnd()
711-
)))
712-
_stars[(points, iscale, oscale)] = cached
732+
p = BezierPath(**kwargs)
733+
p.moveto(x, y+outer)
734+
for i in range(0, int(2*points)+1):
735+
r = (outer, inner)[i%2]
736+
a = pi*i/points
737+
p.lineto(x+r*sin(a), y+r*cos(a))
738+
p.closepath()
713739

714-
fill, stroke, strokewidth, strokestyle = color_mixin(**kwargs)
715-
for i, clr in enumerate((fill, stroke)):
716-
if clr is not None and (i == 0 or strokewidth > 0):
717-
if i == 1:
718-
glLineWidth(strokewidth)
719-
if strokestyle != _strokestyle:
720-
glLineDash(strokestyle)
721-
glColor4f(clr[0], clr[1], clr[2], clr[3] * _alpha)
722-
glPushMatrix()
723-
glTranslatef(x, y, 0)
724-
glScalef(scale, scale, 1)
725-
glCallList(cached[i])
726-
glPopMatrix()
727-
else:
728-
# For whatever reason, the original api specified that you
729-
# can get the path to the star. This is about 30x slower,
730-
# but I'm keeping it here for backwards compatibility.
731-
p = BezierPath(**kwargs)
732-
p.moveto(x, y+outer)
733-
for i in range(0, int(2*points)+1):
734-
r = (outer, inner)[i%2]
735-
a = pi*i/points
736-
p.lineto(x+r*sin(a), y+r*cos(a))
737-
p.closepath()
738-
return p
740+
if kwargs.get("draw", True):
741+
p.draw(**kwargs)
742+
return p
739743

740744
#=====================================================================================================
741745

0 commit comments

Comments
 (0)