@@ -15,15 +15,20 @@ pub fn Point2d(comptime T: type) type {
15
15
16
16
/// Assumes the point's coordinates are normalized between 0 and 1. Then it denormalizes
17
17
/// 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 ) {
19
19
return .{
20
20
.x = scaleDimension (self .x , x_dim ),
21
21
.y = scaleDimension (self .y , y_dim ),
22
22
};
23
23
}
24
24
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
+
25
30
/// 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 {
27
32
return
28
33
// zig fmt: off
29
34
(self .x - other .x ) * (self .x - other .x ) +
@@ -32,12 +37,12 @@ pub fn Point2d(comptime T: type) type {
32
37
}
33
38
34
39
/// 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 {
36
41
return @sqrt (self .distance_squared (other ));
37
42
}
38
43
39
44
/// 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 ) {
41
46
const cos = @cos (angle );
42
47
const sin = @sin (angle );
43
48
return .{
@@ -47,12 +52,12 @@ pub fn Point2d(comptime T: type) type {
47
52
}
48
53
49
54
/// 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 ) {
51
56
return .{ .x = @floatCast (self .x ), .y = @floatCast (self .y ) };
52
57
}
53
58
54
59
/// 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 ) {
56
61
return .{ .x = self .x , .y = self .y , .z = z };
57
62
}
58
63
};
@@ -68,16 +73,21 @@ pub fn Point3d(comptime T: type) type {
68
73
69
74
/// Assumes the point's coordinates are normalized between 0 and 1. Then it denormalizes
70
75
/// 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 ) {
72
77
return .{
73
78
.x = scaleDimension (self .x , x_dim ),
74
79
.y = scaleDimension (self .y , y_dim ),
75
80
.z = scaleDimension (self .z , z_dim ),
76
81
};
77
82
}
78
83
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
+
79
89
/// 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 {
81
91
return
82
92
// zig fmt: off
83
93
(self .x - other .x ) * (self .x - other .x ) +
@@ -87,23 +97,23 @@ pub fn Point3d(comptime T: type) type {
87
97
}
88
98
89
99
/// 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 {
91
101
return @sqrt (self .distance_squared (other ));
92
102
}
93
103
94
104
/// 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 ) {
96
106
return .{ .x = @floatCast (self .x ), .y = @floatCast (self .y ), .z = @floatCast (self .z ) };
97
107
}
98
108
99
109
/// 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 ) {
101
111
return .{ .x = self .x , .y = self .y };
102
112
}
103
113
104
114
/// Converts the 3d point to a 2d point in homogeneous coordinates, that is, by dividing
105
115
/// x and y by z.
106
- pub inline fn to2dHomogeneous (self : Point3d (T )) Point2d (T ) {
116
+ pub fn to2dHomogeneous (self : Point3d (T )) Point2d (T ) {
107
117
if (self .z == 0 ) {
108
118
return self .to2d ();
109
119
} else {
0 commit comments