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# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Aoj.CGL.Lib;

namespace Aoj.CGL.Chapter2C
{
    class Program
    {
        public static void Solve()
        {
            int q = int.Parse(Console.ReadLine());
 
            for (int i = 0; i < q; i++)
            {
                int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

                var seg1 = new Segment(input[0],input[1],input[2],input[3]);
                var seg2 = new Segment(input[4],input[5],input[6],input[7]);

                var crossPoint = MathUtil.GetCrossPoint(seg1, seg2);

                Console.WriteLine("{0:0.0000000000} {1:0.0000000000}", crossPoint.X, crossPoint.Y);
            }
        }

        public static void Main()
        {
            // Console.SetIn(new System.IO.StreamReader("ComputationalGeometry/Input/02_Segment_Line_C_01.txt"));
            // var sw = new Stopwatch();
            // sw.Start();

            Solve();

            // Console.WriteLine("{0}ms", sw.ElapsedMilliseconds);
        }
    }
}

namespace Aoj.CGL.Lib
{
    using Line = Segment;

    using Point = Vector;

    using Polygon = List<Vector>;

    public struct Segment
    {
        private Point _p1;
        private Point _p2;

        public Point P1 { get { return _p1; } }
        public Point P2 { get { return _p2; } }

        public Segment(Point p1, Point p2)
        {
            _p1 = p1;
            _p2 = p2;
        }

        public Segment(double x1, double y1, double x2, double y2)
        {
            _p1 = new Point(x1, y1);
            _p2 = new Point(x2, y2);
        }

        public override string ToString()
        {
            return string.Format("P1x:{0},P1y:{1}, P2x:{2},P2y:{3}", P1.X, P1.Y, P2.X, P2.Y);
        }
    }

    public struct Circle
    {
        private Point _p;
        private double _r;

        public Point P { get { return _p; } }
        public double R { get { return _r; } }

        public Circle(Point p, double r)
        {
            _p = p;
            _r = r;
        }

        public override string ToString()
        {
            return string.Format("px:{0},py:{1},r:{2}", P.X, P.Y, R);
        }
    }

    public struct Vector
    {
        private double _x;
        private double _y;

        public double X { get { return _x; } }
        public double Y { get { return _y; } }

        public Vector(double x, double y)
        {
            _x = x;
            _y = y;
        }

        public double Norm()
        {
            return X * X + Y * Y;
        }

        public double Abs()
        {
            return Math.Sqrt(Norm());
        }

        public Vector Normalize()
        {
            double mag = Abs();
            return new Vector(X * mag, Y * mag);
        }

        public static Vector operator +(Vector v1, Vector v2)
        {
            return new Vector(v1.X + v2.X, v1.Y + v2.Y);
        }

        public static Vector operator -(Vector v1, Vector v2)
        {
            return new Vector(v1.X - v2.X, v1.Y - v2.Y);
        }

        public static Vector operator *(Vector v, double k)
        {
            return new Vector(v.X * k, v.Y * k);
        }

        public static Vector operator *(double k, Vector v)
        {
            return v * k;
        }

        public override string ToString()
        {
            return string.Format("x:{0},y:{1}", X, Y);
        }
    }

    public static class MathUtil
    {
        public static double Dot(Vector v1, Vector v2)
        {
            return v1.X * v2.X + v1.Y * v2.Y;
        }

        public static double Cross(Vector v1, Vector v2)
        {
            return v1.X * v2.Y - v1.Y * v2.X;
        }

        public static bool IsOrthogonal(Vector v1, Vector v2)
        {
            return Dot(v1, v2) == 0.0;
        }

        public static bool IsOrthogonal(Point a1, Point a2, Point b1, Point b2)
        {
            return IsOrthogonal(a1 - a2, b1 - b2);
        }

        public static bool IsOrthogonal(Segment s1, Segment s2)
        {
            return IsOrthogonal((Vector)(s1.P1 - s1.P2), (Vector)(s2.P1 - s2.P2));
        }

        public static bool IsParallel(Vector v1, Vector v2)
        {
            return Cross(v1, v2) == 0.0;
        }

        public static bool IsParallel(Point a1, Point a2, Point b1, Point b2)
        {
            return IsParallel(a1 - a2, b1 - b2);
        }
        public static bool IsParallel(Segment s1, Segment s2)
        {
            return IsParallel((Vector)(s1.P1 - s1.P2), (Vector)(s2.P1 - s2.P2));
        }

        public static Vector Project(Vector v, Vector onNormal)
        {
            // mag = |b|cos?? / |a|
            //     = |a||b|cos?? / |a|^2
            double mag = Dot(v, onNormal) / onNormal.Norm();
            return new Vector(onNormal.X * mag, onNormal.Y * mag);
        }

        public static Point Project(Point p, Segment s)
        {
            Vector v = p - s.P1;
            Vector baseV = s.P2 - s.P1;
            Vector project = Project(v, baseV);
            return s.P1 + project;
        }

        public static Point Reflect(Point p, Segment s)
        {
            Point project = Project(p, s);
            return p + (project - p) * 2.0;
        }

        // ??????????????????????¨??????? ~ ????¨????????????¢???????????°???
        public const int OnLineBack = 2;
        public const int CounterClockwise = 1;
        public const int OnSegment = 0;
        public const int Clockwise = -1;
        public const int OnLineFront = -2;

        /// <summary>
        /// ??????p0p1??????????????????p0p2??????????¨??????? ~ ????¨?????????????????????????????????????int??§?????????
        /// </summary>
        /// <param name="p0"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static int Ccw(Point p0, Point p1, Point p2)
        {
            var baseV = p1 - p0;
            var v = p2 - p0;
            if (Cross(baseV, v) > 0)
                return CounterClockwise;
            else if (Cross(baseV, v) < 0)
                return Clockwise;
            else if (Dot(baseV, v) < 0)
                return OnLineBack;
            else if (baseV.Norm() < v.Norm())
                return OnLineFront;
            return OnSegment;
        }

        /// <summary>
        /// ????????????????????´??????????????????????¢?????????????
        /// http://www5d.biglobe.ne.jp/~tomoya03/shtml/algorithm/Intersection.htm
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p3"></param>
        /// <param name="p4"></param>
        /// <returns></returns>
        public static bool Intersect(Segment s1, Segment s2)
        {
            return Intersect(s1.P1, s1.P2, s2.P1, s2.P2);
        }

        public static bool Intersect(Point p1, Point p2, Point p3, Point p4)
        {
            return (
                Ccw(p1, p2, p3) * Ccw(p1, p2, p4) <= 0 &&
                Ccw(p3, p4, p1) * Ccw(p3, p4, p2) <= 0
            );
        }

        public static double GetDistancePP(Point p1, Point p2)
        {
            return (p1 - p2).Abs();
        }

        public static double GetDistancePL(Point p, Line l)
        {
            var v1 = p - l.P1;
            var v2 = l.P2 - l.P1;

            var area = Math.Abs(Cross(v1, v2));
            var bottom = v2.Abs();
            return area / bottom;
        }

        public static double GetDistancePS(Point p, Segment s)
        {
            // p??¨???????????????????????????????????????????§????90???????????§????????´??????????????¨???????????¢???
            if (Dot(p - s.P1, s.P2 - s.P1) < 0.0)
                return GetDistancePP(p, s.P1);
            if (Dot(p - s.P2, s.P1 - s.P2) < 0.0)
                return GetDistancePP(p, s.P2);

            // ????????\??????????????¨??´???????????¢???
            return GetDistancePL(p, s);
        }
        public static double GetDistanceSS(Segment s1, Segment s2)
        {
            // ?????????????????????????????¢???0???
            if (Intersect(s1, s2))
                return 0.0;

            // ????????\????????´??????????????????????????¨????????????????????¢?????????????????????????°????????????????
            return Math.Min(
                Math.Min(GetDistancePS(s1.P1, s2), GetDistancePS(s1.P2, s2)),
                Math.Min(GetDistancePS(s2.P1, s1), GetDistancePS(s2.P2, s1))
            );
        }
        public static double GetDistanceLL(Line l1, Line l2)
        {
            // ?????????????????????????????¢???0???
            if (! IsParallel(l1, l2))
                return 0.0;
            return GetDistancePL(l1.P1, l2);
        }

        public static Point GetCrossPoint(Segment s1, Segment s2)
        {
            if (! Intersect(s1, s2))
                throw new ArgumentException("");

            var baseV = s1.P2 - s1.P1;
            var bottom = baseV.Abs();

            var v1 = s2.P1 - s1.P1;
            var h1 = Math.Abs(Cross(baseV, v1)) / bottom;

            var v2 = s2.P2 - s1.P1;
            var h2 = Math.Abs(Cross(baseV, v2)) / bottom;

            return s2.P1 + (s2.P2 - s2.P1) * (h1 / (h1 + h2));
        }
    }
}