Last active
January 1, 2016 16:29
-
-
Save kaedroho/8170604 to your computer and use it in GitHub Desktop.
Sawblade generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fractions import Fraction | |
import math | |
def circle_point(angle, diameter): | |
return ( | |
math.sin(angle * math.pi * 2) * diameter, | |
math.cos(angle * math.pi * 2) * diameter, | |
) | |
def svg_moveto(f, point): | |
f.write('M' + str(point[0]) + ',' + str(point[1]) + ' ') | |
def svg_curve(file, point, control_point): | |
f.write('Q' + str(control_point[0]) + ',' + str(control_point[1]) + ' ' + str(point[0]) + ',' + str(point[1]) + ' ') | |
def draw_sawblade(f, teeth, inside_diameter, outside_diameter): | |
f.write('<path d="') | |
for tooth in range(teeth): | |
angle_start = Fraction(tooth, teeth) | |
angle_end = Fraction(tooth + 1, teeth) | |
angle_mid = (angle_start + angle_end) / 2 | |
angle_threequater = (angle_mid + angle_end) / 2 | |
point_start = circle_point(angle_start, inside_diameter) | |
point_tip = circle_point(angle_end, outside_diameter) | |
control_back = circle_point(angle_mid, outside_diameter) | |
point_end = circle_point(angle_end, inside_diameter) | |
control_front = circle_point(angle_threequater, (inside_diameter + outside_diameter) / 2) | |
if tooth == 0: | |
svg_moveto(f, point_start) | |
svg_curve(f, point_tip, control_back) | |
svg_curve(f, point_end, control_front) | |
f.write('Z" style="stroke:#000000; fill:#CCCCCC;"/>') | |
with open('sawblade.svg', 'w') as f: | |
f.write('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">') | |
f.write('<g id="saw">') | |
draw_sawblade(f, 16, 100, 125) | |
f.write('</g>') | |
f.write('</svg>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment