def DoSimplex(points,D):
O=vector([0,0])
if len(points)==1:
D=O-points[0]
if len(points)==2:
if (points[0]-points[1])*(O-points[1])<0:
D=O-points[1]
points=[points[1]]
else:
D=(O-points[1]-projection(O-points[1],points[0]-points[1]))
if len(points)==3:
V1=points[0]-points[2]
V2=points[1]-points[2]
if vector([V1[1],-V1[0]])*V2>0:
temp=points[0]
points[0]=points[1]
points[1]=temp
V1=points[0]-points[2]
V2=points[1]-points[2]
V0=O-points[2]
if vector([V1[1],-V1[0]])*V0<0:
if (vector([-V2[1],V2[0]])*V0)<0:
return (true,points,D)
if V2*V0>0:
points=[points[2],points[1]]
D=(V0-projection(V0,V2))
else:
if V1*V0>0:
points=[points[2],points[0]]
D=(V0-projection(V0,V1))
else:
points=[points[1]]
D=V0
else:
if V1*V0>0:
points=[points[2],points[0]]
D=(V0-projection(V0,V1))
else:
if V2*V0>0:
points=[points[2],points[1]]
D=(V0-projection(V0,V2))
else:
points=[points[0]]
D=V0
if D==O:
return (true,points,D)
else:
return (false,points,D)