artificer/api/schemas/position.zig

44 lines
975 B
Zig

const std = @import("std");
const Position = @This();
x: i64,
y: i64,
pub fn init(x: i64, y: i64) Position {
return Position{ .x = x, .y = y };
}
pub fn zero() Position {
return init(0, 0);
}
pub fn eql(self: Position, other: Position) bool {
return self.x == other.x and self.y == other.y;
}
pub fn subtract(self: Position, other: Position) Position {
return init(self.x - other.x, self.y - other.y);
}
pub fn multiply(self: Position, other: Position) Position {
return init(self.x * other.x, self.y * other.y);
}
pub fn distance(self: Position, other: Position) f32 {
const dx: f32 = @floatFromInt(self.x - other.x);
const dy: f32 = @floatFromInt(self.y - other.y);
return @sqrt(dx * dx + dy * dy);
}
pub fn format(
self: Position,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
try writer.print("Position{{ {}, {} }}", .{ self.x, self.y });
}