Skip to content

Instantly share code, notes, and snippets.

@fogleman
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogleman/a57816791e79bb9c16fa to your computer and use it in GitHub Desktop.
Save fogleman/a57816791e79bb9c16fa to your computer and use it in GitHub Desktop.
Polygon Tiling G-code
from tile import Model, Shape
def normalize(x, y):
return (round(x + 3.5, 6), round(y + 3.5, 6))
def create_model():
model = Model(width=700, height=700, scale=100)
model.append(Shape(6))
a = model.add(0, range(6), 4)
b = model.add(a, 1, 3)
c = model.add(a, 2, 6)
model.repeat(c)
return model
def find_edges(model):
result = set()
shapes = model.lookup.values()
for shape in shapes:
points = shape.points()
for (x1, y1), (x2, y2) in zip(points, points[1:]):
x1, y1 = normalize(x1, y1)
x2, y2 = normalize(x2, y2)
if x1 <= 0 or y1 <= 0 or x2 <= 0 or y2 <= 0:
continue
if x1 >= 7 or y1 >= 7 or x2 >= 7 or y2 >= 7:
continue
a, b = (x1, y1), (x2, y2)
edge = tuple(sorted([a, b]))
result.add(edge)
return sorted(result)
def pop(edges, point):
for a, b in edges:
if a == point:
edges.remove((a, b))
return b
if b == point:
edges.remove((a, b))
return a
return None
def combine_edges(edges):
result = []
edges = list(edges)
while edges:
path = []
start, point = edges.pop(0)
path.append(start)
while point:
path.append(point)
point = pop(edges, point)
result.append(path)
return result
def emit_code(paths):
print 'M3'
print 'M8'
print 'G0Z0.5'
for path in paths:
print 'G0X%0.6fY%0.6f' % path[0]
print 'G1Z-0.25F75'
for point in path[1:]:
print 'G1X%0.6fY%0.6fF75' % point
print 'G0Z0.5'
print 'M5'
print 'M9'
def main():
model = create_model()
edges = find_edges(model)
paths = combine_edges(edges)
emit_code(paths)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment