This Python script draws the Sierpinski triangle.<br>
<br>As with my Koch snowflake example, the user can modify the number of triangles using left and right arrows.<br>
<br>
diff --git a/data/GSOC examples/Sierpinski - graphics b/data/GSOC examples/Sierp<br>
new file mode 100644<br>
index 0000000..449277a<br>
--- /dev/null<br>
+++ b/data/GSOC examples/Sierpinski - graphics <br>
@@ -0,0 +1,146 @@<br>
+# This example draws what is called the Sierpinski triangle. <br>
+# First, one black triangle is created. <br>
+# After that it is removed and in its place three smaller black triangles are c<br>
+# which leaves a white hole (also shaped like a triangle, but upside down!) in <br>
+#<br>
+# This can continue for any number of steps - you can split the smaller<br>
+# triangles into even smaller ones and so on. <br>
+# We have, however, limited it to 8 to keep our computers from freezing due to<br>
+# too much calculations! If you change it to a higher number, that will probabl<br>
+#<br>
+# This code is similar to the code in "Koch snowflake" example so you <br>
+# could first go through that to understand this example better.<br>
+<br>
+import pippy, pygame, sys<br>
+from pygame.locals import *<br>
+from math import sin, cos<br>
+from math import pi as Pi<br>
+<br>
+class Triangle(object):<br>
+ def __init__(self, first_vertex, length, displacement_angle = 0 ):<br>
+ # remember your first vertex<br>
+ self.A = first_vertex<br>
+ # calculate the other two<br>
+ self.B = self.A[0] + length * cos(Pi/3 + displacement_angle), \<br>
+ self.A[1] - length * sin(Pi/3 + displacement_angle)<br>
+ self.C = self.A[0] + length * cos(displacement_angle), \<br>
+ self.A[1] - length * sin(displacement_angle)<br>
+ # remember your length<br>
+ self.length = length<br>
+ # calculate the midpoints of each line<br>
+ # m1 for AB, m2 for BC, m3 for CA<br>
+ # m1 and m3 are calculated the same way as points B and C, but with<br>
+ # half the length.<br>
+ self.m1 = self.A[0] + length/2 * cos(Pi/3 + displacement_angle), \<br>
+ self.A[1] - length/2 * sin(Pi/3 + displacement_angle)<br>
+ self.m3 = self.A[0] + length/2 * cos(displacement_angle), \<br>
+ self.A[1] - length/2 * sin(displacement_angle)<br>
+ # m2 is 120 degrees (2*Pi/3) from C, half the length.<br>
+ # ... but we don't actually need it for anything.<br>
+ self.m2 = self.C[0] + length/2 * cos(2*Pi/3 + displacement_angle), \<br>
+ self.C[1] - length/2 * sin(2*Pi/3 + displacement_angle)<br>
+ <br>
+ # create three new triangles from yourself.<br>
+ def split(self):<br>
+ new_triangles = []<br>
+ new_triangles.append(Triangle(self.A, self.length/2))<br>
+ new_triangles.append(Triangle(self.m1, self.length/2))<br>
+ new_triangles.append(Triangle(self.m3, self.length/2))<br>
+ return new_triangles<br>
+<br>
+ # This is how a triangle draws itself.<br>
+ def draw(self, screen, color):<br>
+ points = [self.A, self.B, self.C]<br>
+ pygame.draw.polygon(screen, color, points)<br>
+<br>
+# always need to init first thing before drawing<br>
+pygame.init()<br>
+<br>
+# XO screen is 1200x900<br>
+size = width, height = 1200, 800<br>
+<br>
+# create the window and keep track of the surface<br>
+# for drawing into<br>
+screen = pygame.display.set_mode(size)<br>
+<br>
+# The font we'll use to display the current depth.<br>
+font_size = 36<br>
+font = pygame.font.Font(None, font_size)<br>
+<br>
+black = (0, 0, 0)<br>
+white = (255, 255, 255)<br>
+<br>
+starting_point = (200, 750)<br>
+side_length = 800<br>
+<br>
+t1 = Triangle(starting_point, side_length)<br>
+t2 = Triangle((800, 600), 150)<br>
+<br>
+depth = 0<br>
+<br>
+all_triangles = [t1]<br>
+new_triangles = []<br>
+<br>
+recalculate = False<br>
+<br>
+while pippy.pygame.next_frame():<br>
+ for event in pygame.event.get():<br>
+ if event.type == QUIT:<br>
+ sys.exit()<br>
+ # When R arrow is pressed, go one step further.<br>
+ # This means splitting the existing triangle(s) into new ones.<br>
+ # Note that all the triangles have to be recalculated before redrawn.<br>
+ elif event.type == KEYDOWN and event.key == K_RIGHT and depth < 8:<br>
+ depth += 1<br>
+ recalculate = True<br>
+ # When L arrow is pressed, go one step back, reducing the number of <br>
+ # triangles.<br>
+ # Note that all the triangles have to be recalculated before redrawn.<br>
+ elif event.type == KEYDOWN and event.key == K_LEFT and depth > 0:<br>
+ depth -= 1<br>
+ recalculate = True<br>
+ elif event.type == KEYDOWN:<br>
+ sys.exit() <br>
+ screen.fill(white)<br>
+ # Display the current step.<br>
+ msg = "Step: " + str(depth) + "/8"<br>
+ text = font.render(msg , True, black)<br>
+ text_box = text.get_rect()<br>
+ text_box.top = 130<br>
+ text_box.left = 50<br>
+ # Display the instructions<br>
+ text2 = font.render("Use left and right arrows.", True, black)<br>
+ text_box2 = text2.get_rect()<br>
+ text_box2.top = 100<br>
+ text_box2.left = 50<br>
+ # Write the instructions and the current step on the screen.<br>
+ screen.blit(text, text_box)<br>
+ screen.blit(text2, text_box2)<br>
+ <br>
+ # If the depth was changed (L or R pressed), recalculate everything so we c<br>
+ # draw the change.<br>
+ if recalculate == True:<br>
+ # Delete the existing triangles.<br>
+ all_triangles = [t1] <br>
+ new_triangles = []<br>
+ # Keep splitting until the new value of "depth" variable is reached. <br>
+ # (it can be one more (R key) or one less (L key) from the last value)<br>
+ for step in range(depth):<br>
+ for triangle in all_triangles:<br>
+ new_triangles += triangle.split()<br>
+ all_triangles = new_triangles<br>
+ new_triangles = []<br>
+ recalculate = False<br>
+<br>
+ # Draw the triangle on the screen.<br>
+ for triangle in all_triangles:<br>
+ triangle.draw(screen, black)<br>
+ <br>
+ # Refresh the screen.<br>
+ pygame.display.flip()<br>
+<br>
<br>