alt text

It is necessary to draw a triangle at first, 3 more inside it, 3 more inside three, and so on.

How is this implemented?

  • one
    Search in the search engines for the “serpissky implementation of python” - Evgeney Kuznetsov
  • What's the problem? You do not know how to draw a line, or can not calculate the coordinates? - VladD

2 answers 2

Here is the pseudocode:

SierpinskiSieve(v1, v2, v3, recdepth) : if (recdepth == 0) : draw triangle (v1, v2, v3) return m1 = (v2 + v3)/2 m2 = (v3 + v1)/2 m3 = (v1 + v2)/2 SierpinskiSieve(v1, m3, m2, recdepth - 1) SierpinskiSieve(m3, v2, m1, recdepth - 1) SierpinskiSieve(v2, m1, v3, recdepth - 1) 

    My version using turtle, can someone come in handy. l is the length of the side of the triangle, n is the number of recursively drawn Serpiski triangles. For example, >>>sierpinski(100,1) will give the result as in the third picture from the question.

     from turtle import * speed("fastest") def sierpinski(l,n): assert (isinstance(l, int) or isinstance(l, float)) and l>0, 'parameter l is a positive float or integer' assert isinstance(n, int) and n>=0, 'parameter n is a whole number' if n==0: for i in range(0,3): fd(l) left(120) else: sierpinski(l/2,n-1) fd(l/2) sierpinski(l/2,n-1) bk(l/2) left(60) fd(l/2) right(60) sierpinski(l/2,n-1) left(60) bk(l/2) right(60) 
    • Not even 6 years! - trane294
    • I will add a more detailed answer after 6 years :) - Martin