Westonci.ca is the ultimate Q&A platform, offering detailed and reliable answers from a knowledgeable community. Ask your questions and receive precise answers from experienced professionals across different disciplines. Join our Q&A platform to connect with experts dedicated to providing accurate answers to your questions in various fields.

i have to make a code for a sierpinski triangle and these are the directions


A Sierpinski Triangle is a self-similar fractal that results from removing the triangle connecting the three midpoints of a triangle's sides, and continuing this process for the resulting triangles within that triangle.
ght) )
1)
radius)
Typically the triangle can be drawn by a computer program by using a recursive method. Another way to draw a Sierpinski Triangle is by using a series of dots. When enough dots are drawn, the shape will start to form.
The algorithm to accomplish this is demonstrated in the animated image here, Start with 3 vertices of the biggest triangle (biue points). To begin getting all the other points (white points), choose one of the vertices as your starting point. Then find the midpoint from this starting point to one

of the other vertices (red point). From this midpoint you just found and drew, randomly choose one of the vertices and find the midpoint from your current point to this vertex and draw your next point (new red point).
Always finding the midpoint from the red point to a vertex, continue this process.

To accomplish this with Pygame, think of the pints as Rect objects. Use lists of Rect objects for the vertices and all the other points to draw them. Keep appending a Rect object to the list as you find the new midpoint from the current point to a vertex.


we have only done like basic python


i kinda started it and this is what i have so far but i don’t really know if it’s right


import pygame
import random
import sys

WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREY = (128, 128, 128)
BLACK = (0, 0, 0)

pygame.init()
fpsClock = pygame.time.Clock()
FPS = 30


width, height = 480, 360
DISPLAYSURF = pygame.display.set_mode((width, height))

#Main Triangle Vertices
vertex1 = pygame.Rect(width//2, 50, 1, 1)
vertex2 = pygame.Rect(50, height - 50, 1, 1)
vertex3 = pygame.Rect(width - 50, height - 50, 1, 1)

vertices = [vertex1, vertex2, vertex3]

def drawDot(surface, color, position, radius):
pygame.draw.circle(surface, color, position, radius)

def midpoint(p1, p2, color):
midpointX = (p1[0] + p2[0])//2
midpointY = (p1[1] + p2[1])//2
midpoint = (midpointX, midpointY)
drawDot(DISPLAYSURF, color, midpoint, 3)
return midpoint

for vertex in vertices:
drawDot(DISPLAYSURF, BLUE,(vertex.x, vertex.y), 3)


while True: # main game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill(GREY)

vertexRandom = random.randint(0,2)
randomPosition = (vertices[vertexRandom].x, vertices[vertexRandom].y)

newVertex = random.randint(0,2)
newPosition = (vertices[newVertex].x, vertices[newVertex].y)

newMidpoint = midpoint(randomPosition, newPosition, RED)

pygame.display.update()
fpsClock.tick(FPS)





in this code only the red dots are showing not the blue or white


the red dots are supposed to turn into white ones