kble/src/rect.zig

36 lines
1.1 KiB
Zig

// 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.
const std = @import("std");
const expect = std.testing.expect;
const Vec2 = @import("vec2.zig");
const Self = @This();
pub const int_type = Vec2.int_type;
left_x: int_type,
top_y: int_type,
right_x: int_type,
bottom_y: int_type,
pub fn init_from_vec2(first: Vec2, second: Vec2) Self {
return Self{
.left_x = if (first.x < second.x) first.x else second.x,
.top_y = if (first.y < second.y) first.y else second.y,
.right_x = if (first.x > second.x) first.x else second.x,
.bottom_y = if (first.y > second.y) first.y else second.y,
};
}
test "create Rect from two Vec2" {
const rect_1 = Self.init_from_vec2(Vec2{ .x = 5, .y = 2 }, Vec2{ .x = 2, .y = 9 });
expect(rect_1.left_x == 2);
expect(rect_1.top_y == 2);
expect(rect_1.right_x == 5);
expect(rect_1.bottom_y == 9);
}