Skip to content

Instantly share code, notes, and snippets.

@gitfaf
Last active August 18, 2017 13:48
Show Gist options
  • Save gitfaf/eb1ce8ed21d5d626def9b5e6404276f5 to your computer and use it in GitHub Desktop.
Save gitfaf/eb1ce8ed21d5d626def9b5e6404276f5 to your computer and use it in GitHub Desktop.
export function Vector (x, y) {
this.x = x;
this.y = y;
}
Vector.prototype = {
plus: function (vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
},
minus: function (vector) {
return new Vector(this.x - vector.x, this.y - vector.y);
},
get length () {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
let v = new Vector(10, 10);
v.plus(new Vector(1, 1));
Vector {x: 11, y: 11}
v.length
// 10.954451150103322
import { Vector } from './EJS Exercise - Vector.js';
function test_vector () {
function test_creation () {
let vecA = new Vector(10, 20);
let vecB = new Vector(20, 30);
console.log(vecA.x === 10);
console.log(vecA.y === 20);
console.log(vecB.x === 20);
console.log(vecB.y === 30);
}
function test_plus () {
let vecA = new Vector(10, 20);
let vecB = new Vector(20, 30);
let vecC = vecA.plus(vecB);
console.log(vecA.x + vecB.x === vecC.x);
console.log(vecA.y + vecB.y === vecC.y);
}
function test_minus () {
let vecA = new Vector(10, 20);
let vecB = new Vector(20, 30);
let vecC = vecA.minus(vecB);
console.log(vecA.x - vecB.x === vecC.x);
console.log(vecA.y - vecB.y === vecC.y);
}
function test_length () {
let vecA = new Vector(10, 20);
let vecB = new Vector(20, 30);
console.log(vecA.length === Math.sqrt(500));
console.log(vecB.length === Math.sqrt(1300));
}
test_creation();
test_plus();
test_minus();
test_length();
}
test_vector();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment