package group7.anemone.UI;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import processing.core.PApplet;

public class Utilities {
	public static double distanceBetweenPoints(double ax, double ay, double bx, double by){
		return Math.sqrt(Math.pow((float) (bx - ax), 2) + Math.pow((float) (by - ay), 2));
	}
	public static double angleBetweenPoints(double ax, double ay, double bx, double by){
		double angle = 0;
		if(bx - ax == 0){
			if(by > ay) angle = -90;
			else angle = 90;
		}else angle = Math.atan((by - ay) / (bx - ax)) * 180.0 / Math.PI;

		if(bx > ax) {
			if (by < ay) angle = 360 + angle;
		}else{
			if (by >= ay) angle = 180 + angle;
			else angle += 180;
		}

		return angle;
	}
	public static void line(PApplet canvas, int x1, int y1, int len, double angle){
		canvas.pushMatrix();
		canvas.translate(x1, y1);
		canvas.rotate((float) ((angle - 90) * Math.PI / 180));
		canvas.line(0, 0, 0, len);
		canvas.popMatrix();
	}
	public static void pointAtAngle(PApplet canvas, double x, double y, double dist, double angle){
		canvas.pushMatrix();
		canvas.translate((int) x, (int) y);
		canvas.rotate((float) ((angle - 90) * Math.PI / 180));
		canvas.point(0, (int) dist);
		canvas.popMatrix();
	}
	public static boolean isPointInBox(int x, int y, int rx, int ry, int w, int h){
		return (x >= rx && x <= rx + w && y >= ry && y <= ry + h);
	}
	
	//assumes that the lines do intersect 
	public static Point2D.Double findIntersection(Line2D.Double line1, Line2D.Double line2){
		Point2D.Double intersection = new Point2D.Double();
		//get gradients and y intercepts
		double m1 = (line1.y2-line1.y1)/(line1.x2-line1.x1);
		double c1 = line1.y1 - m1 * line1.x1;
		double m2 = (line2.y2-line2.y1)/(line2.x2-line2.x1);
		double c2 = line2.y1 - m2 * line2.x1;
		
		//if((m1 == Double.POSITIVE_INFINITY || m1 == Double.NEGATIVE_INFINITY) && (c1 == Double.POSITIVE_INFINITY || c1 == Double.NEGATIVE_INFINITY)) m1 = 0;
		

		
		
		//if the lines aren't parallel
		if(m1 != m2){
			//get x value of intersection
			double intersectX = -(c1-c2)/(m1-m2);
			if((m2 == Double.POSITIVE_INFINITY || m2 == Double.NEGATIVE_INFINITY) && (c2 == Double.POSITIVE_INFINITY || c2 == Double.NEGATIVE_INFINITY)) intersectX = line2.x1;
			
			//check it lies within both segments
			boolean withinLine1 = Math.min(line1.x1,line1.x2) < intersectX && intersectX < Math.max(line1.x1, line1.x2);
			boolean withinLine2 = Math.min(line2.x1,line2.x2) < intersectX && intersectX < Math.max(line2.x1, line2.x2);
			if((m2 == Double.POSITIVE_INFINITY || m2 == Double.NEGATIVE_INFINITY) && (c2 == Double.POSITIVE_INFINITY || c2 == Double.NEGATIVE_INFINITY)) withinLine2 = true;
			if(withinLine1  && withinLine2) {
				//calculate y value of intersection
				double intersectY = m1*intersectX + c1;
				intersection = new Point2D.Double(intersectX, intersectY);
			}
		}
		
		return intersection;
	}
	
	public static Line2D.Double generateLine(Point2D.Double point, double length, double angle){
		double endX = length * Math.cos(angle*(Math.PI/180)) + point.x;
		double endY = length * Math.sin(angle*(Math.PI/180)) + point.y;
		
		return new Line2D.Double(point,new Point2D.Double(endX,endY));
	}
}

@@ -1784,16 +1784,31 @@
 %09%0A%09%09
-//if((m1
+boolean m2Invalid = (m2
  == 
@@ -1836,17 +1836,17 @@
 ITY %7C%7C m
-1
+2
  == Doub
@@ -1869,16 +1869,56 @@
 NITY
-) && (c1
+ %7C%7C Double.isNaN(m2));%0A%09%09boolean c2Invalid = (c2
  == 
@@ -1946,17 +1946,17 @@
 ITY %7C%7C c
-1
+2
  == Doub
@@ -1979,22 +1979,66 @@
 NITY
-)) m1 = 0;%0A%09%09%0A
+ %7C%7C Double.isNaN(c2));%0A%09%09boolean c2NaN = c2 == Double.NaN;
 %0A%09%09%0A
@@ -2173,144 +2173,30 @@
 %09if(
-(
 m2
- == Double.POSITIVE_INFINITY %7C%7C m2 == Double.NEGATIVE_INFINITY)
+Invalid
  && 
-(
 c2
- == Double.POSITIVE_INFINITY %7C%7C c2 == Double.NEGATIVE_INFINITY)
+Invalid
 ) in
@@ -2497,140 +2497,28 @@
 ((m2
- == Double.POSITIVE_INFINITY %7C%7C m2 == Double.NEGATIVE_INFINITY)
+Invalid
  && 
-(
 c2
- == Double.POSITIVE_INFINITY %7C%7C c2 == Double.NEGATIVE_INFINITY
+Invalid
 )) w

Fix so agents can see left wall