Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_triangular_tiling.py
- import tkinter as tk
- import math
- WIDTH, HEIGHT = 600, 600
- class Point:
- """convenience for point arithmetic
- """
- def __init__(self, x, y):
- self.x, self.y = x, y
- def __add__(self, other):
- return Point(self.x + other.x, self.y + other.y)
- def __iter__(self):
- yield self.x
- yield self.y
- def tile_with_triangles(canvas, side_length=50):
- """tiles the entire surface of the canvas with triangular polygons
- """
- triangle_height = int(side_length * math.sqrt(3) / 2)
- half_side = side_length // 2
- p0 = Point(0, 0)
- p1 = Point(0, side_length)
- p2 = Point(triangle_height, half_side)
- for idx, x in enumerate(range(-triangle_height, WIDTH+1, triangle_height)):
- for y in range(-side_length, HEIGHT+1, side_length):
- y += half_side * (idx%2 + 1)
- offset = Point(x, y)
- pa, pb, pc = p0 + offset, p1 + offset,p2 + offset
- canvas.create_polygon(*pa, *pb, *pc, outline='black', fill='', activefill='red')
- p2 = Point(-triangle_height, half_side) # flip the model triangle
- for idx, x in enumerate(range(-triangle_height, WIDTH+triangle_height+1, triangle_height)):
- for y in range(-side_length, HEIGHT+1, side_length):
- y += half_side * (idx%2 + 1)
- offset = Point(x, y)
- pa, pb, pc = p0 + offset, p1 + offset,p2 + offset
- canvas.create_polygon(*pa, *pb, *pc, outline='black', fill='', activefill='blue')
- root = tk.Tk()
- canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='cyan')
- canvas.pack()
- tile_with_triangles(canvas) #, side_length=10)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement