Skip to content

Instantly share code, notes, and snippets.

@radzionc
Created May 2, 2019 14:45
Show Gist options
  • Save radzionc/bc63336199447433796c782e065a92aa to your computer and use it in GitHub Desktop.
Save radzionc/bc63336199447433796c782e065a92aa to your computer and use it in GitHub Desktop.
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