//  Hanno Rein 
//  http://hanno-rein.de


import java.applet.Applet;
import java.awt.*;

public class Pytho extends Applet {
	int maxdepth = 18;
	
	public void init(){
		//this.setSize(1000,650);
	}

	public void paint(Graphics g){
		int startwidth = (int)(this.getWidth()/65*10);
		for (int i=0;i<this.getHeight();i++){
			g.setColor(new Color((int)(200-i/(double)this.getHeight()*100),(int)(200-i/(double)this.getHeight()*100),255));
			g.drawLine(0,i,this.getWidth(),i);
		}
		recPytho(g,(this.getWidth()-startwidth)/2,this.getHeight()-2,(this.getWidth()+startwidth)/2,this.getHeight()-2,0);
	}
	
	
	public void recPytho(Graphics g,int x_0,int y_0,int x_1,int y_1, int depth){
		if (depth>= maxdepth){
			if (Math.random()<0.0005){
				g.setColor(Color.RED);
				g.fillArc(x_0,y_0,10,10,0,360);
			}
			return;
		}
		double w,h_2;
		int x_2,y_2;
		
		
		
		
		w = 0.2*Math.random()+0.4;
		//w=0.5;
		h_2 =  Math.sqrt(0.25-(0.5-w)*(0.5-w));
		x_2 = (int)(x_0+(y_1-y_0)+w*(x_1-x_0)+h_2*(y_1-y_0));
		y_2 = (int)(y_0-(x_1-x_0)-h_2*(x_1-x_0)+w*(y_1-y_0));
		
		if (x_2-x_1+(y_1-y_0)==0&&y_2-y_1-(x_1-x_0)==0){
			if (Math.random()>0.5){
				y_2+=Math.random()*2-1;
			}else{
				x_2+=Math.random()*2-1;
			}
		}
		
		
		
		int xs[]  = {x_0, x_1, x_1+(y_1-y_0), x_0+(y_1-y_0)};
		int ys[]  = {y_0, y_1, y_1-(x_1-x_0), y_0-(x_1-x_0)};
		int xs2[] = {x_1+(y_1-y_0), x_0+(y_1-y_0),x_2};
		int ys2[] = {y_1-(x_1-x_0), y_0-(x_1-x_0),y_2};
		g.setColor(new Color(70-(int)((depth*2)/(double)(maxdepth*2)*70),(int)((depth*2)/(double)(maxdepth*2)*80+50),0));
		g.fillPolygon(xs,ys,4);
		g.setColor(new Color(70-(int)((depth*2+1)/(double)(maxdepth*2)*70),(int)((depth*2+1)/(double)(maxdepth*2)*80+50),0));
		g.fillPolygon(xs2,ys2,3);
		

		if (Math.random()>0.5){
			recPytho(g,x_0+(y_1-y_0),y_0-(x_1-x_0),x_2,y_2,depth+1);
			recPytho(g,x_2,y_2,x_1+(y_1-y_0),y_1-(x_1-x_0),depth+1);
		}else{
			recPytho(g,x_2,y_2,x_1+(y_1-y_0),y_1-(x_1-x_0),depth+1);
			recPytho(g,x_0+(y_1-y_0),y_0-(x_1-x_0),x_2,y_2,depth+1);
		}
		
	}
}

