Created
April 18, 2017 14:10
-
-
Save plut0nium/9653ff5501799429fcae590268276bd0 to your computer and use it in GitHub Desktop.
Draw a Sierpinski triangle using the Chaos game algorithm
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
# draw a Sierpinski triangle using the Chaos game algorithm | |
import random | |
import tkinter as tk | |
width = 640 | |
height = 640 | |
background_color = "#000000" | |
foreground_color = "#ffffff" | |
root = tk.Tk() | |
root.resizable(0,0) | |
canvas = tk.Canvas(root, width=width, height=height, bg=background_color) | |
canvas.pack() | |
img = tk.PhotoImage(width=width, height=height) | |
canvas.create_image((width//2, height//2), image=img, state="normal") | |
vertices = [(width//2,0),(0,height),(width,height)] | |
current_position = (width//2, height//2) | |
def draw(): | |
global current_position | |
x1, y1 = current_position | |
x2, y2 = random.choice(vertices) | |
current_position = ((x1 + x2)//2, | |
(y1 + y2)//2) | |
img.put(foreground_color, current_position) | |
canvas.after(1, draw) | |
draw() | |
tk.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment