package zh2015.vorosOktober;

import java.util.Random;

public class Ship implements Runnable {
	
	public double speed = 30;
	
	class coord {
		double x, y;
		public coord(double x, double y){
			this.x = x;
			this.y = y;
		}
		public coord() {
			Random rand = new Random();
			x = rand.nextInt(100);
			y = rand.nextInt(100);
		}
		public double distance (coord other) {
			return Math.sqrt((x-other.x)*(x-other.x)+(y-other.y)*(y-other.y));
		}
		public coord innerProduct(coord other) {
			x = x*other.x;
			y = y*other.y;
			return this;
		}
		public coord normalize() {
			double length = Math.sqrt(x*x+ y*y);
			x /= length;
			y /= length;
			return this;
		}
		public coord add(coord other) {
			x += other.x;
			y += other.y;
			return this;
		}
		public coord multiply(double d) {
			x *= d;
			y *= d;
			return this;
		}
		public String toString() {
			return x + " " + y;
		}
	}
	
	coord substract(coord c1, coord c2) {
		return new coord(c1.x-c2.x, c1.y-c2.y);
	}
	
	public coord position, goal;
	
	Ship () {
		position = new coord();
		goal = new coord();
	}
	
	protected void move() {
		if (position.distance(goal) < 30)
			goal = new coord();
		position.add(substract(goal, position).normalize().multiply(speed));
	}

	@Override
	public void run() {
		while (!Main.success) {
			move();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
