Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to calculate the angle between two points on a circle?

First and foremost let me say I am an absolutely atrocious at maths, please bear with me.

I'm trying to calculate the angle between two points on a circle, the two points being the distance the user has dragged in a certain amount of time.

This is what I have so far:

intervalId = setInterval(function(){

      if(p1x != undefined){
        p2x = Xpos;

      }

      if(p1y != undefined){
        p2y = Ypos;
      }


      if( p1x != p2x || p1y != p2y ){

        p1a = p1x - wheelMiddleVer;
        p1b = p1y - wheelMiddleHor;
        a = Math.sqrt((p1a * p1a) + (p1b * p1b));

        p2a = p2x - wheelMiddleVer;
        p2b = p2y - wheelMiddleHor;
        b = Math.sqrt((p2a * p2a) + (p2b * p2b));

        u = p1x - p2x;
        v = p1y - p2y;
        c = Math.sqrt((u * u) + (v * v));


      }

      p1x = Xpos;
      p1y = Ypos;


  }, 1000);

I'm not sure how to finish it. I have tried using the cos A = (b^2 + c^2 - a^2)/2bc formula but it's not worked out for me. I will appreciate your input on this. If I can make the question clearer let me know.

like image 473
styke Avatar asked Mar 10 '13 12:03

styke


1 Answers

First of all, keep in mind that most of the languages have an invert Y axis. Meaning the higher the point on the scale -> the smaller it Y value is.

As for your question, you should use arctan:

var angle = Math.atan((p1y - p2y) / (p2x - p1x)) * (180 / Math.PI);
like image 53
YuriG Avatar answered Sep 30 '22 16:09

YuriG