2023-06-04 15:05:20 +00:00
|
|
|
const std = @import("std");
|
2023-06-02 10:21:32 +00:00
|
|
|
const PointF = @import("point_f.zig").PointF;
|
|
|
|
|
|
|
|
pub const Point = struct {
|
|
|
|
x: i64,
|
|
|
|
y: i64,
|
|
|
|
|
2023-06-05 03:56:54 +00:00
|
|
|
pub const One = Point{ .x = 1, .y = 1 };
|
2023-06-04 15:05:20 +00:00
|
|
|
pub const Zero = Point{ .x = 0, .y = 0 };
|
|
|
|
|
2023-06-02 10:21:32 +00:00
|
|
|
pub fn init(x: i64, y: i64) Point {
|
|
|
|
return Point{ .x = x, .y = y };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn initUsize(x: usize, y: usize) Point {
|
2024-05-01 07:15:30 +00:00
|
|
|
return Point.init(@as(i64, @intCast(x)), @as(i64, @intCast(y)));
|
2023-06-02 10:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(self: Point, other: Point) Point {
|
|
|
|
return Point{ .x = self.x + other.x, .y = self.y + other.y };
|
|
|
|
}
|
|
|
|
|
2023-06-04 15:05:20 +00:00
|
|
|
pub fn distance(self: Point, to: Point) i64 {
|
|
|
|
return std.math.sqrt(self.distance2(to));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn distance2(self: Point, to: Point) i64 {
|
|
|
|
const dx = to.x - self.x;
|
|
|
|
const dy = to.y - self.y;
|
|
|
|
return dx * dx + dy * dy;
|
|
|
|
}
|
|
|
|
|
2023-06-02 10:21:32 +00:00
|
|
|
pub fn float(self: Point) PointF {
|
2024-05-01 07:15:30 +00:00
|
|
|
return PointF{ .x = @as(f32, @floatFromInt(self.x)), .y = @as(f32, @floatFromInt(self.y)) };
|
2023-06-02 10:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multiply(self: Point, factor: i64) Point {
|
|
|
|
return Point{ .x = self.x * factor, .y = self.y * factor };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subtract(self: Point, other: Point) Point {
|
|
|
|
return Point{ .x = self.x - other.x, .y = self.y - other.y };
|
|
|
|
}
|
|
|
|
};
|