How to calculate an angle from points?
You want the arctangent:
dy = ey - cy
dx = ex - cx
theta = arctan(dy/dx)
theta *= 180/pi // rads to degs
Erm, note that the above is obviously not compiling Javascript code. You'll have to look through documentation for the arctangent function.
Edit: Using Math.atan2(y,x) will handle all of the special cases and extra logic for 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 to degs, range (-180, 180]
//if (theta < 0) theta = 360 + theta; // range [0, 360)
return theta;
}
Runnable version of Christian's answer.
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 to degs, range (-180, 180]
return theta;
}
function angle360(cx, cy, ex, ey) {
var theta = angle(cx, cy, ex, ey); // range (-180, 180]
if (theta < 0) theta = 360 + theta; // range [0, 360)
return theta;
}
show("right", 0, 0, 1, 0);
show("top right", 0, 0, 1, 1);
show("top", 0, 0, 0, 1);
show("top left", 0, 0, -1, 1);
show("left", 0, 0, -1, 0);
show("bottom left", 0, 0, -1, -1);
show("bottom", 0, 0, 0, -1);
show("bottom right", 0, 0, 1, -1);
// IGNORE BELOW HERE (all presentational stuff)
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
padding: 2px 4px;
}
tr > td:not(:first-child) {
text-align: center;
}
tfoot {
font-style: italic;
}
<table>
<thead>
<tr><th>Direction*</th><th>Start</th><th>End</th><th>Angle</th><th>Angle 360</th></tr>
</thead>
<tfoot>
<tr><td colspan="5">* Cartesian coordinate system<br>positive x pointing right, and positive y pointing up.</td>
</tfoot>
<tbody id="angles">
</tbody>
</table>
<script>
function show(label, cx, cy, ex, ey) {
var row = "<tr>";
row += "<td>" + label + "</td>";
row += "<td>" + [cx, cy] + "</td>";
row += "<td>" + [ex, ey] + "</td>";
row += "<td>" + angle(cx, cy, ex, ey) + "</td>";
row += "<td>" + angle360(cx, cy, ex, ey) + "</td>";
row += "</tr>";
document.getElementById("angles").innerHTML += row;
}
</script>
If you're using canvas, you'll notice (if you haven't already) that canvas uses clockwise rotation(MDN) and y
axis is flipped. To get consistent results, you need to tweak your angle
function.
From time to time, I need to write this function and each time I need to look it up, because I never get to the bottom of the calculation.
While the suggested solutions work, they don't take the canvas coordinate system into consideration. Examine the following demo:
Calculate angle from points - JSFiddle
function angle(originX, originY, targetX, targetY) {
var dx = originX - targetX;
var dy = originY - targetY;
// var theta = Math.atan2(dy, dx); // [0, Ⲡ] then [-Ⲡ, 0]; clockwise; 0° = west
// theta *= 180 / Math.PI; // [0, 180] then [-180, 0]; clockwise; 0° = west
// if (theta < 0) theta += 360; // [0, 360]; clockwise; 0° = west
// var theta = Math.atan2(-dy, dx); // [0, Ⲡ] then [-Ⲡ, 0]; anticlockwise; 0° = west
// theta *= 180 / Math.PI; // [0, 180] then [-180, 0]; anticlockwise; 0° = west
// if (theta < 0) theta += 360; // [0, 360]; anticlockwise; 0° = west
// var theta = Math.atan2(dy, -dx); // [0, Ⲡ] then [-Ⲡ, 0]; anticlockwise; 0° = east
// theta *= 180 / Math.PI; // [0, 180] then [-180, 0]; anticlockwise; 0° = east
// if (theta < 0) theta += 360; // [0, 360]; anticlockwise; 0° = east
var theta = Math.atan2(-dy, -dx); // [0, Ⲡ] then [-Ⲡ, 0]; clockwise; 0° = east
theta *= 180 / Math.PI; // [0, 180] then [-180, 0]; clockwise; 0° = east
if (theta < 0) theta += 360; // [0, 360]; clockwise; 0° = east
return theta;
}