Skip to content

Commit 5ba84be

Browse files
author
Adrià Arrufat
committed
Add norm to Point
1 parent 89618bf commit 5ba84be

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/point.zig

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ pub fn Point2d(comptime T: type) type {
1515

1616
/// Assumes the point's coordinates are normalized between 0 and 1. Then it denormalizes
1717
/// them by rescaling with the appropriate dims.
18-
pub inline fn scale(self: Point2d(T), x_dim: usize, y_dim: usize) Point2d(T) {
18+
pub fn scale(self: Point2d(T), x_dim: usize, y_dim: usize) Point2d(T) {
1919
return .{
2020
.x = scaleDimension(self.x, x_dim),
2121
.y = scaleDimension(self.y, y_dim),
2222
};
2323
}
2424

25+
/// Computes the norm of self.
26+
pub fn norm(self: Point2d(T)) T {
27+
return @sqrt(self.x * self.x + self.y * self.y);
28+
}
29+
2530
/// Computes the squared distance between self and other, useful to avoid the call to sqrt.
26-
pub inline fn distance_squared(self: Point2d(T), other: Point2d(T)) T {
31+
pub fn distance_squared(self: Point2d(T), other: Point2d(T)) T {
2732
return
2833
// zig fmt: off
2934
(self.x - other.x) * (self.x - other.x) +
@@ -32,12 +37,12 @@ pub fn Point2d(comptime T: type) type {
3237
}
3338

3439
/// Computes the distance between self and other.
35-
pub inline fn distance(self: Point2d(T), other: Point2d(T)) T {
40+
pub fn distance(self: Point2d(T), other: Point2d(T)) T {
3641
return @sqrt(self.distance_squared(other));
3742
}
3843

3944
/// Rotates the point by angle with regards to center.
40-
pub inline fn rotate(self: Point2d(T), angle: T, center: Point2d(T)) Point2d(T) {
45+
pub fn rotate(self: Point2d(T), angle: T, center: Point2d(T)) Point2d(T) {
4146
const cos = @cos(angle);
4247
const sin = @sin(angle);
4348
return .{
@@ -47,12 +52,12 @@ pub fn Point2d(comptime T: type) type {
4752
}
4853

4954
/// Casts the underlying 2d point type T to U.
50-
pub inline fn cast(self: Point2d(T), comptime U: type) Point2d(U) {
55+
pub fn cast(self: Point2d(T), comptime U: type) Point2d(U) {
5156
return .{ .x = @floatCast(self.x), .y = @floatCast(self.y) };
5257
}
5358

5459
/// Converts the 2d point to a 3d point with the given z coordinate.
55-
pub inline fn to3d(self: Point2d(T), z: T) Point3d(T) {
60+
pub fn to3d(self: Point2d(T), z: T) Point3d(T) {
5661
return .{ .x = self.x, .y = self.y, .z = z };
5762
}
5863
};
@@ -68,16 +73,21 @@ pub fn Point3d(comptime T: type) type {
6873

6974
/// Assumes the point's coordinates are normalized between 0 and 1. Then it denormalizes
7075
/// them by rescaling with the appropriate dims.
71-
pub inline fn scale(self: Point3d(T), x_dim: usize, y_dim: usize, z_dim: usize) Point3d(T) {
76+
pub fn scale(self: Point3d(T), x_dim: usize, y_dim: usize, z_dim: usize) Point3d(T) {
7277
return .{
7378
.x = scaleDimension(self.x, x_dim),
7479
.y = scaleDimension(self.y, y_dim),
7580
.z = scaleDimension(self.z, z_dim),
7681
};
7782
}
7883

84+
/// Computes the norm of self.
85+
pub fn norm(self: Point3d(T)) T {
86+
return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
87+
}
88+
7989
/// Computes the squared distance between self and other, useful to avoid the call to sqrt.
80-
pub inline fn distance_squared(self: Point3d(T), other: Point3d(T)) T {
90+
pub fn distance_squared(self: Point3d(T), other: Point3d(T)) T {
8191
return
8292
// zig fmt: off
8393
(self.x - other.x) * (self.x - other.x) +
@@ -87,23 +97,23 @@ pub fn Point3d(comptime T: type) type {
8797
}
8898

8999
/// Computes the distance between self and other.
90-
pub inline fn distance(self: Point3d(T), other: Point3d(T)) T {
100+
pub fn distance(self: Point3d(T), other: Point3d(T)) T {
91101
return @sqrt(self.distance_squared(other));
92102
}
93103

94104
/// Casts the underlying 3d point type T to U.
95-
pub inline fn cast(self: Point3d(T), comptime U: type) Point3d(U) {
105+
pub fn cast(self: Point3d(T), comptime U: type) Point3d(U) {
96106
return .{ .x = @floatCast(self.x), .y = @floatCast(self.y), .z = @floatCast(self.z) };
97107
}
98108

99109
/// Converts the 3d point to a 2d point by removing the z coordinate.
100-
pub inline fn to2d(self: Point3d(T)) Point2d(T) {
110+
pub fn to2d(self: Point3d(T)) Point2d(T) {
101111
return .{ .x = self.x, .y = self.y };
102112
}
103113

104114
/// Converts the 3d point to a 2d point in homogeneous coordinates, that is, by dividing
105115
/// x and y by z.
106-
pub inline fn to2dHomogeneous(self: Point3d(T)) Point2d(T) {
116+
pub fn to2dHomogeneous(self: Point3d(T)) Point2d(T) {
107117
if (self.z == 0) {
108118
return self.to2d();
109119
} else {

0 commit comments

Comments
 (0)