Skip to content

Commit d674f37

Browse files
committed
added ChunkPos distance operations
1 parent 3ea55a6 commit d674f37

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/main/kotlin/com/lambda/util/math/Vectors.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.lambda.util.math.MathUtils.floorToInt
2121
import com.lambda.util.math.MathUtils.sq
2222
import net.minecraft.entity.Entity
2323
import net.minecraft.util.math.BlockPos
24+
import net.minecraft.util.math.ChunkPos
2425
import net.minecraft.util.math.Direction
2526
import net.minecraft.util.math.EightWayDirection
2627
import net.minecraft.util.math.Vec2f
@@ -78,6 +79,19 @@ infix operator fun Vec2f.div(other: Int): Vec2f = times(1.0 / other)
7879

7980
operator fun Vec2f.unaryMinus(): Vec2f = negate()
8081

82+
/* Chunk */
83+
val ChunkPos.center: ChunkPos
84+
get() = ChunkPos(centerX, centerZ)
85+
86+
infix fun ChunkPos.dist(other: Vec3i): Double = other dist this
87+
infix fun ChunkPos.dist(other: Vec3d): Double = other dist this
88+
infix fun ChunkPos.dist(other: ChunkPos): Double = sqrt((this distSq other).toDouble())
89+
infix fun ChunkPos.distCenter(other: ChunkPos): Double = sqrt((this distSqCenter other).toDouble())
90+
infix fun ChunkPos.distSq(other: Vec3i): Int = other distSq this
91+
infix fun ChunkPos.distSq(other: Vec3d): Double = other distSq this
92+
infix fun ChunkPos.distSq(other: ChunkPos): Int = (other.x - x).sq + (other.z - z).sq
93+
infix fun ChunkPos.distSqCenter(other: ChunkPos): Int = (other.centerX - centerX).sq + (other.centerZ - centerZ).sq
94+
8195
/* Vec3d */
8296
fun Vec3d.approximate(other: Vec3d, precision: Double = 2.0E-4): Boolean =
8397
(subtract(other) distSq Vec3d.ZERO) > precision.pow(2)
@@ -100,9 +114,10 @@ operator fun Vec3d.component3() = z
100114

101115
infix fun Vec3d.dist(other: Vec3d): Double = sqrt(this distSq other)
102116
infix fun Vec3d.dist(other: Vec3i): Double = sqrt(this distSq other)
103-
infix fun Vec3d.distSq(other: Vec3d): Double = squaredDistanceTo(other)
104-
infix fun Vec3d.distSq(other: Vec3i): Double =
105-
squaredDistanceTo(other.x.toDouble(), other.y.toDouble(), other.z.toDouble())
117+
infix fun Vec3d.dist(other: ChunkPos): Double = sqrt(this distSq other)
118+
infix fun Vec3d.distSq(other: Vec3d): Double = (other.x - x).sq + (other.y - y).sq + (other.z - z).sq
119+
infix fun Vec3d.distSq(other: Vec3i): Double = (other.x - x).sq + (other.y - y).sq + (other.z - z).sq
120+
infix fun Vec3d.distSq(other: ChunkPos): Double = (other.x - x).sq + (other.z - z).sq
106121

107122
infix operator fun Vec3d.plus(other: Vec3d): Vec3d = add(other)
108123
infix operator fun Vec3d.plus(other: Vec3i): Vec3d = Vec3d(x + other.x, y + other.y, z + other.z)
@@ -148,8 +163,10 @@ val Vec3i.vec3d
148163

149164
infix fun Vec3i.dist(other: Vec3d): Double = sqrt(this distSq other)
150165
infix fun Vec3i.dist(other: Vec3i): Double = sqrt((this distSq other).toDouble())
166+
infix fun Vec3i.dist(other: ChunkPos): Double = sqrt((this distSq other).toDouble())
151167
infix fun Vec3i.distSq(other: Vec3d): Double = getSquaredDistance(other)
152168
infix fun Vec3i.distSq(other: Vec3i): Int = (x - other.x).sq + (y - other.y).sq + (z - other.z).sq
169+
infix fun Vec3i.distSq(other: ChunkPos): Int = (other.x - x).sq + (other.z - z).sq
153170

154171
infix operator fun Vec3i.plus(other: Vec3i): Vec3i = add(other)
155172
infix operator fun Vec3i.plus(other: Int): Vec3i = add(other, other, other)

0 commit comments

Comments
 (0)