// SPDX-License-Identifier: MIT // Copyright (c) 2021 KikooDX // This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. //! This module provides functions for creating and manipulating 2D //! vectors with integer precision. const expect = @import("std").testing.expect; const Self = @This(); pub const int_type = u16; x: int_type, y: int_type, /// Create a vector with `x` and `y` values. pub fn init(x: int_type, y: int_type) Self { return Self { .x = x, .y = y, }; } /// Return the sum of `self` and `other`. pub fn add(self: Self, other: Self) Self { return Self { .x = self.x + other.x, .y = self.y + other.y, }; } /// Return the difference between `self` and `other`. pub fn sub(self: Self, other: Self) Self { return Self { .x = self.x - other.x, .y = self.y - other.y, }; } test "create Vector" { const vector = Self.init(14, 95); expect(vector.x == 14); expect(vector.y == 95); } test "add two Self using dot syntax" { const vector_1 = Self.init(23, 81); const vector_2 = Self.init(13, 12); const vector_sum = vector_1.add(vector_2); expect(vector_sum.x == 36); expect(vector_sum.y == 93); } test "calculate difference between two Self using dot syntax" { const vector_1 = Self.init(12, 98); const vector_2 = Self.init(4, 40); const vector_diff = vector_1.sub(vector_2); expect(vector_diff.x == 8); expect(vector_diff.y == 58); }