### Thread: Point rotation function in javascript

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2012
Posts
32
Rep Power
7

#### Point rotation function in javascript

Writing a function to rotate a point in javascript:

Code:
```<!doctype html>

<html>

<script>
// ==============================================================
// Test rotate_point()
// ==============================================================
function rotate_test() {

var x = 3;
var y = 3;
var degrees = 90;
var rotated_point = rotate_point(x, y, degrees);
console.log("Original point: (" + x + ", " + y + ")");
var msg = "Rotated point: (" + rotated_point[0] + ", ";
msg += rotated_point[1] + ")";
console.log(msg);

return;
}
// ==============================================================
// Rotate a point (x, y) by degrees degrees.
// Returns the coordinates of the rotated point as a two item
// array in the format [x, y]
// ==============================================================
function rotate_point(x, y, degrees) {

// convert cartesian point to polar representation
var r = Math.sqrt((x * x) + (y * y));
var theta = Math.atan(y / x);
// add rotation degrees to polar coordinates
// 2 pi radians = 360 degrees
// pi radians = 180 degrees
// (pi / 180) radians = 1 degree
var radians = degrees * (Math.PI / 180);
// account for rotation beyond 2 pi radians
// (i.e. one full revolution)
if (theta > Math.PI) {
var total_pi_multiples = Math.floor(theta / Math.PI);
var deduction = total_pi_multiples * Math.PI;
theta -= deduction;
}
// convert polar back to cartesian
// (adjust for positive/negative cos value, depending on
// i.e. cos is positive for the following ranges of theta;
//   0 degrees <= theta <=  90 degrees
// 270 degrees <= theta <= 360 degrees
// i.e.
//   0 pi <= theta <= pi / 2
// 3/2 pi <= theta <= 2 pi
// As theta is currently in radians, we'll use the radian units
var cos_theta = Math.cos(theta);
if ((cos_theta >= 0) && (cos_theta <= (Math.PI / 2))) {
cos_theta = Math.abs(cos_theta);
}
if ((cos_theta >= (Math.PI * 1.5)) && (cos_theta <= (Math.PI * 2))) {
cos_theta = Math.abs(cos_theta);
}

// We'll need to adjust sin theta in a similar fashion to above with cos
// sin is positive in the first and second quadrants. Adjust accordingly.
var sin_theta = Math.sin(theta);
if ((sin_theta >= 0) && (sin_theta <= Math.PI))
sin_theta = Math.abs(sin_theta);
x = r * cos_theta;
y = r * sin_theta;

return [x, y];
}
// ==============================================================
</script>

<body>

<button onclick="rotate_test()">Rotate Test</button>

</body>

</html>```
Any ideas where I'm going wrong?

2. Code:
`var theta = Math.atan(y / x);`
Think about how that works: if x and y are both positive or both negative then the result will be the same, right? 3/3 = 1 and -3/-3 = 1 too. That will give you the same angle for diagonal quadrants. And what will you do if x=0?
`// account for rotation beyond 2 pi radians`
`if (theta > Math.PI) {`