Created
February 8, 2017 15:18
-
-
Save Pan-Maciek/17f89f9d81bee1eac3a46458f5211f77 to your computer and use it in GitHub Desktop.
Rotating 2D vector.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** Class representing a 2D vector. (X, Y)*/ | |
| export default class Vector2 { | |
| constructor(X = 0, Y = 0) { | |
| this.X = X | |
| this.Y = Y | |
| } | |
| /** Returns new Vector2 rotated by given angle (in radians) and any point. | |
| * @param {Number} rad Angle of rotation given in radians. | |
| * @param {Vector2} vector2 Point against which rotation is performed. | |
| * @returns {Vector2} | |
| */ | |
| rotate(rad, vector2 = new Vector2(0, 0)) { | |
| const x = this.X - vector2.X | |
| const y = this.Y - vector2.Y | |
| return new Vector2( | |
| x * Math.cos(rad) - y * Math.sin(rad) | |
| + vector2.X, | |
| x * Math.sin(rad) + y * Math.cos(rad) | |
| + vector2.Y | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment