
The Pythagoras tree is a fractal tree constructed from squares. It is named after Pythagoras because each triple of touching squares encloses a right triangle, in a configuration traditionally used to represent the Pythagorean theorem.




Task
Construct a Pythagoras tree of order 7 using only vectors (no rotation or trigonometric functions).




Related tasks
 Fractal tree


fcn pythagorasTree{
   bitmap:=PPM(640,640,0xFF|FF|FF);  // White background
 
   fcn(bitmap, ax,ay, bx,by, depth=0){
      if(depth>10) return();
      dx,dy:=bx-ax, ay-by;
      x3,y3:=bx-dy, by-dx;
      x4,y4:=ax-dy, ay-dx;
      x5,y5:=x4 + (dx - dy)/2, y4 - (dx + dy)/2;
      bitmap.cross(x3,y3);bitmap.cross(x4,y4);bitmap.cross(x5,y5);
      bitmap.line(ax,ay, bx,by, 0); bitmap.line(bx,by, x3,y3, 0);
      bitmap.line(x3,y3, x4,y4, 0); bitmap.line(x4,y4, ax,ay, 0);
 
      self.fcn(bitmap,x4,y4, x5,y5, depth+1);
      self.fcn(bitmap,x5,y5, x3,y3, depth+1);
   }(bitmap,275,500, 375,500);
 
   bitmap.writeJPGFile("pythagorasTree.jpg",True);
}();