Created
May 2, 2019 14:45
-
-
Save radzionc/bc63336199447433796c782e065a92aa to your computer and use it in GitHub Desktop.
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 Vector { | |
constructor(...components) { | |
this.components = components | |
} | |
// ... | |
// 3D vectors only | |
crossProduct({ components }) { | |
return new Vector( | |
this.components[1] * components[2] - this.components[2] * components[1], | |
this.components[2] * components[0] - this.components[0] * components[2], | |
this.components[0] * components[1] - this.components[1] * components[0] | |
) | |
} | |
} | |
const one = new Vector(2, 1, 1) | |
const other = new Vector(1, 2, 2) | |
console.log(one.crossProduct(other)) | |
// Vector { components: [ 0, -3, 3 ] } | |
console.log(other.crossProduct(one)) | |
// Vector { components: [ 0, 3, -3 ] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment