Last active
December 13, 2022 13:03
-
-
Save marmakoide/294a304b91e8c2f05235629c90a0d927 to your computer and use it in GitHub Desktop.
Pick a 3d unit vector orthogonal to an other 3d unit vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
def norm2(X): | |
return numpy.sqrt(numpy.sum(X ** 2)) | |
def normalized(X): | |
return X / norm2(X) | |
def pick_orthogonal_vector(U): | |
# Original idea from Sam Hocevar | |
# http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts | |
if numpy.fabs(U[0]) > numpy.fabs(U[2]): | |
V = numpy.array([-U[1], U[0], 0], dtype = U.dtype) | |
else: | |
V = numpy.array([0, -U[2], U[1]], dtype = U.dtype) | |
return normalized(V) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment