Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to find the rotation of a vector

I have two 2D vectors, say u and v, defined by cartesian coordinates.

Imagine that vectors are needles of a clock. I'm looking for the fastest way to find out, using python, if v is after or before u (or in other words find out in wich half plane is v, regarding to position of u). For the purpose of the problem if vectors are aligned answer should be before.

It seems easy using some trigonometry, but I believe there should be a faster way using coordinates only.

My test case:

def after(u, v):
    """code here"""
  • after((4,2), (6, 1)) : True
  • after((4,2), (3, 3)) : False
  • after((4,2), (2, 1)) : False
  • after((4,2), (3, -3)) : True
  • after((4,2), (-2, -5)) : True
  • after((4,2), (-4, -2)) : False
like image 985
kriss Avatar asked Jan 22 '26 04:01

kriss


1 Answers

def after(u, v):
    # return sign of cross product
    return u[0]*v[1]<u[1]*v[0]

don't know if it's fast, but it is terse

like image 184
Pete Kirkham Avatar answered Jan 25 '26 18:01

Pete Kirkham