Files
ngzz-mc/internal/coord/coord.go

26 lines
644 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package coord 坐标工具:floorDiv 与区块坐标换算(设计.md §4.1)。
// 关键:世界坐标转区块坐标必须使用 floorDiv,正确处理负数。
package coord
// FloorDiv 向下取整除法(负安全)。
func FloorDiv(a, b int32) int32 {
q := a / b
r := a % b
if r < 0 {
q--
}
return q
}
// ChunkCoord 世界坐标 → 区块坐标(区块大小 16)。
func ChunkCoord(worldX int32) int32 { return FloorDiv(worldX, 16) }
// LocalCoord 世界坐标 → 区块内局部坐标(0..15)。
func LocalCoord(worldX int32) int32 {
l := worldX - ChunkCoord(worldX)*16
if l < 0 {
l += 16
}
return l
}