Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 3D Vector type
- fields:
- number x
- number y
- number z
- consructors:
- default(number x, number y, number z) aka new keyword
- zero()
- fill(number) fills each field with the same value
- copy(vector) creates new vector the same as the old
- methods:
- add(vector) => vector, object keeps unchanged
- sub(vector) => vector, object keeps unchanged
- mul(vector) => vector, object keeps unchanged
- div(vector) => vector, object keeps unchanged
- negative() => vector, object keeps unchanged
- scale(number) => vector, object keeps unchanged
- cross(vector) => vector, object keeps unchanged
- dot(vector) => number
- length() => number
- normalize() => vector, object keeps unchanged
- prototype.toString() => string
- */
- function Vector(x, y, z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- this.add = (a) => new Vector(this.x + a.x, this.y + a.y, this.z + a.z);
- this.sub = (a) => new Vector(this.x - a.x, this.y - a.y, this.z - a.z);
- this.mul = (a) => new Vector(this.x * a.x, this.y * a.y, this.z * a.z);
- this.div = (a) => new Vector(this.x / a.x, this.y / a.y, this.z / a.z);
- this.negative = () => new Vector(-this.x, -this.y, -this.z);
- this.scale = (value) => new Vector(this.x * value, this.y * value, this.z * value);
- this.cross = (a) => new Vector(
- this.y * a.z - this.z * a.y,
- this.x * a.z - this.z * a.x,
- this.x * a.y - this.y * a.x
- );
- this.dot = (a) => this.x * a.x + this.y * a.y + this.z * a.z;
- this.length = () => Math.sqrt(this.dot(this));
- this.normalize = () => this.scale(1 / this.length());
- }
- Vector.zero = () => new Vector(0, 0, 0);
- Vector.fill = (value) => new Vector(value, value, value);
- Vector.copy = (a) => new Vector(a.x, a.y, a.z);
- Vector.prototype.toString = function()
- {
- return `x: ${this.x}\ty: ${this.y}\tz: ${this.z}`;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement