Skip to content

Instantly share code, notes, and snippets.

@kahrl
Created July 9, 2013 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahrl/5957422 to your computer and use it in GitHub Desktop.
Save kahrl/5957422 to your computer and use it in GitHub Desktop.
diff --git a/builtin/voxelarea.lua b/builtin/voxelarea.lua
index dd9af79..f546cd3 100644
--- a/builtin/voxelarea.lua
+++ b/builtin/voxelarea.lua
@@ -60,3 +60,28 @@ function VoxelArea:containsi(i)
return (i >= 1) and (i <= self:getVolume())
end
+function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
+ local i = self:index(minx, miny, minz) - 1
+ local last = self:index(maxx, maxy, maxz)
+ local ystride = self.ystride
+ local zstride = self.zstride
+ local yoff = (last+1) % ystride
+ local zoff = (last+1) % zstride
+ local ystridediff = (i - last) % ystride
+ local zstridediff = (i - last) % zstride
+ return function()
+ i = i + 1
+ if i % zstride == zoff then
+ i = i + zstridediff
+ elseif i % ystride == yoff then
+ i = i + ystridediff
+ end
+ if i <= last then
+ return i
+ end
+ end
+end
+
+function VoxelArea:iterp(minp, maxp)
+ return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment