Skip to content

Commit 0654028

Browse files
committed
Implement the vector data type
1 parent cf312fe commit 0654028

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ A data structure is a data organization, management, and storage format that ena
1212
* [Graph](src/data-structures/graph) - a set of vertices and edges;
1313
* [Stack](src/data-structures/stack) - a data structure to follow the LIFO principle;
1414
* [Linked list](src/data-structures/linked-list) - a data structure to follow the FIFO principle;
15+
* [Vector](src/data-structures/vector) - is used to represent the mathematical vector used in linear algebra;
1516

1617
## Algorithms
1718

src/data-structures/vector/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Vector
2+
3+
A vector, in computing, is generally a one-dimensional array, typically storing numbers. Vectors typically have fixed sizes, unlike lists and queues.
4+
5+
The vector data structure can be used to represent the mathematical vector used in linear algebra. See related pages for mathematical vector operations. Vectors are often used in computing in computer graphics and simulating physical systems.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { vec2 } from "../vec2";
2+
3+
describe("vec2", () => {
4+
it("resets", () => {
5+
const vector = new vec2(1.0, 2.0);
6+
7+
vector.reset();
8+
9+
expect(vector.x).toEqual(0);
10+
expect(vector.y).toEqual(0);
11+
});
12+
13+
it("copies", () => {
14+
const vector1 = new vec2(1.0, 2.0);
15+
const vector2 = vector1.copy();
16+
17+
expect(vector2.x).toEqual(vector1.x);
18+
expect(vector2.y).toEqual(vector1.y);
19+
});
20+
21+
it("negates", () => {
22+
const vector = new vec2(1.0, 2.0);
23+
24+
vector.negate();
25+
26+
expect(vector.x).toEqual(-1.0);
27+
expect(vector.y).toEqual(-2.0);
28+
});
29+
30+
it("compares", () => {
31+
const vector1 = new vec2(1.0, 2.0);
32+
const vector2 = new vec2(1.0, 2.0);
33+
const vector3 = new vec2(2.0, 3.0);
34+
35+
expect(vector1.equals(vector2)).toEqual(true);
36+
expect(vector1.equals(vector3)).toEqual(false);
37+
});
38+
39+
it("adds", () => {
40+
const vector1 = new vec2(1.0, 2.0);
41+
const vector2 = new vec2(2.0, 3.0);
42+
43+
const result = vector1.add(vector2);
44+
45+
expect(result.x).toEqual(3.0);
46+
expect(result.y).toEqual(5.0);
47+
});
48+
49+
it("subtracts", () => {
50+
const vector1 = new vec2(1.0, 2.0);
51+
const vector2 = new vec2(2.0, 4.0);
52+
53+
const result = vector1.subtract(vector2);
54+
55+
expect(result.x).toEqual(-1.0);
56+
expect(result.y).toEqual(-2.0);
57+
});
58+
59+
it("multiplies", () => {
60+
const vector1 = new vec2(2.0, 3.0);
61+
const vector2 = new vec2(5.0, 6.0);
62+
63+
const result = vector1.multiply(vector2);
64+
65+
expect(result.x).toEqual(10.0);
66+
expect(result.y).toEqual(18.0);
67+
});
68+
69+
it("divides", () => {
70+
const vector1 = new vec2(2.0, 3.0);
71+
const vector2 = new vec2(5.0, 6.0);
72+
73+
const result = vector1.divide(vector2);
74+
75+
expect(result.x).toEqual(0.4);
76+
expect(result.y).toEqual(0.5);
77+
});
78+
79+
it("scales", () => {
80+
const vector = new vec2(1.0, 2.0);
81+
82+
vector.scale(2.0);
83+
84+
expect(vector.x).toEqual(2.0);
85+
expect(vector.y).toEqual(4.0);
86+
});
87+
88+
it("normalizes", () => {
89+
const vector = new vec2(1.0, 2.0);
90+
91+
vector.normalize();
92+
93+
expect(vector.x).toBeCloseTo(0.44721, 5);
94+
expect(vector.y).toBeCloseTo(0.89443, 5);
95+
});
96+
});

src/data-structures/vector/vec2.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export class vec2 {
2+
x: number;
3+
y: number;
4+
5+
constructor(x: number, y: number) {
6+
this.x = x;
7+
this.y = y;
8+
}
9+
10+
reset() {
11+
this.x = 0;
12+
this.y = 0;
13+
}
14+
15+
copy() {
16+
return new vec2(this.x, this.y);
17+
}
18+
19+
negate() {
20+
this.x *= -1;
21+
this.y *= -1;
22+
}
23+
24+
equals(vector: vec2) {
25+
return this.x === vector.x && this.y === vector.y;
26+
}
27+
28+
add(vector: vec2) {
29+
return new vec2(this.x + vector.x, this.y + vector.y);
30+
}
31+
32+
subtract(vector: vec2) {
33+
return new vec2(this.x - vector.x, this.y - vector.y);
34+
}
35+
36+
multiply(vector: vec2) {
37+
return new vec2(this.x * vector.x, this.y * vector.y);
38+
}
39+
40+
divide(vector: vec2) {
41+
return new vec2(this.x / vector.x, this.y / vector.y);
42+
}
43+
44+
scale(value: number) {
45+
this.x *= value;
46+
this.y *= value;
47+
}
48+
49+
normalize() {
50+
const length = this.length();
51+
this.x /= length;
52+
this.y /= length;
53+
}
54+
55+
length(): number {
56+
return Math.sqrt(this.x * this.x + this.y * this.y);
57+
}
58+
}

0 commit comments

Comments
 (0)