So for my current university paper we are meant to create a Sierpinksi Triangle and Recursively draw new triangles inside.
The original code we got was this:
import sys, pygame
# a function that will draw a right-angled triangle of a given size anchored at a given location
def draw_triangle(screen, x, y, size):
pygame.draw.polygon(screen,white,[[x,y], [x+size,y], [x,y-size]])
#############################################################################################
# Define a function that will draw Sierpinski's Triangle at a given size anchored at a given location
# You need to update this function
# currently only one triangle is drawn
def sierpinski(screen, x, y, size):
draw_triangle(screen, x, y, size)
#############################################################################################
# Initialize the game engine
pygame.init()
# Define the colors we will use in RGB format
black = [ 0, 0, 0]
white = [255,255,255]
blue = [ 0, 0,255]
green = [ 0,255, 0]
red = [255, 0, 0]
# Set the height and width of the screen
size=[512, 512]
screen=pygame.display.set_mode(size)
# Loop until the user clicks the close button.
done=False
clock = pygame.time.Clock()
while done==False:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick(10)
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Clear the screen and set the screen background
screen.fill(black)
# Draw Sierpinski's triangle at a given size anchored at a given location
sierpinski(screen,0, 512, 512)
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Tidy up
pygame.quit ()
Ok I know that this only creates a single triangle. Here is what I did to make it work "sort of":
I created a new triangle function to draw a upside down triangle:
def draw_upside_down_triangle(screen, x, y, size, color):
pygame.draw.polygon(screen, color, [[x+size, y+size], [x+size, y], [x, y]])
Then I updated the old triangle function to accept a color variable:
def draw_triangle(screen, x, y, size, color):
pygame.draw.polygon(screen, color, [[x, y], [x+size, y], [x, y-size]])
After that I updated the main function which will recursively draw triangles:
def sierpinski(screen, x, y, size):
if size < 10:
return False
else:
draw_triangle(screen, x, y, size, white)
draw_upside_down_triangle(screen, x, y/2, size/2, black)
sierpinski(screen, x+size/2, y+size/2, size/2)
sierpinski(screen, x, y-size/2, size/2)
sierpinski(screen, x, y, size/2)
sierpinski(screen, x, y+size/2, size/2)
I started the function off
- By adding the exit argument (when the triangle get's too small return false)
- If it's not too small then draw the first triangle in white
- After that draw an upside down triangle half the size at the same x location but half the y location in black (this creates the 3 triangle illusion)
- After all of that I have 4 recursive calls, based on experimentation I know that the order of these calls matter as the output changes radically when changed.
At the moment the current output is as follows:
I am not asking for anyone to finish or correct my code simply a better understanding or a point in the right direction. Have been battling with this one for a few hours.
Thanks!