forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.test.js
More file actions
103 lines (81 loc) · 3.42 KB
/
Vector2.test.js
File metadata and controls
103 lines (81 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Vector2 } from '../Vector2.js'
describe('Vector2', () => {
describe('#equalsExactly', () => {
it('should compare equality correctly', () => {
expect(new Vector2(1, 0).equalsExactly(new Vector2(1, 0))).toBe(true)
expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(false)
})
})
describe('#equalsApproximately', () => {
it('should compare equality (approximately) correctly', () => {
expect(new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001))
.toBe(true)
expect(new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001))
.toBe(false)
})
})
describe('#add', () => {
it('should add two vectors correctly', () => {
expect(new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001))
.toBe(true)
expect(new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001))
.toBe(true)
})
})
describe('#subtract', () => {
it('should subtract two vectors correctly', () => {
expect(new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001))
.toBe(true)
expect(new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001))
.toBe(true)
})
})
describe('#multiply', () => {
it('should multiply two vectors correctly', () => {
expect(new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001))
.toBe(true)
expect(new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001))
.toBe(true)
})
})
describe('#length', () => {
it('should calculate it\'s length correctly', () => {
expect(new Vector2(1, 0).length()).toBe(1)
expect(new Vector2(-1, 1).length()).toBe(Math.sqrt(2))
})
})
describe('#normalize', () => {
it('should normalize vectors correctly', () => {
expect(new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001))
.toBe(true)
expect(new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001))
.toBe(true)
})
})
describe('#distance', () => {
it('should calculate the distance between two vectors correctly', () => {
expect(new Vector2(0, 0).distance(new Vector2(0, -1))).toBe(1)
expect(new Vector2(1, 0).distance(new Vector2(0, 1))).toBe(Math.sqrt(2))
})
})
describe('#dotProduct', () => {
it('should calculate the dot product correctly', () => {
expect(new Vector2(1, 0).dotProduct(new Vector2(0, 1))).toBe(0)
expect(new Vector2(1, 2).dotProduct(new Vector2(3, 4))).toBe(11) // 1 * 3 + 2 * 4
})
})
describe('#rotate', () => {
it('should rotate a vector correctly', () => {
expect(new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001))
.toBe(true)
expect(new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001))
.toBe(true)
})
})
describe('#angleBetween', () => {
it('should calculate the angle between two vectors correctly', () => {
expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(Math.PI / 2)
expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(-Math.PI / 4)
})
})
})