Creating a (R,G,B) Sierpinski Triangle in python, using pygame window. Each corner is, respectively Red, Green, and Blue and gradient into each other

StackOverflow https://stackoverflow.com/questions/22135235

  •  19-10-2022
  •  | 
  •  

Question

So, for my computer science class we are supposed to import the pygame from the website:

http://www.petercollingridge.co.uk/image-processing-pygame

Then we are supposed to create the sierpinski triangle in python in the pygame window using pixels. So each pixel in the window needs to be colored in the shape of the triangle. I can't even get my triangle to show up in just black pixels, and what we're supposed to do is get it to appear with the top corner as red, the bottom left as green, and the bottom right as blue. These colors are supposed to slowly fade into each other (gradient) throughout the triangle. The finished process is supposed to look something like this:

http://eldar.mathstat.uoguelph.ca/dashlock/ftax/Gallery/Sierpenski2D960.gif

First off, everytime I set the window up it says that the midPoint in my main function, where I call the earlier midPoint function is not assigned. This confuses me because I clearly assigned it in the very first function: def midPoint, so any help with that problem would be great. Other than that I'm not sure why I can't get the actual triangle to show up. I just want to start out by getting it to show up black first, and then color it. Any help on what is wrong with my, most likely, awful code would be much appreciated. I am supposed to be minoring in computer science but I am failing the class. I am desperate. Please you can even make fun of my shitty code but I need something, anything. I'm lost. Thank you.

#######################################

import pygame, math, random

pygame.init()

#######################################

def midpoint (x0, x1, y0, y1):

    panda = ((x0 + x1)/2)
    polarbear = ((y0 + y1)/2)
    return panda, polarbear

#######################################

def randomPoint (width, height):

    potato = (random.random() * width)
    pickle = (random.random() * height)
    return potato, pickle


#newImage

#   PRE: Takes size, which is a 2-tuple (width, height) and provides size of image
#   POST: Creates list of length size[0]. Each item in list is length size[1]. Each item of list is a 3-tuple.
#
#   Points of this data structure are denoted as image[x][y] and each point has 3 components: [0] for red, [1] for green, and [2] for blue
#
def newImage(size):

return pygame.surfarray.array3d(pygame.Surface(size))
#######################################

#showImage
#   Takes image created by newImage and displays it in open window
#   PRE: image is a list of lists of 3-tuples. Each 3 tuple corresponds to a (R,G,B) color.
#   POST: image is displayed in window.
#
def showImage(image):

    width, height, depth = image.shape
    pygame.display.set_mode((width, height))
    surface = pygame.display.get_surface()
    pygame.surfarray.blit_array(surface, image)
    pygame.display.flip()

#######################################

# MAIN PROGRAM

pygame.init()

width = int(input("How large do you want your window? "))
height = int(input("How tall do you want your window? "))
window = newImage((width, height))

for x in range(width):
    for y in range(height):
        window[x][y] = (255,255,255) #Colors window white
showImage(window)

#

p = randomPoint(width, height)

for i in range(1000001):

    corners = [(width, height),(0, height),(width//2, 0)]
    c = random.choice(corners)
    mid = midPoint(p[0], p[1], c[0], c[1])
    if i > 10:
        window[(mid[0])][(mid[1])] = (0,0,0)
    p = mid
    if i % 1000 == 0:
        showImage(window)

#

print('Done!')

input("Enter to quit")

pygame.quit()
#
#######################################`

No correct solution

OTHER TIPS

###################SIERPINSKI TRIANGLE (R,G,B)###################
                   ###########################
                       ###################
                            ##########

#######################################

import pygame, math, random
pygame.init()

#######################################

#newImage
#   PRE: takes a 2-tuple (width, height) input from user and provides size of image
#   POST: Creates list, len[0]. Each item in list is len[1]. Each item is 3-tuple.
#   Points of data structure (pixels) are denoted as image[x][y] and each point has 3 components:
##########################################################################################       
[0] - RED
##########################################################################################     
[1] - GREEN
########################################################################################## 
[2] - BLUE

def newImage(size):
    return pygame.surfarray.array3d(pygame.Surface(size))

#######################################

#showImage
#   Main function: Takes image created by "newImage" and displays it in pygame window
#   PRE: image is a LIST OF LISTS of 3-tuples. Each 3 of the tuples corresponds to a (R,G,B) color.
#   POST: image is displayed in window

def showImage(image):
    width, height, depth = image.shape
    pygame.display.set_mode((width, height))
    surface = pygame.display.get_surface()
    pygame.surfarray.blit_array(surface, image)
    pygame.display.flip()

#######################################

#randomPoint
#   set the variable "p" in main function to the returned values of this function which should be a random point
#   PRE: takes in 2-tuple (width, height)
#   POST: returns coordinates of a random point in the size of the image

def randomPoint(width, height):
    ex = (random.random() * width)
    wye = (random.random() * height)
    return ex, wye

#######################################

#midPoint
#   find the mid-point between "p" - random point and "c" - random corner in the main function
#   PRE: take in all 4 coordinates of the 2 points
#   POST: calculate the mid-point between these 2 points and color it black. Set p to the midPoint once this function is complete and repeat.

def midPoint(x0, y0, x1, y1):
    eks = ((x0 + x1)/2)
    wie = ((y0 + y1)/2)
    return eks, wie

#######################################

def colorPoint (x, y, width, height):
    w = (255/width)
    h = (255/height)
    x_color = x*w
    y_color = y*h
    r = math.fabs(255 - y_color)
    g = math.fabs(255 - x_color)
    b = math.fabs(255 - x_color - y_color)
    return r, g, b
    showImage(window)

#######################################

# MAIN PROGRAM

# Start the CHAOS GAME

pygame.init()

#######################################

# Get user input for the width and height of the window

width = int(input("How wide would you like your window to be? "))
height = int(input("How tall would you like your window to be? "))
window = newImage((width, height))

for ecks in range(width):
    for why in range(height):
        window[ecks][why] = (255,255,255) #Colors window white
showImage(window)

#######################################

# Set "p" to starting value

p = 1

# Call randomPoint in order to set "p" to a random point within the parameters of the window size

p = randomPoint(width, height)
i = 0
for i in range(1000001):
    corners = [(width, height),(0, height),(width//2, 0)]
    c = random.choice(corners)
    mid = midPoint(p[0], p[1], c[0], c[1])
    colour = colorPoint((mid[0]), (mid[1]), width, height)
    if i > 10:
        window[(mid[0])][(mid[1])] = colour 
        i += 1
    p = mid
    if i % 1000 == 0:
        showImage(window)

#######################################

#End the CHAOS GAME

print ('Done!')
input ("ENTER to quit")
pygame.quit()

#######################################
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top