package ifs;

import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;

public class AffineMap {
	
	private final double a, b, e, c, d, f;  // translation (e,f) and matrix with rows (a,b) and (c,d)
    public final double x0, y0, x1, y1, x2, y2, x3, y3;
    public final double area;
    public final GeneralPath outline;
    
	public AffineMap(double x0, double y0, double x1, double y1, double x2, double y2) {  // where (0,0), (1,0), and (0,1) map
		this.x0 = x0;
		this.y0 = y0;
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		e = x0;
		f = y0;
		double v1_x = x1 - x0;
		double v1_y = y1 - y0;
		double v2_x = x2 - x0;
		double v2_y = y2 - y0;
		x3 = v1_x + v2_x + x0;
		y3 = v1_y + v2_y + y0;
		a = v1_x;
		b = v2_x;
		c = v1_y;
		d = v2_y;
		area = Math.abs(a*d - b*c);
		if ( (a*a + b*b) < 1e-20 || (c*c + d*d) < 1e-20 )
			throw new IllegalArgumentException();
		outline = new GeneralPath();
		outline.moveTo((float)x0,(float)y0);
		outline.lineTo((float)x1,(float)y1);
		outline.lineTo((float)x3,(float)y3);
		outline.lineTo((float)x2,(float)y2);
		outline.closePath();
	}
	
	public void apply(Point2D.Double pt) {
		double x = pt.x;
		double y = pt.y;
		pt.x = a * x + b * y + e;
		pt.y = c * x + d * y + f;
	}
    
	public boolean containsPoint(double x, double y) {
		return outline.contains(x, y);
	}

}

