@@ -24,15 +24,132 @@ describe('util', function () {
2424 // [ -0, -0, 1, -1 ]
2525 // ]);
2626 // });
27- describe ( 'areEqual' , ( ) => {
28- it ( 'should return true if two matrices are equal' , ( ) => {
29- expect ( util . areEqual ( 1 , 1 ) ) . toBe ( true ) ;
30- expect ( util . areEqual ( 2 , 2 ) ) . toBe ( true ) ;
31- } ) ;
32- it ( 'should return false if two matrices are not equal' , ( ) => {
33- expect ( util . areEqual ( 1 , 3.000000000000004 ) ) . toBe ( false ) ;
34- expect ( util . areEqual ( 2 , 1 ) ) . toBe ( false ) ;
35- } ) ;
27+
28+ describe ( 'areEqual' , ( ) => {
29+ it ( 'should return true if two matrices are equal' , ( ) => {
30+ expect ( util . areEqual ( 1 , 1 ) ) . toBe ( true ) ;
31+ expect ( util . areEqual ( 2 , 2 ) ) . toBe ( true ) ;
32+ } ) ;
33+ it ( 'should return false if two matrices are not equal' , ( ) => {
34+ expect ( util . areEqual ( 1 , 3.000000000000004 ) ) . toBe ( false ) ;
35+ expect ( util . areEqual ( 2 , 1 ) ) . toBe ( false ) ;
36+ } ) ;
37+ } ) ;
38+ describe ( 'toDegrees' , ( ) => {
39+ it ( 'should return the degree equivalent of the input radian' , ( ) => {
40+ expect ( util . toDegrees ( 0 ) ) . toBe ( 0 ) ;
41+ expect ( util . toDegrees ( Math . PI ) ) . toBe ( 180 ) ;
42+ expect ( util . toDegrees ( Math . PI / 2 ) ) . toBe ( 90 ) ;
3643 } ) ;
3744 } ) ;
38- // });
45+ describe ( 'toRadians' , ( ) => {
46+ it ( 'should return the radian equivalent of the input degree' , ( ) => {
47+ expect ( util . toRadians ( 0 ) ) . toBe ( 0 ) ;
48+ expect ( util . toRadians ( 180 ) ) . toBe ( Math . PI ) ;
49+ expect ( util . toRadians ( 90 ) ) . toBe ( Math . PI / 2 ) ;
50+ } ) ;
51+ } ) ;
52+ } ) ;
53+
54+ /*
55+ const EPSILON = 0.00000001
56+
57+ const areEqual = (one, other, epsilon = EPSILON) =>
58+ Math.abs(one - other) < epsilon
59+
60+ const toDegrees = radians => (radians * 180) / Math.PI
61+ const toRadians = degrees => (degrees * Math.PI) / 180
62+ const sum = arr => arr.reduce((acc, value) => acc + value, 0)
63+ const withoutElementAtIndex = (arr, index) => [ ...arr.slice(0, index), ...arr.slice(index + 1) ]
64+
65+ class Vector {
66+ constructor(...components) {
67+ this.components = components
68+ }
69+ add({ components }) {
70+ return new Vector(
71+ ...components.map((component, index) => this.components[index] + component)
72+ )
73+ }
74+ subtract({ components }) {
75+ return new Vector(
76+ ...components.map((component, index) => this.components[index] - component)
77+ )
78+ }
79+ scaleBy(number) {
80+ return new Vector(
81+ ...this.components.map(component => component * number)
82+ )
83+ }
84+ length() {
85+ return Math.hypot(...this.components)
86+ }
87+ dotProduct({ components }) {
88+ return components.reduce((acc, component, index) => acc + component * this.components[index], 0)
89+ }
90+ normalize() {
91+ return this.scaleBy(1 / this.length())
92+ }
93+ haveSameDirectionWith(other) {
94+ const dotProduct = this.normalize().dotProduct(other.normalize())
95+ return areEqual(dotProduct, 1)
96+ }
97+ haveOppositeDirectionTo(other) {
98+ const dotProduct = this.normalize().dotProduct(other.normalize())
99+ return areEqual(dotProduct, -1)
100+ }
101+ isPerpendicularTo(other) {
102+ const dotProduct = this.normalize().dotProduct(other.normalize())
103+ return areEqual(dotProduct, 0)
104+ }
105+ // 3D vectors only
106+ crossProduct({ components }) {
107+ return new Vector(
108+ this.components[1] * components[2] - this.components[2] * components[1],
109+ this.components[2] * components[0] - this.components[0] * components[2],
110+ this.components[0] * components[1] - this.components[1] * components[0]
111+ )
112+ }
113+ angleBetween(other) {
114+ return toDegrees(
115+ Math.acos(
116+ this.dotProduct(other) /
117+ (this.length() * other.length())
118+ )
119+ )
120+ }
121+ negate() {
122+ return this.scaleBy(-1)
123+ }
124+ withLength(newLength) {
125+ console.log('from util test this.normalize()',this.normalize())
126+ return this.normalize().scaleBy(newLength)
127+ }
128+ projectOn(other) {
129+ const normalized = other.normalize()
130+ return normalized.scaleBy(this.dotProduct(normalized))
131+ }
132+ equalTo({ components }) {
133+ return components.every((component, index) => areEqual(component, this.components[index]))
134+ }
135+ transform(matrix) {
136+ const columns = matrix.columns()
137+ if(columns.length !== this.components.length) {
138+ throw new Error('Matrix columns length should be equal to vector components length.')
139+ }
140+
141+ const multiplied = columns
142+ .map((column, i) => column.map(c => c * this.components[i]))
143+ const newComponents = multiplied[0].map((_, i) => sum(multiplied.map(column => column[i])))
144+ return new Vector(...newComponents)
145+ }
146+ }
147+
148+
149+ const one = new Vector(2, 3)
150+ console.log(one.length())
151+ // 3.6055512754639896
152+ const modified = one.withLength(10)
153+ // 10
154+ console.log({modified},modified.length())
155+ */
0 commit comments