aoc-2023/src/point.zig

62 lines
1.4 KiB
Zig

pub fn Point(comptime T: type) type {
return struct {
const Self = @This();
x: T,
y: T,
pub fn eql(self: Self, other: Self) bool {
return self.x == other.x and self.y == other.y;
}
pub fn neg(self: Self) Self {
return Self{
.x = -self.x,
.y = -self.y,
};
}
pub fn zero() Self {
return Self{ .x = 0, .y = 0 };
}
pub fn add(self: Self, other: Self) Self {
return Self{
.x = self.x + other.x,
.y = self.y + other.y,
};
}
pub fn sub(self: Self, other: Self) Self {
return Self{
.x = self.x - other.x,
.y = self.y - other.y,
};
}
pub fn mul(self: Self, scale: T) Self {
return Self{
.x = self.x * scale,
.y = self.y * scale
};
}
pub fn to_i32(self: Self) Point(i32) {
return Point(i32){
.x = @intCast(self.x),
.y = @intCast(self.y),
};
}
pub fn to_u32(self: Self) Point(u32) {
return Point(u32){
.x = @intCast(self.x),
.y = @intCast(self.y),
};
}
};
}
pub const PointI32 = Point(i32);
pub const PointU32 = Point(u32);