def getDistanceLP(l,p):
  return np.linalg.norm(np.cross(l[1]-l[0],p-l[0])/np.linalg.norm(l[1]-l[0]))

def getDistanceSP(s,p):
  if (np.dot(s[1]-s[0],p-s[0]) < 0): 
      return np.linalg.norm(p-s[0])
  if (np.dot(s[0]-s[1],p-s[1]) < 0): 
      return np.linalg.norm(p-s[1])
  return getDistanceLP(s,p)

def judge(p1,p2,p3,p4):
  #print(p1,p2,p3,p4)
  t1 = (p1[0] - p2[0]) * (p3[1] - p1[1]) + (p1[1] - p2[1]) * (p1[0] - p3[0])
  t2 = (p1[0] - p2[0]) * (p4[1] - p1[1]) + (p1[1] - p2[1]) * (p1[0] - p4[0])
  t3 = (p3[0] - p4[0]) * (p1[1] - p3[1]) + (p3[1] - p4[1]) * (p3[0] - p1[0])
  t4 = (p3[0] - p4[0]) * (p2[1] - p3[1]) + (p3[1] - p4[1]) * (p3[0] - p2[0])
  #print(t1,t2,t3,t4)
  #print(t1*t2,t3*t4)
  return t1*t2<=0 and t3*t4<=0

def getDistance(s1,s2):
  if (judge(s1[0],s1[1],s2[0],s2[1])):
      return 0.0 
  #print("not return 0.0")
  gd1 = getDistanceSP(s1,s2[0])
  gd2 = getDistanceSP(s1,s2[1])
  gd3 = getDistanceSP(s2,s1[0])
  gd4 = getDistanceSP(s2,s1[1])
  #print(gd1,gd2,gd3,gd4)
  return min(min(gd1,gd2),min(gd3,gd4))

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][::-1],hn[1][::-1]])
    s2 = np.array([hn[2][::-1],hn[3][::-1]])
    #print(s1,s2)
    print(getDistance(s1,s2))
    #print(getDistance(hn))
3               
0 0 1 0 0 1 1 1
1.0
0 0 1 0 2 1 1 2
1.41421356237
-1 0 1 0 0 1 0 -1
0.0

Refs.

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.