import java.awt.*;
import java.applet.*;
import java.util.*;

public class Sqr2 extends Applet {


	Canvas hereCanvas;
	Button touchMe;
	int w, h;

	long cTime = 0;

    Image oImage;
    Graphics oGraphics;
	mySquare joey1, joey2;
	int posx, posy;
	int distx=0, disty=0;
	int xInter[];
	int yInter[];
	int xUnion[];
	int yUnion[];

	Polygon inter;

    public void init() {

		Dimension d = size();
		w=d.width;
		h=d.height;

        oImage = createImage(w, h);
        oGraphics = oImage.getGraphics();

        setBackground(Color.white);
        setForeground(Color.black);

		int[] x1 = {200, 330, 330, 200, 200};
		int[] y1 = {20, 20, 150, 150, 20};

		int[] x2 = {20, 260, 260, 20, 20};
		int[] y2 = {60, 60, 300, 300, 60};

		joey1 = new mySquare(1, x1, y1, 5);
		joey2 = new mySquare(1, x2, y2, 5);
	
		repaint();

    }

 
    public boolean mouseDown(Event e, int x, int y) {
		this.posx = x; 	
		this.posy = y;
        return true;
    }


	public boolean mouseDrag(Event e, int x, int y) {
		distx = x - posx;
		disty = y - posy;

		if (joey1.inXscale(posx, posy)) joey1.scale(distx);
		else if (joey2.inXscale(posx, posy)) joey2.scale(distx);

		else if (joey1.inYscale(posx, posy)) joey1.scale(disty);
		else if (joey2.inYscale(posx, posy)) joey2.scale(disty);

		else if (joey1.inDrag(posx, posy)) joey1.drag(distx, disty);
		else if (joey2.inDrag(posx, posy)) joey2.drag(distx, disty);

		this.repaint();

		this.posx = x; 	
		this.posy = y;

        return true;
    }


    public void paint( Graphics g ) {
		
        update(g);
    }

    public void update(Graphics g) {
        oGraphics.setColor(Color.white);
        oGraphics.fillRect(0, 0, w, h);


		if (findUnion(joey1, joey2) != null) {
        		oGraphics.setColor(new Color(240,247,255));
			oGraphics.fillPolygon(findUnion(joey1, joey2));
        		oGraphics.setColor(Color.black);
			oGraphics.drawPolygon(findUnion(joey1, joey2));
		}
		
		joey1.paint(oGraphics);
		joey2.paint(oGraphics);

		if (findUnion(joey1, joey2) != null) {
       		oGraphics.setColor(Color.black);
			oGraphics.drawRect(xUnion[1]-8,yUnion[1]-8,16,16);
			oGraphics.drawRect(xUnion[2]-8,yUnion[2]-8,16,16);
			oGraphics.drawRect(xUnion[3]-8,yUnion[3]-8,16,16);
			oGraphics.drawRect(xUnion[4]-8,yUnion[4]-8,16,16);
			oGraphics.drawRect(xUnion[5]-8,yUnion[5]-8,16,16);
			oGraphics.drawRect(xUnion[6]-8,yUnion[6]-8,16,16);
			oGraphics.drawRect(xUnion[7]-8,yUnion[7]-8,16,16);
			oGraphics.drawRect(xUnion[8]-8,yUnion[8]-8,16,16);
			oGraphics.drawRect(xUnion[0]-8,yUnion[0]-8,16,16);
		}



        g.drawImage(oImage, 0, 0, this);
	}

	public Polygon findUnion(mySquare s1, mySquare s2)  {

			int xx1 = Math.min(s1.x[0], s2.x[0]);
			int xx2 = Math.max(s1.x[0], s2.x[0]);
			int xx3 = Math.min(s1.x[2], s2.x[2]);
			int xx4 = Math.max(s1.x[2], s2.x[2]);

			int yy1 = Math.min(s1.y[0], s2.y[0]);
			int yy2 = Math.max(s1.y[0], s2.y[0]);
			int yy3 = Math.min(s1.y[2], s2.y[2]);
			int yy4 = Math.max(s1.y[2], s2.y[2]);

			int x1 = Math.min(s1.x[0], s2.x[0]) - 5;
			int x2 = Math.max(s1.x[0], s2.x[0]) - 5;
			int x3 = Math.min(s1.x[2], s2.x[2]) + 5;
			int x4 = Math.max(s1.x[2], s2.x[2]) + 5;

			int y1 = Math.min(s1.y[0], s2.y[0]) - 5;
			int y2 = Math.max(s1.y[0], s2.y[0]) - 5;
			int y3 = Math.min(s1.y[2], s2.y[2]) + 5;
			int y4 = Math.max(s1.y[2], s2.y[2]) + 5;

			if ((xx1==s1.x[0] & xx4==s2.x[2] & yy1==s1.y[0] & yy4==s2.y[2]) 
				|| (xx1==s2.x[0] & xx4==s1.x[2] & yy1==s2.y[0] & yy4==s1.y[2])) {
			int[] xi = {x1, x3, x3, x4, x4, x2, x2, x1, x1};
			int[] yi = {y1, y1, y2, y2, y4, y4, y3, y3, y1};
			this.xUnion = xi;
			this.yUnion = yi;
			inter = new Polygon(xUnion, yUnion, 9);
			}
			else if ((xx1==s1.x[0] & xx4==s2.x[2] & yy1==s2.y[0] & yy4==s1.y[2]) 
				|| (xx1==s2.x[0] & xx4==s1.x[2] & yy1==s1.y[0] & yy4==s2.y[2])) {
			int[] xi = {x1, x2, x2, x4, x4, x3, x3, x1, x1};
			int[] yi = {y2, y2, y1, y1, y3, y3, y4, y4, y2};
			this.xUnion = xi;
			this.yUnion = yi;
			inter = new Polygon(xUnion, yUnion, 9);
			}
			else  {
			int[] xi = {x1, x2, x2, x4, x4, x3, x3, x1, x1};
			int[] yi = {y2, y2, y1, y1, y3, y3, y4, y4, y2};
			this.xUnion = xi;
			this.yUnion = yi;
			inter = new Polygon(xUnion, yUnion, 9);
			}

			return inter;
	}
}


class mySquare extends java.awt.Polygon {

	long time;
	int[] x, y;
	int n;
	Polygon p;

    public mySquare(long time, int[] x, int[] y, int n) {
        this.time = time;
		this.x = x;
        this.y = y;
		this.n = n;
    }

	public boolean inDrag(int xPos, int yPos) {
		int[] xD = {x[0]+3, x[1]-3, x[2]-3, x[3]+3, x[4]+3};
		int[] yD = {y[0]+3, y[1]+3, y[2]-3, y[3]-3, y[4]+3};
		Polygon drag = new Polygon(x, y, 5);
		return drag.inside(xPos, yPos); 
	}

	public boolean inXscale(int xPos, int yPos) {
		Rectangle r1 = new Rectangle(x[1]-3, y[1], 7, y[2]-y[1]);
		return r1.inside(xPos, yPos); 
	}

	public boolean inYscale(int xPos, int yPos) {

		Rectangle r2 = new Rectangle(x[3], y[3]-3, x[2]-x[3], 7);
		return r2.inside(xPos, yPos); 
	}

	public void drag(int xDist, int yDist) {
		for (int i=0; i<5; i++) {
			x[i] = x[i]+xDist;
			y[i] = y[i]+yDist;
		}
	}
		

	public void scale(int xScale) {

		x[0] = x[0] - xScale;
		x[1] = x[1] + xScale;
		x[2] = x[2] + xScale;
		x[3] = x[3] - xScale;
		x[4] = x[4] - xScale;

		y[0] = y[0] - xScale;
		y[1] = y[1] - xScale;
		y[2] = y[2] + xScale;
		y[3] = y[3] + xScale;
		y[4] = y[4] - xScale;
	}



	public void paint(Graphics g) {
		 g.setColor(Color.black);

		g.drawPolygon(x, y, n);
			g.setColor(Color.red);		
 g.drawRect(x[1]-3, y[1], 3, y[2]-y[1]);
		 g.drawRect(x[3], y[3]-3, x[2]-x[3], 3);

		 
    }


}
