mirror of https://git.sr.ht/~kikoodx/kble
Create Vec2 structure and functions.
This commit is contained in:
parent
e8c5a3f39e
commit
2180d91dc2
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue