DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Find X, Y Intersection Of 2 Lines

02.17.2012
| 4523 views |
  • submit to reddit
        // Find X,Y intersection of 2 lines

public class findLineIntersection {

	public static void main(String args[]){
		//Ay = Bx + C
		//Dy = Ex + F
		double A,B,C,D,E,F;
		
		A=1; B=2; C=3; D=4; E=5; F=6;
		
		double X = findX(A,B,C,D,E,F);
		double Y = findY(A,B,C,X);
		
		System.out.println("("+X+","+Y+")");
	}
	
	public static double findX(double A, double B, double C, double D, double E, double F){
		double neg =-1;
		double Q,R,S;
		if (A<0 ^ D<0){
			//If A or D is negative add the values
			neg = 1;
		}
		//Eliminate Y using multiple
		double multiple = neg*(A/D);
		D = D*multiple;
		E = E*multiple;
		F = F*multiple;
		
		//Q will be 0
		Q = A+D;
		R = B+E;
		S = C+F;
		return (-S)/R;
	}
	public static double findY(double A, double B, double C, double X){
		return ((B*X) + C)/A;
	}
}