diff options
| author | mat <git@matdoes.dev> | 2025-06-12 05:13:58 +1100 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-06-11 05:17:06 -1300 |
| commit | 067ec06f26ecaf7a319eb3ce61307b9730176313 (patch) | |
| tree | e70685f895bba258acda2f4a89b2202586450c2c /azalea-core/src | |
| parent | ab05e7bdae43de3595718fee877c0fbcc6368555 (diff) | |
| download | azalea-drasl-067ec06f26ecaf7a319eb3ce61307b9730176313.tar.xz | |
add BlockPos::distance_to and length
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/position.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index b1560577..d6e67dc2 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -419,6 +419,22 @@ impl BlockPos { pub fn offset_with_direction(self, direction: Direction) -> Self { self + direction.normal() } + + /// Get the distance (as an f64) of this BlockPos to the origin by + /// doing `sqrt(x^2 + y^2 + z^2)`. + pub fn length(&self) -> f64 { + f64::sqrt((self.x * self.x + self.y * self.y + self.z * self.z) as f64) + } + + /// Get the distance (as an f64) from this position to another position. + /// Equivalent to `(self - other).length()`. + /// + /// Note that if you're using this in a hot path, it may be more performant + /// to use [`BlockPos::distance_squared_to`] instead (by squaring the other + /// side in the comparison). + pub fn distance_to(self, other: Self) -> f64 { + (self - other).length() + } } /// Similar to [`BlockPos`] but it's serialized as 3 varints instead of one |
