1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

add BlockPos::distance_to and length

This commit is contained in:
mat 2025-06-12 05:13:58 +11:00
parent ab05e7bdae
commit 067ec06f26

View file

@ -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