Python
numpy
programming-contest

Getting cross point in python

def getCrossPoint(s1, s2):
  base = s2[1]-s2[0]
  d1 = np.linalg.norm(np.cross(base,s1[0]-s2[0]))
  d2 = np.linalg.norm(np.cross(base,s1[1]-s2[0]))
  t = d1 / (d1+d2)
  return s1[0] + (s1[1]+s1[0])*t

n = input()
for i in range(n):
  a = []
  a.append(map(int, raw_input().split()))
  for aa in a:
    hn = np.array(aa).reshape(4,2)
    s1 = np.array([hn[0],hn[1]])
    s2 = np.array([hn[2],hn[3]])
    gcp = getCrossPoint(s1,s2)
    out = ""
    for x in gcp:
        if (out):
            out = out + " " 
        out = out + str(x)
    print(out)
3
0 0 2 0 1 1 1 -1
1.0 0.0
0 0 1 1 0 1 1 0
0.5 0.5
0 0 1 1 1 0 0 1
0.5 0.5

Refs.