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 C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
bool eq(double a, double b) { return fabs(a - b) < EPS; }

struct point
{
	double x, y;
	point(double x = 0, double y = 0) :x(x), y(y) {};

	point operator + (point p) { return point(x + p.x, y + p.y); }
	point operator - (point p) { return point(x - p.x, y - p.y); }
	point operator * (double s) { return point(x * s, y * s); }
	point operator / (double s) { return point(x / s, y / s); }

	double norm() { return x*x + y*y; }
	double abs() { return sqrt(norm()); }

	bool operator < (const point &p) const
	{
		return x != p.x ? x < p.x : y < p.y;
	}

	bool operator == (const point &p) const
	{
		return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
	}

};
typedef vector<point> polygon;

double dot(point a, point b)
{
	return a.x*b.x + a.y*b.y;
}

double cross(point a, point b)
{
	return a.x*b.y - a.y*b.x;
}

int ccw(point a, point b, point c)
{
	b = b - a; c = c - a;
	if (cross(b, c) > EPS) return 1;//???????¨????????????????a,b,c??¨?????¶
	if (cross(b, c) < -EPS) return -1;//???????¨????????????????a,c,b??¨?????¶
	if (dot(b, c) < 0) return 2;//??´?????????c,a,b??????????????¶
	if (b.norm() < c.norm()) return -2;//??´?????????a,b,c??????????????¶
	return 0;//??´?????????a,c,b??????????????¶
}

struct line
{
	point a, b;
	line(double x1 = 0, double y1 = 0, double x2 = 0, double y2 = 0) :a(point(x1, y1)), b(point(x2, y2)) {};

	point univec() { return (b - a) / (b - a).norm(); }
};

point cross_point(line s1, line s2)
{
	point sv = s1.b - s1.a;
	point tv = s2.b - s2.a;
	return s1.a + sv*cross(tv, s2.a - s1.a) / cross(tv, sv);
}

line perbis(line l)
{
	double x = (l.a.x + l.b.x) / 2;
	double y = (l.a.y + l.b.y) / 2;
	point s(x, y);
	point v = l.b - s;
	point w(-v.y, v.x);
	point p = s + w;
	return line(s.x, s.y, p.x, p.y);
}

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		point p[4];
		REP(i, 4)
		{
			double x, y;
			cin >> x >> y;
			p[i] = point(x, y);
		}
		line s(p[0].x, p[0].y, p[1].x, p[1].y);
		line t(p[2].x, p[2].y, p[3].x, p[3].y);
		point ans = cross_point(s, t);
		cout << D10 << ans.x << " " << ans.y << endl;
	}


/*	while (n--)
	{
		double x[3], y[3];
		REP(i, 3) cin >> x[i] >> y[i];
		line a = perbis(line(x[0], y[0], x[1], y[1]));
		line b = perbis(line(x[0], y[0], x[2], y[2]));
		point ans = cross_point(a, b);
		cout << D10 << ans.x << " " << ans.y << " " << (ans - point(x[0], y[0])).abs() << endl;
	}
	*/
	return 0;
}