package pythagoras;

import java.applet.*;
import java.awt.*;
import psplot.*;
import real.*;

class DissectionCanvas extends PlotCanvas {
	double A, B, C;
	PlotNode cn;
	RealPoint corner0, corner1;
	
	public DissectionCanvas(Applet a, int w, int h, double x, double y) {
		super(a, w, h);
		A = x; B = y;
		C = Math.sqrt(A*A + B*B);
		double W = 16, H = W*h/w;
		setCorners(-W, -H, W, H);
		// set the origin at the centre
		corner0 = new RealPoint(-delta-A-B, -0.5*(A+B));
		corner1 = new RealPoint( delta, -0.5*(A+B));
		cn = new PlotNode(this, new RealPoint(corner0.x+B, corner0.y+A+B));
		mkFixedpaths();
		repaint();
	}
	
	double delta = 1.0;
	
	PlotPath rightabsquare;
	PlotPath absquare;
	
	void mkFixedpaths() {
		absquare = new PlotPath(this);
		absquare.moveto(corner0);
		absquare.rlineto(A+B, 0);
		absquare.rlineto(0, A+B);
		absquare.rlineto(-(A+B), 0);
		absquare.close();

		rightabsquare = new PlotPath(this);
		rightabsquare.moveto(corner1);
		rightabsquare.rlineto(A+B, 0);
		rightabsquare.rlineto(0, A+B);
		rightabsquare.rlineto(-(A+B), 0);
		rightabsquare.close();

	}
	
	boolean ready = true;
	
	public void draw() {
		ready = false;

		// left side
		
		absquare.stroke(Color.black);
		PlotPath cs = new PlotPath(this);
		cs.moveto(corner0.x+A, corner0.y);
		cs.rlineto(B, A);
		cs.rlineto(-A, B);
		cs.rlineto(-B, -A);
		cs.close();
		cs.stroke(Color.black);
		
		translate(corner0.x, corner0.y);
		PlotPath p = new PlotPath(this);
		p.moveto(0, B);
		p.lineto(A, 0);
		double y = (B-A)*A/B;
		p.lineto(B, y);
		p.lineto(B, B);
		p.close();
		p.fill(Color.pink);
		p.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(0, B);
		p.rlineto(B-A, 0);
		p.rlineto(0, y);
		p.close();
		p.fill(Color.pink);
		p.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(B, B);
		p.rlineto(-A, 0);
		p.rlineto(0, y);
		p.lineto(B, A+B);
		p.close();
		p.fill(Color.red);
		p.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(B, A+B);
		p.lineto(B, A);
		p.lineto(A+B, A);
		p.close();
		p.fill(Color.pink);
		p.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(B, A);
		p.rlineto(A, 0);
		p.lineto(B, y);
		p.close();
		p.fill(Color.red);
		p.stroke(Color.black);
		
		translate(-corner0.x, -corner0.y);
		
		cn.draw();

		// right side
		
		translate(delta, -0.5*(A+B));
		
		rightabsquare.stroke(Color.black);
		
		PlotPath as = new PlotPath(this);
		as.moveto(B, B);
		as.rlineto(A, 0);
		as.rlineto(0, A);
		as.rlineto(-A, 0);
		as.close();
		as.fill(Color.red);
		as.stroke(Color.black);
		
		PlotPath bs = new PlotPath(this);
		bs.moveto(0, 0);
		bs.rlineto(B, 0);
		bs.rlineto(0, B);
		bs.rlineto(-B, 0);
		bs.close();
		bs.fill(Color.pink);
		bs.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(0, 0);
		p.lineto(A, 0);
		p.lineto(0, B);
		p.close();
		p.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(A, 0);
		p.lineto(B, 0);
		p.rlineto(0, y);
		p.close();
		p.stroke(Color.black);
		
		p = new PlotPath(this);
		p.moveto(B, B+y);
		p.lineto(B, A+B);
		p.lineto(A+B, A+B);
		p.close();
		p.stroke(Color.black);
		
		translate(-delta, 0.5*(A+B));
		ready = true;
	}
	
	boolean active = false;

	public boolean mouseDown(Event evt, int x, int y) {
		// if (x, y) inside a node set mouseState = MouseActive
		RealPoint P = toRealPoint(x, y);
		if (cn.contains(P)) {
			active = true;
		}
		return true;
	}

	public boolean mouseDrag(Event evt, int x, int y) {
		if (active && ready) {
			double D = A+B;
			RealPoint q = toRealPoint(x, y);
			if (q.x < -(D)*0.5-delta) q.x = -(D)*0.5-delta;
			if (q.x >= -delta) q.x = -delta;
			q.y = (D)*0.5;
			cn.locate(q);
			B = q.x + (this.A+this.B)+delta;
			A = -q.x - delta;
			C = Math.sqrt(A*A + B*B);
			repaint();
		}
		return true;
	}

	public boolean mouseUp(Event evt, int x, int y) {
		// set nodes inactive
		active = false;
		return true;
	}
}

public class DissectionApplet extends Applet {

	double a = 3, b = 4;
	DissectionCanvas baby;
	
	public void init() {
		setBackground(Color.white);
		baby = new DissectionCanvas(this, 500, 150, a, b);
		add(baby);
		show();
	}

}