r/pythonhelp • • Oct 18 '20

Need hep :(

Hi guys,

I'm new in the coding scene and I took a complementary class at my CEGEP because it looks fun. However the course is so hard and he want us to do a project a week without teaching us anything. We have to teach ourself everythings. Moreover, we use sage phyton not pure python so it's harder to find things online... So I'm beggeing your help on one of the question of the project that is:

Sierpinski triangle: The Sierpinski triangle is formed from a black equilateral triangle, by erasing an ”upside-down” triangle in the middle, and then by repeating this process to each remaining black triangle.

(a) Write a function white_triangles(), which takes as input a list of three points (tuples) [A,B,C], and an integer current_iter, and which returns a graphics object S which will contain many white triangles (so it will only be visible if we show it on top of a black background later). Hint: Create an empty graphics object at the beginning of the function with S=Graphics()). If current_iter is equal to 0, the function should stop and return the empty graphics object S. Otherwise, it should do the following: • Calculate the coordinates of the midpoints of all three sides (call them M1, M2, and M3).

• Add a white ”middle” triangle to S, which connects the three midpoints. Hint: Use the command polygon2d([M1,M2,M3], color=’white’, fill=True).

• If current_iter==1, stop here. Otherwise, ”deal” with each of the surrounding small triangles: For each of the three ”small” triangles surrounding the white triangle, assuming that its vertices are P1, P2 and P3, add to S the output of white_triangles([P1,P2,P3],current_iter-1): i.e. the function should recursively call ”itself” (but with the iteration counter decreased by 1!), to deal with each of the smaller triangles. Note that for example for the lower left triangle, its vertices will be A, M1, and M3 (assuming you labelled the bottom left vertex A and then went counter-clockwise.)

(b) Write a function sierpinski(), which takes as input a list L of three points (tuples) and an integer maxiter, and which creates the plot of a black filled triangle, and adds to it the output of white_triangles(L,maxiter).

(c) Show the output of sierpinski([(0,0),(1,0),(1/2, sqrt(2)/2)],7) (without axes, but with 1-to-1 aspect ratio).

(d) Show the output of sierpinski(regpolygon(3),7) (without axes, but with 1-to-1 aspect ratio).

I reaaly tried har but only came up with that

def white_triangle(points, current_iter):

S=Graphics()

if current_iter == 0:

return S

elif current_iter == 1:

return S

else:

AB= vector(points[0])

BC= vector(points[1])

AC= vector(points[3])

M1= (AB[0] / 2, AB[1] / 2)

M2= (BC[0] / 2, BC[1] / 2)

M3= (AC[0] / 2, AC[1] / 2)

polygon2d([M1,M2,M3], color="white", fill=True)

That probably sucks but that the best that I got...

0 Upvotes

0 comments sorted by