Problem:
Cross Point

For given two segments s1 and s2, print the coordinate of the cross point of them.

s1 is formed by end points p0 and p1, and
s2 is formed by end points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of end points of s1 and s2 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print the coordinate of the cross point. The output values should be in a decimal fraction with an error less than 0.00000001.

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

The given segments have a cross point and are not in parallel.

Sample Input

3
0 0 2 0 1 1 1 -1
0 0 1 1 0 1 1 0
0 0 1 1 1 0 0 1

Sample Output

1.0000000000 0.0000000000
0.5000000000 0.5000000000
0.5000000000 0.5000000000

Solution in JAVA (judge verdict: Accepted)

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int q = scan.nextInt();
		for(int i = 0; i < q; i++) {
			double[]p0 = new double[2];
			double[]p1 = new double[2];
			double[]p2 = new double[2];
			double[]p3 = new double[2];
			p0[0] = scan.nextInt();
			p0[1] = scan.nextInt();
			p1[0] = scan.nextInt();
			p1[1] = scan.nextInt();
			p2[0] = scan.nextInt();
			p2[1] = scan.nextInt();
			p3[0] = scan.nextInt();
			p3[1] = scan.nextInt();
			double[]h1 = project(p0, p1, p2);
			double[]h2 = project(p0, p1, p3);
			double d1 = Math.sqrt(norm(vec(p2, h1)));
			double d2 = Math.sqrt(norm(vec(p3, h2)));
			double t = d1 / (d1 + d2);
			double[]u = vec(p2, p3);
			double x = u[0] * t + p2[0];
			double y = u[1] * t + p2[1];
			//double[]p = getCrossPoint(p0, p1, p2, p3);
			System.out.println(x + " " + y);
		}
		scan.close();
	}
	static double[] getCrossPoint(double[]p1, double[]p2, double[]p3, double[]p4) {
		double[]base = vec(p3, p4);
		double d1 = Math.sqrt(Math.abs(cross(base, vec(p3, p1))));
		double d2 = Math.sqrt(Math.abs(cross(base, vec(p3, p2))));
		double t = d1 / (d1 + d2);
		double[]p = {p1[0] + (p3[0] - p1[0]) * t, p1[1] + (p3[1] - p1[1]) * t};
		return p;
	}
	static double[] project(double[]p1, double[]p2, double[]p3) {
		double[]base = vec(p1, p2);
		double[]u = vec(p1, p3);
		double r = dot(u, base) / norm(base);
		double x = p1[0] + base[0]  * r;
		double y = p1[1] + base[1]  * r;
		double[]h = {x, y};
		return h;
	}
	static double norm(double[]p) {
		return p[0] * p[0] + p[1] * p[1];
	}
	static double[] vec(double[]p1, double[]p2) {
		double[]u = {p2[0] - p1[0], p2[1] - p1[1]};
		return u;
	}
	static double dot(double[]u, double[]v) {
		return u[0] * v[0] + u[1] * v[1];
	}
	static double cross(double[]u, double[]v) {
		return u[0] * v[1] - u[1] * v[0];
	}
}