package pythagoras;

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

class perigal2Canvas extends PlotCanvas {
	double A, B, C;
	PlotNode cn;
	
	public perigal2Canvas(Applet a, int w, int h, double x, double y) {
		super(a, w, h);
		A = x; B = y;
		double W = 8, H = W*h/w;
		// set the origin at the centre
		setCorners(-W+1, -H, W+1, H);
		cn = new PlotNode(this, new RealPoint(0, A));
		repaint();
	}
	
	boolean ready = true;
	
	public void draw() {
		ready = false;
		PlotPath parallelogram;
		PlotPath triangle;
		PlotPath square;
		PlotPath block;
		
		double D = (B - A)/2.0;
		
		parallelogram = new PlotPath(this);
		parallelogram.moveto(0, -D);
		parallelogram.lineto(B, -A-D);
		parallelogram.lineto(B, 0);
		parallelogram.lineto(0, A);
		parallelogram.close();
		parallelogram.fill(Color.green);
		parallelogram.stroke(Color.black);

		triangle = new PlotPath(this);
		triangle.moveto(0, 0);
		triangle.lineto(B, 0);
		triangle.lineto(0, A);
		triangle.close();
		// triangle.fill(Color.green);
		triangle.stroke(Color.black);
		
		square = new PlotPath(this);
		square.moveto(0, 0);
		square.lineto(0, A);
		square.rlineto(-A, 0);
		square.rlineto(0, -A);
		square.close();
		square.fill(Color.red);
		square.stroke(Color.black);
	
		block = new PlotPath(this);
		block.moveto(B/2, A/2);
		block.rlineto(0, A + D);
		block.rlineto(-D, 0);
		block.lineto(0, A);
		block.close();
		block.fill(Color.pink);
		block.stroke(Color.black);
		
		block = new PlotPath(this);
		block.moveto(B/2, A/2);
		block.rlineto(0, D);
		block.lineto(B + A/2.0, B/2.0);
		block.lineto(B, 0);
		block.close();
		block.fill(Color.pink);
		block.stroke(Color.black);
		
		block = new PlotPath(this);
		block.moveto(B + A/2.0-D, B/2.0);
		block.rlineto(D, 0);
		block.lineto(B+A, B);
		block.rlineto(-B/2.0, A/2.0);
		block.fill(Color.pink);
		block.close();
		block.stroke(Color.black);
		
		block = new PlotPath(this);
		block.moveto(A, A+B);
		block.rlineto(B/2.0, -A/2.0);
		block.rlineto(0, -D);
		block.rlineto(-(A+D), 0);
		block.close();
		block.fill(Color.pink);
		block.stroke(Color.black);
		
		square = new PlotPath(this);
		square.moveto(B/2.0, A*0.5 + D);
		square.rlineto(A, 0);
		square.rlineto(0, A);
		square.rlineto(-A, 0);
		square.close();
		square.fill(Color.red);
		square.stroke(Color.black);
		cn.draw();
		
		// --------------------------------
		
		block = new PlotPath(this);
		block.moveto(0, 0);
		block.lineto(B-D, 0);
		block.lineto(B/2.0, -B/2.0);
		block.lineto(0, -D);
		block.close();
		// block.fill(Color.pink);
		block.stroke(Color.black);

		block = new PlotPath(this);
		block.moveto(B-D, 0);
		block.lineto(B/2.0, -B/2.0);
		block.lineto(B, -(B-D));
		block.lineto(B, 0);
		block.close();
		// ? block.fill(Color.pink);
		block.stroke(Color.black);

		block = new PlotPath(this);
		block.moveto(0, -B);
		block.rlineto(D, 0);
		block.lineto(B/2.0, -B/2.0);
		block.lineto(0, -D);
		block.close();
		block.fill(Color.pink);
		block.stroke(Color.black);

		block = new PlotPath(this);
		block.moveto(B, -B);
		block.rlineto(0, D);
		block.lineto(B/2.0, -B/2.0);
		block.lineto(D, -B);
		block.close();
		block.fill(Color.pink);
		block.stroke(Color.black);
		
		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) {
			RealPoint q = toRealPoint(x, y);
			if (q.y > B) q.y = B;
			else if (q.y < 0) q.y = 0;
			q.x = 0;
			cn.locate(q);
			A = q.y;
			repaint();
		}
		return true;
	}

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

public class perigal2Applet extends Applet {

	double a = 3, b = 4;
	perigal2Canvas baby;
	
	public void init() {
		setBackground(Color.white);
		baby = new perigal2Canvas(this, 
			bounds().width, bounds().height, a, b);
		add(baby);
		show();
	}

}