Create Vec2 structure and functions.

This commit is contained in:
KikooDX 2021-01-25 09:47:15 +01:00
parent e8c5a3f39e
commit 2180d91dc2
1 changed files with 58 additions and 0 deletions

58
src/vec2.zig Normal file
View File

@ -0,0 +1,58 @@
//! This module provides functions for creating and manipulating 2D
//! vectors with integer precision.
const expect = @import("std").testing.expect;
pub const Vec2 = struct {
x: i32,
y: i32,
pub fn init(x: i32, y: i32) Vec2 {
return Vec2 {
.x = x,
.y = y,
};
}
pub fn add(self: Vec2, other: Vec2) Vec2 {
return Vec2 {
.x = self.x + other.x,
.y = self.y + other.y,
};
}
pub fn sub(self: Vec2, other: Vec2) Vec2 {
return Vec2 {
.x = self.x - other.x,
.y = self.y - other.y,
};
}
};
test "create Vec2 w/ positive values" {
const vector = Vec2.init(15984, 95715);
expect(vector.x == 15984);
expect(vector.y == 95715);
}
test "create Vec2 w/ negative values" {
const vector = Vec2.init(-51428, -56123);
expect(vector.x == -51428);
expect(vector.y == -56123);
}
test "add two Vec2 using dot syntax" {
const vector_1 = Vec2.init(6712, 981);
const vector_2 = Vec2.init(-1823, 12);
const vector_sum = vector_1.add(vector_2);
expect(vector_sum.x == 4889);
expect(vector_sum.y == 993);
}
test "calculate difference between two Vec2 using dot syntax" {
const vector_1 = Vec2.init(6712, 981);
const vector_2 = Vec2.init(-1823, 12);
const vector_diff = vector_1.sub(vector_2);
expect(vector_diff.x == 8535);
expect(vector_diff.y == 969);
}