tins2023/src/engine/point_f.zig
Sander Schobers 93d9fe6c12 Updated to Zig 0.12.0.
Moved allegro as a local package.
2024-05-01 09:15:30 +02:00

55 lines
1.5 KiB
Zig

const std = @import("std");
const Point = @import("point.zig").Point;
pub const PointF = struct {
x: f32,
y: f32,
pub const Half = PointF{ .x = 0.5, .y = 0.5 };
pub const One = PointF{ .x = 1, .y = 1 };
pub const Zero = PointF{ .x = 0, .y = 0 };
pub fn init(x: f32, y: f32) PointF {
return PointF{ .x = x, .y = y };
}
pub fn add(self: PointF, other: PointF) PointF {
return PointF{ .x = self.x + other.x, .y = self.y + other.y };
}
pub fn ceil(self: PointF) PointF {
return PointF{ .x = std.math.ceil(self.x), .y = std.math.ceil(self.y) };
}
pub fn distance(self: PointF, to: PointF) f32 {
return std.math.sqrt(self.distance2(to));
}
pub fn distance2(self: PointF, to: PointF) f32 {
const dx = to.x - self.x;
const dy = to.y - self.y;
return dx * dx + dy * dy;
}
pub fn floor(self: PointF) PointF {
return PointF{ .x = std.math.floor(self.x), .y = std.math.floor(self.y) };
}
pub fn integer(self: PointF) Point {
return Point{ .x = @as(i64, @intFromFloat(self.x)), .y = @as(i64, @intFromFloat(self.y)) };
}
pub fn multiply(self: PointF, factor: f32) PointF {
return PointF{ .x = self.x * factor, .y = self.y * factor };
}
pub fn round(self: PointF) PointF {
return PointF{ .x = std.math.round(self.x), .y = std.math.round(self.y) };
}
pub fn subtract(self: PointF, other: PointF) PointF {
return PointF{ .x = self.x - other.x, .y = self.y - other.y };
}
};