Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Created February 3, 2014 06:21
Show Gist options
  • Save jarek-foksa/8779579 to your computer and use it in GitHub Desktop.
Save jarek-foksa/8779579 to your computer and use it in GitHub Desktop.
# http://svg-whiz.com/svg/starPrimative.svg
# Smoothing: http://www.codeproject.com/Articles/128705/WPF-rounded-corners-polygon
drawStar: (cx, cy, r1, r2, rCorner, arms, shift = 0) ->
polyPoints = null
segments = []
if arms in [1,3,5,7,9]
orient = 'point'
else
orient = 'edge'
for s in [0..arms]
# Segment A
angle = 2.0 * pi * (s / arms)
if orient == 'point'
angle -= pi/2
else
angle = (angle + (pi/arms)) - (pi/2)
x = (r1 * cos(angle)) + cx
y = (r1 * sin(angle)) + cy
if s == 0
polyPoints = "M #{x} #{y}"
else
polyPoints += " L #{x} #{y}"
# Segment B
angle = (2.0 * pi * (s / arms)) + (pi/arms)
if orient == 'point'
angle -= pi/2
else
angle = (angle + (pi/arms)) - (pi/2)
angle += shift
x = (r2 * cos(angle)) + cx
y = (r2 * sin(angle)) + cy
polyPoints += " L #{x} #{y}"
$star = svg 'path', canvas.$drawing
$star.setAttribute 'boxy:shape', 'star'
$star.setAttribute 'boxy:cx', cx
$star.setAttribute 'boxy:cy', cy
$star.setAttribute 'boxy:r1', r1
$star.setAttribute 'boxy:r2', r2
$star.setAttributeNS null, 'd', polyPoints
$star.setAttributeNS null, 'fill', 'blue'
$star.setAttributeNS(null, 'stroke', 'red')
$star.setAttributeNS(null, 'stroke-width', '2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment