javascript - How to calculate an angle from points? -


i want simple solution calculate angle of line (like pointer of clock).

i have 2 points:

cx, cy - center of line. ex, ey - end of line.  result angle (0 <= < 360). 

which function able provide value?

you want arctangent:

dy = ey - cy dx = ex - cx theta = arctan(dy/dx) theta *= 180/pi // rads degs 

erm, note above not compiling javascript code. you'll have through documentation arctangent function.

edit: using math.atan2(y,x) handle of special cases , logic you:

function angle(cx, cy, ex, ey) {   var dy = ey - cy;   var dx = ex - cx;   var theta = math.atan2(dy, dx); // range (-pi, pi]   theta *= 180 / math.pi; // rads degs, range (-180, 180]   //if (theta < 0) theta = 360 + theta; // range [0, 360)   return theta; } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -