# Day 171: Sierpinski Triangle Troubleshooting

AP students are writing a VPython program to generate the Sierpinski Triangle using the rules of the chaos game. Can you find the error in this group’s code?

Advertisements

Noschese 180
A picture-a-day for the school year

AP students are writing a VPython program to generate the Sierpinski Triangle using the rules of the chaos game. Can you find the error in this group’s code?

Advertisements

%d bloggers like this:

The -ball.pos.x is a nice red herring. The negative isn’t needed and actually horizontally flips the triangle. Because p1, p2, and p3 define a horizontally symmetric triangle, this "error" is visually undetectable. I believe the error is in the If n=1 subroutine. The ball.pos.y redefinition should be added, not subtracted. By using "-p1.pos.y", this essentially redefines p1 as (0,-sqrt(3)/2,0). You get another Sierpinski triangle, just not where it was expected. From my perspective, the easiest way to understand this is that the mean of any two points is always the midpoint of the given points. All of the addends should be positive and added.I don’t know VPython, but I’m wondering if the code could have been simplified a bit. Would the following work?If n==1: ball.pos=(ball.pos+p1.pos)/2If n==2: ball.pos=(ball.pos+p2.pos)/2If n==3: ball.pos=(ball.pos+p3.pos)/2That is, can the position recomputations be done on the entire vectors, or must they be done on the individual vector components?Fun problem and fun project. Thanks for sharing.