@@ -34,4 +34,40 @@ describe('Vector test.', () => {
3434 assert . isFalse ( dir . len < pos . len , `${ dir } should be longer than ${ pos } ` ) ;
3535
3636 } ) ;
37+
38+ it ( 'should change length to 1 when calling normalize' , ( ) => {
39+
40+ const pos = new Vector ( 5 , 6 , 7 ) ;
41+ const dir = pos . normalize ( ) ;
42+
43+ const length = dir . length ;
44+ assert ( length > 0.99 && length < 1.01 , `${ dir } should have length 1, but is ${ length } ` ) ;
45+
46+ } ) ;
47+
48+ it ( 'only for readme' , ( ) => {
49+
50+ // create vector by numbers
51+
52+ const pos = new Vector ( 5 , 6 , 7 ) ;
53+ const dir = new Vector ( 1 , 0 , 0 ) ;
54+
55+ console . log ( 'pos:' , pos , ' dir:' , dir ) ;
56+ // pos: { [Number: 10.48] x: 5, y: 6, z: 7 } dir: { [Number: 1] x: 1, y: 0, z: 0 }
57+
58+ // or create vector by calculating other vectors and number
59+ const offset = new Vector ( ( ) => dir * 30 + pos ) ;
60+
61+ console . log ( 'offset:' , offset ) ;
62+ // offset: { [Number: 36.19] x: 35, y: 6, z: 7 }
63+
64+ // compare length
65+ let way = offset ;
66+ if ( way > 1 ) {
67+ way = way . normalize ( ) ;
68+ }
69+ console . log ( 'way:' , way ) ;
70+ // way: { [Number: 1] x: 0.96, y: 0.16, z: 0.19 }
71+
72+ } ) ;
3773} ) ;
0 commit comments