import Vertice from './Vertice' ; export default class Vector { constructor ( public x = 0 , public y = 0 , public z = 0 ) { } static fromVertice ( vertice ) { return new Vector ( vertice . x , vertice . y , vertice . z ) ; } static verticeMinusVertice ( endVertice , startVertice ) { return new Vector ( endVertice . x - startVertice . x , endVertice . y - startVertice . y , endVertice . z - startVertice . z ) ; } static verticePlusVector ( vertice , vector ) { return new Vertice ( vertice . x + vector . x , vertice . y + vector . y , vertice . z + vector . z ) ; } static getRadianOfTwoVectors ( vector1 , vector2 ) { if ( vector1 . isZeroLength ( ) || vector2 . isZeroLength ( ) ) { return 0 ; } var v1 = vector1 . clone ( ) . normalize ( ) ; var v2 = vector2 . clone ( ) . normalize ( ) ; var dotValue = v1 . dot ( v2 ) ; if ( dotValue < - 1 ) { dotValue = - 1 ; } if ( dotValue > 1 ) { dotValue = 1 ; } var radian = Math . acos ( dotValue ) ; return radian ; } getVertice ( ) { return new Vertice ( this . x , this . y , this . z ) ; } getArray ( ) { return [ this . x , this . y , this . z ] ; } clone ( ) { return new Vector ( this . x , this . y , this . z ) ; } getOpposite ( ) { return new Vector ( - this . x , - this . y , - this . z ) ; } getLength ( ) { return Math . sqrt ( this . x * this . x + this . y * this . y + this . z * this . z ) ; } isZeroLength ( ) { return this . x === 0 && this . y === 0 && this . z === 0 ; } normalize ( ) { var length = this . getLength ( ) ; if ( Math . abs ( length ) >= 0.000001 ) { this . x /= length ; this . y /= length ; this . z /= length ; } else { this . x = 0 ; this . y = 0 ; this . z = 0 ; } return this ; } setLength ( length ) { this . normalize ( ) ; this . x *= length ; this . y *= length ; this . z *= length ; return this ; } return { * } getRandomVerticalVector ( ) { var result ; var length = this . getLength ( ) ; if ( length === 0 ) { result = new Vector ( 0 , 0 , 0 ) ; } else { var x2 , y2 , z2 ; if ( this . x !== 0 ) { y2 = 1 ; z2 = 0 ; x2 = - this . y / this . x ; } else if ( this . y !== 0 ) { z2 = 1 ; x2 = 0 ; y2 = - this . z / this . y ; } else if ( this . z !== 0 ) { x2 = 1 ; y2 = 0 ; z2 = - this . x / this . z ; } result = new Vector ( x2 , y2 , z2 ) ; result . normalize ( ) ; } return result ; } param other return { World . Vector } cross ( other ) { var x = this . y * other . z - this . z * other . y ; var y = this . z * other . x - this . x * other . z ; var z = this . x * other . y - this . y * other . x ; return new Vector ( x , y , z ) ; } param other return { * } dot ( other ) { if ( ! ( other instanceof Vector ) ) { throw "invalid other" ; } return this . x * other . x + this . y * other . y + this . z * other . z ; } } ;