package automatvgi;

import java.awt.Graphics;

import automatvgi.tools.Point;
import automatvgi.tools.Vector;


public class Projection{
	private Vector translation=new Vector(0,0);
	private Point centre=null;
	private Vector vi=new Vector(20,0);
	private double arr=.5;
	
	public double getArr() {
		return arr;
	}

	public Vector getTranslation() {
		return translation;
	}

	public Vector getVi() {
		return vi;
	}

	public Projection(){}
	
	public void setCentre(Point centre){
		this.centre = centre.addTo(translation);
	}
	
	public double getExactAbs(Point p){
		return p.getX()*vi.getX()+p.getY()*vi.getY()+centre.getX();		
	}
	
	public double getExactOrd(Point p){
		return p.getX()*vi.getY()-p.getY()*vi.getX()-centre.getY();		
	}
	
	public int getAbs(Point p){
		return (int)getExactAbs(p);
	}
	
	public int getOrd(Point p){
		return (int)getExactOrd(p);
	}
	
	public Point getExactPoint(int x,int y){
		double mx=((x-centre.getX())*vi.getX()+(y+centre.getY())*vi.getY())/(vi.getX()*vi.getX()+vi.getY()*vi.getY()),
		       my=((x-centre.getX())*vi.getY()-(y+centre.getY())*vi.getX())/(vi.getX()*vi.getX()+vi.getY()*vi.getY());
		return new Point(mx,my);
	}

	public Point getPoint(int x,int y){
		Point p=getExactPoint(x,y);
		double mx=p.getX(), my=p.getY();
		//if(mx>0) mx+=.5; else mx-=.5;
		//if(my>0) my+=.5; else my-=.5;
		mx=((int)(mx/arr))*arr; my=((int)(my/arr))*arr;
		double d=2*(mx-p.getX());
		if(d>arr) mx-=arr;
		else if(d<-arr) mx+=arr;
		d=2*(my-p.getY());
		if(d>arr) my-=arr;
		else if(d<-arr) my+=arr;
		return new Point(mx,my);
	}

	public void drawLine(Point a,Point b,Graphics g){
		g.drawLine(getAbs(a),getOrd(a),
				   getAbs(b),getOrd(b));		
	}
	
	public int cmToPix(double l){
		return (int)(l*vi.norm());
	}
	
	public int mmToPix(double l){
		return (int)(l*vi.norm()/10);
	}

	
}
