Skip to content

Commit 196f57c

Browse files
committed
Added RotateAroundDistance and tidied up other Rotate functions.
1 parent 1712560 commit 196f57c

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

v3/src/math/Rotate.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// p = Point or any object with public x/y properties
2-
var Rotate = function (p, angle)
2+
3+
var Rotate = function (point, angle)
34
{
4-
var x = p.x;
5-
var y = p.y;
5+
var x = point.x;
6+
var y = point.y;
67

7-
p.x = x * Math.cos(angle) - y * Math.sin(angle);
8-
p.y = x * Math.sin(angle) + y * Math.cos(angle);
8+
point.x = (x * Math.cos(angle)) - (y * Math.sin(angle));
9+
point.y = (x * Math.sin(angle)) + (y * Math.cos(angle));
910

10-
return p;
11+
return point;
1112
};
1213

1314
module.exports = Rotate;

v3/src/math/RotateAround.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// p = Point or any object with public x/y properties
2-
var RotateAround = function (p, cx, cy, angle)
2+
3+
var RotateAround = function (point, x, y, angle)
34
{
45
var c = Math.cos(angle);
56
var s = Math.sin(angle);
67

7-
var x = p.x - cx;
8-
var y = p.y - cy;
8+
var tx = point.x - x;
9+
var ty = point.y - y;
910

10-
p.x = x * c - y * s + cx;
11-
p.y = x * s + y * c + cy;
11+
point.x = tx * c - ty * s + x;
12+
point.y = tx * s + ty * c + y;
1213

13-
return p;
14+
return point;
1415
};
1516

1617
module.exports = RotateAround;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// p = Point or any object with public x/y properties
2+
3+
var RotateAroundDistance = function (point, x, y, angle, distance)
4+
{
5+
var t = angle + Math.atan2(point.y - y, point.x - x);
6+
7+
point.x = x + (distance * Math.cos(t));
8+
point.y = y + (distance * Math.sin(t));
9+
10+
return point;
11+
};
12+
13+
module.exports = RotateAroundDistance;

v3/src/math/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
RadToDeg: require('./RadToDeg'),
3636
Rotate: require('./Rotate'),
3737
RotateAround: require('./RotateAround'),
38+
RotateAroundDistance: require('./RotateAroundDistance'),
3839
RoundAwayFromZero: require('./RoundAwayFromZero'),
3940
RoundTo: require('./RoundTo'),
4041
SinCosTableGenerator: require('./SinCosTableGenerator'),

0 commit comments

Comments
 (0)