Last active
October 28, 2018 00:07
-
-
Save carlynorama/dd511421432aa306e97114afe6e160ae to your computer and use it in GitHub Desktop.
Generates pattern of triangles, one segment at a time
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
import math | |
save_file_name = "pattern" | |
def triangle(origin_x, origin_y): | |
A_x = triangle_half_base + origin_x | |
A_y = origin_y | |
B_x = triangle_side + origin_x | |
B_y = origin_y + triangle_height | |
C_x = origin_x | |
C_y = origin_y + triangle_height | |
f.write('\t<line x1="%s" y1="%s" x2="%s" y2="%s" style="%s" />\n' % (A_x, A_y, B_x, B_y, line_style)) | |
f.write('\t<line x1="%s" y1="%s" x2="%s" y2="%s" style="%s" />\n' % (B_x, B_y, C_x, C_y, line_style)) | |
f.write('\t<line x1="%s" y1="%s" x2="%s" y2="%s" style="%s" />\n' % (C_x, C_y, A_x, A_y, line_style)) | |
viewBoxWidth = 300 | |
viewBoxHeight = 550 | |
# half an equalteral triangle is a 30-60-90 triangle | |
# length of base = x | |
# length of height = x * math.sqrt(3) | |
# length of hypotenuse = 2x (same as orig side of trianlge) | |
# height of an equalat. given the side length a -> h = math.sqrt(3) / 2 * a | |
triangle_side = float(10) | |
triangle_height = float(float(math.sqrt(3)) * triangle_side * float(0.5)) | |
triangle_half_base = float(triangle_side/2) | |
home_x = 0 | |
home_y = 0 | |
line_style = "stroke:rgb(0,0,0);stroke-width:0.5" | |
f = open('%s.svg' % save_file_name, 'w') | |
f.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') | |
f.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n') | |
f.write('<svg width="100%%" height="100%%" viewBox="0 0 %s %s" xmlns="http://www.w3.org/2000/svg">\n' % (viewBoxWidth, viewBoxHeight)) | |
f.write('<g id="triangle mesh" transform="translate(%s, %s)">\n' % ("0", "0")) | |
for r in range(50): | |
for c in range(50): | |
start_x = float(float(triangle_side*c) + float(r%2 * triangle_half_base)) | |
start_y = float(triangle_height*r) | |
triangle(start_x, start_y) | |
f.write('\t</g>\n') #end grid | |
f.write('</svg>') | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment