Created
February 19, 2017 13:31
-
-
Save Pan-Maciek/5f2cd5a2555c00a603678aa47124640b to your computer and use it in GitHub Desktop.
Rzutowanie 3d na 2d (rzut izometryczny)
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 | |
const sin = Math.sin(rad) | |
const cos = Math.cos(rad) | |
return new Vector2( | |
x * cos - y * sin + vector2.X, | |
x * sin + y * cos + vector2.Y | |
) | |
} | |
/** Returns new Vector2 created with sum of this vector and specified vector. | |
* @param {Vector2} vector | |
* @returns {Vector2} | |
*/ | |
translate(vector) { | |
return new Vector2(this.X + vector.X, this.Y + vector.Y) | |
} | |
} |
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
import Vector2 from './Vector2' | |
const SIN27 = Math.sin(27) | |
const COS27 = Math.cos(27) | |
/** Class representing a 3D vector. (X, Y, Z)*/ | |
export default class Vector3 { | |
constructor(X = 0, Y = 0, Z = 0) { | |
this.X = X | |
this.Y = Y | |
this.Z = Z | |
} | |
/** Returns new Vector2 created from the isometric projection of this vector. | |
* @returns {Vector2} | |
*/ | |
toVector2() { | |
const {X, Y, Z} = this | |
return new Vector2( | |
(Y - X) * SIN27, | |
-((Y + X) * COS27 + Z) | |
) | |
} | |
/** Returns new Vector3 rotated by given angle (in radians) and any point. | |
* @param {Number} rad Angle of rotation given in radians. | |
* @param {Vector2|Vector3} vector Point against which rotation is performed. | |
* @returns {Vector3} | |
*/ | |
rotate(rad, vector = new Vector3(0, 0)) { | |
const x = this.X - vector.X | |
const y = this.Y - vector.Y | |
const sin = Math.sin(rad) | |
const cos = Math.cos(rad) | |
return new Vector3( | |
x * cos - y * sin + vector.X, | |
x * sin + y * cos + vector.Y, | |
this.Z | |
) | |
} | |
/** Returns new Vector3 created with sum of this vector and specified vector. | |
* @param {Vector3} vector | |
* @returns {Vector3} | |
*/ | |
translate(vector) { | |
return new Vector3(this.X + vector.X, this.Y + vector.Y, this.Z + vector.Z) | |
} | |
/** Returns new Vector3 created by multiplying vector by n. | |
* @param {Number} n | |
* @returns {Vector3} | |
*/ | |
scale(n) { | |
return new Vector3(this.X * n, this.Y * n, this.Z * n) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment