How to find the clockwise angle between two vectors in python?












0















I want to find the clockwise angle between two vectors in python
the angle should be in range of (-90,90)



what is the equation/code to calculate the angle ?



class Vect:
def __init__(self, a, b):
self.a = a
self.b = b

def findClockwiseAngle(self, other):
## how to compute ??
pass


vector1 = Vect(a1,b1) ## a1*i + b1*j
vector2 = Vect(a2,b2) ## a2*i + b2*j
angle = vect1.findClockwiseAngle(vect2)









share|improve this question

























  • Try cosine similarity?

    – coldspeed
    Dec 29 '18 at 13:53
















0















I want to find the clockwise angle between two vectors in python
the angle should be in range of (-90,90)



what is the equation/code to calculate the angle ?



class Vect:
def __init__(self, a, b):
self.a = a
self.b = b

def findClockwiseAngle(self, other):
## how to compute ??
pass


vector1 = Vect(a1,b1) ## a1*i + b1*j
vector2 = Vect(a2,b2) ## a2*i + b2*j
angle = vect1.findClockwiseAngle(vect2)









share|improve this question

























  • Try cosine similarity?

    – coldspeed
    Dec 29 '18 at 13:53














0












0








0








I want to find the clockwise angle between two vectors in python
the angle should be in range of (-90,90)



what is the equation/code to calculate the angle ?



class Vect:
def __init__(self, a, b):
self.a = a
self.b = b

def findClockwiseAngle(self, other):
## how to compute ??
pass


vector1 = Vect(a1,b1) ## a1*i + b1*j
vector2 = Vect(a2,b2) ## a2*i + b2*j
angle = vect1.findClockwiseAngle(vect2)









share|improve this question
















I want to find the clockwise angle between two vectors in python
the angle should be in range of (-90,90)



what is the equation/code to calculate the angle ?



class Vect:
def __init__(self, a, b):
self.a = a
self.b = b

def findClockwiseAngle(self, other):
## how to compute ??
pass


vector1 = Vect(a1,b1) ## a1*i + b1*j
vector2 = Vect(a2,b2) ## a2*i + b2*j
angle = vect1.findClockwiseAngle(vect2)






python-3.x computational-geometry






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 15:45









unutbu

546k10111681235




546k10111681235










asked Dec 29 '18 at 13:46









thakee natheesthakee nathees

205




205













  • Try cosine similarity?

    – coldspeed
    Dec 29 '18 at 13:53



















  • Try cosine similarity?

    – coldspeed
    Dec 29 '18 at 13:53

















Try cosine similarity?

– coldspeed
Dec 29 '18 at 13:53





Try cosine similarity?

– coldspeed
Dec 29 '18 at 13:53












1 Answer
1






active

oldest

votes


















0














Vector geometry provides (at least) two useful formulas for finding the angle between two vectors:




  • the dot product formula




where a · b can be computed using






  • and the cross-product formula:




where





and since our vectors are two dimensional, we can take a3 and b3 (the components in the z-axis direction) equal to 0. This simplifies the formula even further:



|a x b| = |a1 * b2 - a2 * b1| = |a| * |b| * sin(ϴ)


The ϴs is these two formulas, however, have different interpretations.
With the dot product, the angle is the included angle between the two vectors -- and thus always a value between 0 and pi.



With the cross product, the angle is measured in the counterclockwise direction from a to to b. Since you are looking for the angle measured in the clockwise direction, you would simply reverse the sign of the angle obtained using the cross product formula.



In Python, math.asin returns values in the range [-pi/2, pi/2], whereas math.acos returns values in the range [0, pi]. Since you want angles in the range [-pi/2, pi/2] (in radians), the cross-product formula seems to be the more promising candidate:



import math

class Vect:

def __init__(self, a, b):
self.a = a
self.b = b

def findClockwiseAngle(self, other):
# using cross-product formula
return -math.degrees(math.asin((self.a * other.b - self.b * other.a)/(self.length()*other.length())))
# the dot-product formula, left here just for comparison (does not return angles in the desired range)
# return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))

def length(self):
return math.sqrt(self.a**2 + self.b**2)

vector1 = Vect(2,0)

N = 12
theta = [i * 2 * math.pi / N for i in range(N)]
result =
for t in theta:
vector2 = Vect(math.cos(t), math.sin(t)) ## a2*i + b2*j
angle = vector1.findClockwiseAngle(vector2)
result.append((math.degrees(t), angle))

print('{:>10}{:>10}'.format('t', 'angle'))
print('n'.join(['{:>10.2f}{:>10.2f}'.format(*pair) for pair in result]))


prints



     t     angle
0.00 -0.00
30.00 -30.00
60.00 -60.00
90.00 -90.00
120.00 -60.00
150.00 -30.00
180.00 -0.00
210.00 30.00
240.00 60.00
270.00 90.00
300.00 60.00
330.00 30.00


Above, t is the angle from vector1 to vector2 measured in the counterclockwise direction in the range (0, 360) degrees. angle is the angle from vector1 to vector2 measured in the clockwise direction and in the range (-90, 90) degrees.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53970131%2fhow-to-find-the-clockwise-angle-between-two-vectors-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Vector geometry provides (at least) two useful formulas for finding the angle between two vectors:




    • the dot product formula




    where a · b can be computed using






    • and the cross-product formula:




    where





    and since our vectors are two dimensional, we can take a3 and b3 (the components in the z-axis direction) equal to 0. This simplifies the formula even further:



    |a x b| = |a1 * b2 - a2 * b1| = |a| * |b| * sin(ϴ)


    The ϴs is these two formulas, however, have different interpretations.
    With the dot product, the angle is the included angle between the two vectors -- and thus always a value between 0 and pi.



    With the cross product, the angle is measured in the counterclockwise direction from a to to b. Since you are looking for the angle measured in the clockwise direction, you would simply reverse the sign of the angle obtained using the cross product formula.



    In Python, math.asin returns values in the range [-pi/2, pi/2], whereas math.acos returns values in the range [0, pi]. Since you want angles in the range [-pi/2, pi/2] (in radians), the cross-product formula seems to be the more promising candidate:



    import math

    class Vect:

    def __init__(self, a, b):
    self.a = a
    self.b = b

    def findClockwiseAngle(self, other):
    # using cross-product formula
    return -math.degrees(math.asin((self.a * other.b - self.b * other.a)/(self.length()*other.length())))
    # the dot-product formula, left here just for comparison (does not return angles in the desired range)
    # return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))

    def length(self):
    return math.sqrt(self.a**2 + self.b**2)

    vector1 = Vect(2,0)

    N = 12
    theta = [i * 2 * math.pi / N for i in range(N)]
    result =
    for t in theta:
    vector2 = Vect(math.cos(t), math.sin(t)) ## a2*i + b2*j
    angle = vector1.findClockwiseAngle(vector2)
    result.append((math.degrees(t), angle))

    print('{:>10}{:>10}'.format('t', 'angle'))
    print('n'.join(['{:>10.2f}{:>10.2f}'.format(*pair) for pair in result]))


    prints



         t     angle
    0.00 -0.00
    30.00 -30.00
    60.00 -60.00
    90.00 -90.00
    120.00 -60.00
    150.00 -30.00
    180.00 -0.00
    210.00 30.00
    240.00 60.00
    270.00 90.00
    300.00 60.00
    330.00 30.00


    Above, t is the angle from vector1 to vector2 measured in the counterclockwise direction in the range (0, 360) degrees. angle is the angle from vector1 to vector2 measured in the clockwise direction and in the range (-90, 90) degrees.






    share|improve this answer






























      0














      Vector geometry provides (at least) two useful formulas for finding the angle between two vectors:




      • the dot product formula




      where a · b can be computed using






      • and the cross-product formula:




      where





      and since our vectors are two dimensional, we can take a3 and b3 (the components in the z-axis direction) equal to 0. This simplifies the formula even further:



      |a x b| = |a1 * b2 - a2 * b1| = |a| * |b| * sin(ϴ)


      The ϴs is these two formulas, however, have different interpretations.
      With the dot product, the angle is the included angle between the two vectors -- and thus always a value between 0 and pi.



      With the cross product, the angle is measured in the counterclockwise direction from a to to b. Since you are looking for the angle measured in the clockwise direction, you would simply reverse the sign of the angle obtained using the cross product formula.



      In Python, math.asin returns values in the range [-pi/2, pi/2], whereas math.acos returns values in the range [0, pi]. Since you want angles in the range [-pi/2, pi/2] (in radians), the cross-product formula seems to be the more promising candidate:



      import math

      class Vect:

      def __init__(self, a, b):
      self.a = a
      self.b = b

      def findClockwiseAngle(self, other):
      # using cross-product formula
      return -math.degrees(math.asin((self.a * other.b - self.b * other.a)/(self.length()*other.length())))
      # the dot-product formula, left here just for comparison (does not return angles in the desired range)
      # return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))

      def length(self):
      return math.sqrt(self.a**2 + self.b**2)

      vector1 = Vect(2,0)

      N = 12
      theta = [i * 2 * math.pi / N for i in range(N)]
      result =
      for t in theta:
      vector2 = Vect(math.cos(t), math.sin(t)) ## a2*i + b2*j
      angle = vector1.findClockwiseAngle(vector2)
      result.append((math.degrees(t), angle))

      print('{:>10}{:>10}'.format('t', 'angle'))
      print('n'.join(['{:>10.2f}{:>10.2f}'.format(*pair) for pair in result]))


      prints



           t     angle
      0.00 -0.00
      30.00 -30.00
      60.00 -60.00
      90.00 -90.00
      120.00 -60.00
      150.00 -30.00
      180.00 -0.00
      210.00 30.00
      240.00 60.00
      270.00 90.00
      300.00 60.00
      330.00 30.00


      Above, t is the angle from vector1 to vector2 measured in the counterclockwise direction in the range (0, 360) degrees. angle is the angle from vector1 to vector2 measured in the clockwise direction and in the range (-90, 90) degrees.






      share|improve this answer




























        0












        0








        0







        Vector geometry provides (at least) two useful formulas for finding the angle between two vectors:




        • the dot product formula




        where a · b can be computed using






        • and the cross-product formula:




        where





        and since our vectors are two dimensional, we can take a3 and b3 (the components in the z-axis direction) equal to 0. This simplifies the formula even further:



        |a x b| = |a1 * b2 - a2 * b1| = |a| * |b| * sin(ϴ)


        The ϴs is these two formulas, however, have different interpretations.
        With the dot product, the angle is the included angle between the two vectors -- and thus always a value between 0 and pi.



        With the cross product, the angle is measured in the counterclockwise direction from a to to b. Since you are looking for the angle measured in the clockwise direction, you would simply reverse the sign of the angle obtained using the cross product formula.



        In Python, math.asin returns values in the range [-pi/2, pi/2], whereas math.acos returns values in the range [0, pi]. Since you want angles in the range [-pi/2, pi/2] (in radians), the cross-product formula seems to be the more promising candidate:



        import math

        class Vect:

        def __init__(self, a, b):
        self.a = a
        self.b = b

        def findClockwiseAngle(self, other):
        # using cross-product formula
        return -math.degrees(math.asin((self.a * other.b - self.b * other.a)/(self.length()*other.length())))
        # the dot-product formula, left here just for comparison (does not return angles in the desired range)
        # return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))

        def length(self):
        return math.sqrt(self.a**2 + self.b**2)

        vector1 = Vect(2,0)

        N = 12
        theta = [i * 2 * math.pi / N for i in range(N)]
        result =
        for t in theta:
        vector2 = Vect(math.cos(t), math.sin(t)) ## a2*i + b2*j
        angle = vector1.findClockwiseAngle(vector2)
        result.append((math.degrees(t), angle))

        print('{:>10}{:>10}'.format('t', 'angle'))
        print('n'.join(['{:>10.2f}{:>10.2f}'.format(*pair) for pair in result]))


        prints



             t     angle
        0.00 -0.00
        30.00 -30.00
        60.00 -60.00
        90.00 -90.00
        120.00 -60.00
        150.00 -30.00
        180.00 -0.00
        210.00 30.00
        240.00 60.00
        270.00 90.00
        300.00 60.00
        330.00 30.00


        Above, t is the angle from vector1 to vector2 measured in the counterclockwise direction in the range (0, 360) degrees. angle is the angle from vector1 to vector2 measured in the clockwise direction and in the range (-90, 90) degrees.






        share|improve this answer















        Vector geometry provides (at least) two useful formulas for finding the angle between two vectors:




        • the dot product formula




        where a · b can be computed using






        • and the cross-product formula:




        where





        and since our vectors are two dimensional, we can take a3 and b3 (the components in the z-axis direction) equal to 0. This simplifies the formula even further:



        |a x b| = |a1 * b2 - a2 * b1| = |a| * |b| * sin(ϴ)


        The ϴs is these two formulas, however, have different interpretations.
        With the dot product, the angle is the included angle between the two vectors -- and thus always a value between 0 and pi.



        With the cross product, the angle is measured in the counterclockwise direction from a to to b. Since you are looking for the angle measured in the clockwise direction, you would simply reverse the sign of the angle obtained using the cross product formula.



        In Python, math.asin returns values in the range [-pi/2, pi/2], whereas math.acos returns values in the range [0, pi]. Since you want angles in the range [-pi/2, pi/2] (in radians), the cross-product formula seems to be the more promising candidate:



        import math

        class Vect:

        def __init__(self, a, b):
        self.a = a
        self.b = b

        def findClockwiseAngle(self, other):
        # using cross-product formula
        return -math.degrees(math.asin((self.a * other.b - self.b * other.a)/(self.length()*other.length())))
        # the dot-product formula, left here just for comparison (does not return angles in the desired range)
        # return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))

        def length(self):
        return math.sqrt(self.a**2 + self.b**2)

        vector1 = Vect(2,0)

        N = 12
        theta = [i * 2 * math.pi / N for i in range(N)]
        result =
        for t in theta:
        vector2 = Vect(math.cos(t), math.sin(t)) ## a2*i + b2*j
        angle = vector1.findClockwiseAngle(vector2)
        result.append((math.degrees(t), angle))

        print('{:>10}{:>10}'.format('t', 'angle'))
        print('n'.join(['{:>10.2f}{:>10.2f}'.format(*pair) for pair in result]))


        prints



             t     angle
        0.00 -0.00
        30.00 -30.00
        60.00 -60.00
        90.00 -90.00
        120.00 -60.00
        150.00 -30.00
        180.00 -0.00
        210.00 30.00
        240.00 60.00
        270.00 90.00
        300.00 60.00
        330.00 30.00


        Above, t is the angle from vector1 to vector2 measured in the counterclockwise direction in the range (0, 360) degrees. angle is the angle from vector1 to vector2 measured in the clockwise direction and in the range (-90, 90) degrees.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 29 '18 at 22:15

























        answered Dec 29 '18 at 15:14









        unutbuunutbu

        546k10111681235




        546k10111681235






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53970131%2fhow-to-find-the-clockwise-angle-between-two-vectors-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            compose and upload a new article using a custom form

            How to correct the classpath of spring boot application so that it contains a single, compatible version of...