// Jarett Schwartz
// COS 451 Problem Set 1
// 3-2-10

public class Point {

		private double x, y;
		public Point(double x, double y)
		{ 
			this.x = x; this.y = y; 
		}
		
		// Returns distance between two points as a double
		public double distanceTo(Point that)
		{
			double dx = this.x - that.x;
			double dy = this.y - that.y;
			return Math.sqrt(dx*dx + dy*dy);
		}
		
		// Returns the x and y values and returns a double
		public double getX()
		{
			return x;
		}
		
		public double getY()
		{
			return y;
		}
		
		// Checks to see if a point equals another point and 
		// returns a double
		public boolean equals(Point that)
		{
			if (that == null)
				return false;
			if (this.x == that.x && this.y == that.y)
				return true;
			return false;
		}
	// Finds the distance between a point A and the line
	// that passes between points B and C and returns a double
	public static double distanceLine(Point a, Point b, Point c)
	{
		double y1 = a.y;
		double x1 = a.x;
		double y2 = b.y;
		double x2 = b.x;
		double x3 = c.x;
		double y3 = c.y;
		if (x1 == x2 && y1 == y2)
			return -1;
		double dist = ((x2 - x1)*(y1-y3)-(x1-x3)*(y2-y1))/
		(Math.sqrt(Math.pow((x2-x1), 2) + Math.pow(y2-y1, 2)));
		return dist;
	}
	
	// Checks whether the line A1-B1 intersects
	// line A2-B2 and returns a boolean
	public static boolean intersect(Point a1, Point b1, Point a2, Point b2)
	{
		int ccw1 = ccw(a1, b1, a2);
		int ccw2 = ccw(a1, b1, b2);
		int ccw3 = ccw(a2, b2, a1);
		int ccw4 = ccw(a2, b2, b1);
		if (ccw1 == 0 || ccw2 == 0 || ccw3 == 0 || ccw4 == 0)
			return true;
		if (ccw1 == -ccw2 && ccw3 == -ccw4)
			return true;
		return false;
	}

	public static Point intersection(Point a1, Point b1, Point a2, Point b2)
	{
		double x1 = a1.getX();
		double x2 = b1.getX();
		double x3 = a2.getX();
		double x4 = b2.getX();
		double y1 = a1.getY();
		double y2 = b1.getY();
		double y3 = a2.getY();
		double y4 = b2.getY();
		double ua = (((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3)))/((y4 - y3) * (x2 - x1) - (x4 - x3)*(y2 - y1));
		// double ub = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3))/((y4 - y3) * (x2 - x1) - (x4 - x3)*(y2 - y1));
		double x = x1 + ua * (x2 - x1);
		double y = y1 + ua * (y2 - y1);
		return new Point(x, y);
	}
	
	public static double valueAt(Point a1, Point b1, double x)
	{
		return intersection(a1, b1, new Point(x, -10), new Point(x, 10)).getY();
	}
	
	public String toString()
	{
		return ("(" + x + ", " + y + ")"); 
	}
	
	// Calculates the ccw of Points A, B, and C in that order
	public static int ccw(Point a, Point b, Point c)
	{
		double area = (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
		if (area < 0) return 1;
		if (area > 0) return -1;
		// Checks the conditions for equaling 2
		if (((a.x < b.x && b.x < c.x) || (a.y < b.y && b.y < c.y)) 
				|| ((a.x > b.x && b.x > c.x) || (a.y > b.y && b.y > c.y)) 
				|| (a.x == b.x && a.y == b.y && (a.x != c.x || a.y != c.y)))
			return 2;
		// Checks the conditions for equaling -2
		if (((b.x < a.x && a.x < c.x) || (b.y < a.y && a.y < c.y) || 
				((b.x > a.x && a.x > c.x) || (b.y > a.y && a.y > c.y))))
			return -2;
		return 0;
				
	}
	
	
	// Test function
	// Reads 7 points from Standard Input and does a 
	// ccw computation on the first three and an
	// intersection check on the lines formed by the next
	// two pairs and prints the results.
	public static void main(String[] args)
	{
		}
}