Skip to content

Commit 8d1311b

Browse files
authored
cube/pos.go: Added new Min/Max and Range3D functions. (#1139)
1 parent 03f3847 commit 8d1311b

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

server/block/cube/pos.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cube
22

33
import (
44
"fmt"
5-
"github.com/go-gl/mathgl/mgl64"
5+
"iter"
66
"math"
7+
8+
"github.com/go-gl/mathgl/mgl64"
79
)
810

911
// Pos holds the position of a block. The position is represented as an array
@@ -136,3 +138,32 @@ func (p Pos) Neighbours(f func(neighbour Pos), r Range) {
136138
func PosFromVec3(vec3 mgl64.Vec3) Pos {
137139
return Pos{int(math.Floor(vec3[0])), int(math.Floor(vec3[1])), int(math.Floor(vec3[2]))}
138140
}
141+
142+
// Min returns a new position where each coordinate is the minimum
143+
// of input positions p1 and p2.
144+
func Min(p1, p2 Pos) Pos {
145+
return Pos{min(p1[0], p2[0]), min(p1[1], p2[1]), min(p1[2], p2[2])}
146+
}
147+
148+
// Max returns a new position where each coordinate is the maximum
149+
// of input positions p1 and p2.
150+
func Max(p1, p2 Pos) Pos {
151+
return Pos{max(p1[0], p2[0]), max(p1[1], p2[1]), max(p1[2], p2[2])}
152+
}
153+
154+
// Range3D returns iterator that iterates all points between minimum and maximum of p1 & p2.
155+
func Range3D(p1, p2 Pos) iter.Seq[Pos] {
156+
max := Max(p1, p2)
157+
min := Min(p1, p2)
158+
return func(yield func(Pos) bool) {
159+
for x := min[0]; x <= max[0]; x++ {
160+
for y := min[1]; y <= max[1]; y++ {
161+
for z := min[2]; z <= min[2]; z++ {
162+
if !yield(min.Add(Pos{x, y, z})) {
163+
return
164+
}
165+
}
166+
}
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)