Calculate angle between two vectors python
I need to determine the angle(s) between two n-dimensional vectors in Python. For example, the input can be two lists like the following: Show
Gabriel 38k69 gold badges218 silver badges379 bronze badges asked May 13, 2010 at 14:06
1 Note: all of the other answers here will fail if the two vectors have either the same direction (ex, Here is a function which will correctly handle these cases:
answered Dec 12, 2012 at 21:47
David WoleverDavid Wolever 142k84 gold badges330 silver badges492 bronze badges 9
Note: this will fail when the vectors have either the same or the opposite direction. The correct implementation is here: https://stackoverflow.com/a/13849249/71522
answered May 13, 2010 at 14:14
Alex MartelliAlex Martelli 823k163 gold badges1202 silver badges1379 bronze badges 13 Using numpy (highly recommended), you would do:
answered May 13, 2010 at 14:13
Olivier VerdierOlivier Verdier 45k26 gold badges97 silver badges90 bronze badges 5 The other possibility is using just
and here is the output:
answered Feb 1, 2016 at 15:17
MK83MK83 1,29210 silver badges11 bronze badges 2 If you're working with 3D vectors, you can do this concisely using the toolbelt vg. It's a light layer on top of numpy.
You can also specify a viewing angle to compute the angle via projection:
Or compute the signed angle via projection:
I created the library at my last startup, where it was motivated by uses like this: simple ideas which are verbose or opaque in NumPy. answered Apr 4, 2019 at 13:29
paulmelnikowpaulmelnikow 16.6k7 gold badges61 silver badges113 bronze badges Easy way to find angle between two vectors(works for n-dimensional vector), Python code:
answered Apr 5, 2020 at 13:15
Kevin PatelKevin Patel 5168 silver badges10 bronze badges David Wolever's solution is good, but If you want to have signed angles you have to determine if a given pair is right or left handed (see wiki for further info). My solution for this is:
It's not perfect because of
this answered Jun 9, 2019 at 15:24
sgt peppersgt pepper 4247 silver badges13 bronze badges For the few who may have (due to SEO complications) ended here trying to calculate the angle between two lines in python, as in
And the use would be
answered Apr 21, 2019 at 0:53
Building on sgt pepper's great answer and adding support for aligned vectors plus adding a speedup of over 2x using Numba
And with
answered Jan 13, 2020 at 22:05
crizCraigcrizCraig 7,9485 gold badges53 silver badges52 bronze badges Use some functions from numpy.
answered Apr 25, 2021 at 12:35
Using numpy and taking care of BandGap's rounding errors:
Note, this function will throw an exception if one of the vectors has zero magnitude (divide by 0). answered Mar 12, 2013 at 23:16
PacePace 39.4k12 gold badges112 silver badges147 bronze badges 0
The traditional approach to obtaining an angle between two vectors (i.e.
This code is adapted from a Julia implementation by Jeffrey Sarnoff (MIT license), in turn based on these notes by Prof. W. Kahan (page 15). answered Jan 20 at 16:06
fakenfaken 6,3224 gold badges24 silver badges27 bronze badges 3 How do you find the angle between two vectors given two vectors?The angle between two vectors a and b is found using the formula θ = cos-1 [ (a · b) / (|a| |b|) ]. If the two vectors are equal, then substitute b = a in this formula, then we get θ = cos-1 [ (a · a) / (|a| |a|) ] = cos-1 (|a|2/|a|2) = cos-11 = 0°.
How do you find the angle in Python?angle() function is used when we want to compute the angle of the complex argument. A complex number is represented by “ x + yi ” where x and y are real number and i= (-1)^1/2 . The angle is calculated by the formula tan-1(x/y).
|