Compare commits

..

1 commit

Author SHA1 Message Date
71f98fd831 Centered grid 2025-10-24 12:52:33 +03:00
3 changed files with 8 additions and 5 deletions

View file

@ -7,6 +7,7 @@ VERSION = 'v0.2'
---@field pointRadius number Radius of each grid point, relative to cellSize ---@field pointRadius number Radius of each grid point, relative to cellSize
---@field linePointRadius number Radius of line start/end points, relative to cellSize ---@field linePointRadius number Radius of line start/end points, relative to cellSize
---@field cellSize number Size of each grid cell ---@field cellSize number Size of each grid cell
---@field gridOffset number X offset of grid processing
---@field lineStyle string love2d line style setting ---@field lineStyle string love2d line style setting
---@field lineWidth number love2d line width setting ---@field lineWidth number love2d line width setting
---@field menuLineWidth number love2d line width for buttons ---@field menuLineWidth number love2d line width for buttons
@ -17,6 +18,7 @@ Config = {
linePointRadius = 0.4, linePointRadius = 0.4,
cellSize = 30, cellSize = 30,
gridOffset = 160,
lineStyle = "smooth", lineStyle = "smooth",
lineWidth = 0.1, lineWidth = 0.1,

View file

@ -39,8 +39,9 @@ function Grid:draw()
end end
for y = 1, self.size.y do for y = 1, self.size.y do
local px, py = x * Config.cellSize, y * Config.cellSize local gp = GridPoint:new(x, y)
love.graphics.circle( "fill", px - Config.cellSize / 2, py - Config.cellSize / 2, Config.cellSize * Config.pointRadius) local px, py = gp:globalCoords()
love.graphics.circle( "fill", px, py, Config.cellSize * Config.pointRadius)
end end
end end
-- Draw lines -- Draw lines

View file

@ -9,7 +9,7 @@ GridPoint = Point
---@return Point ---@return Point
function GridPoint:absolute() function GridPoint:absolute()
return Point:new( return Point:new(
self.x * Config.cellSize, self.x * Config.cellSize + Config.gridOffset,
self.y * Config.cellSize self.y * Config.cellSize
) )
end end
@ -17,7 +17,7 @@ end
-- Same as coords, but converted to global coords -- Same as coords, but converted to global coords
---@return number, number ---@return number, number
function GridPoint:globalCoords() function GridPoint:globalCoords()
return self.x * Config.cellSize - Config.cellSize / 2, return self.x * Config.cellSize - Config.cellSize / 2 + Config.gridOffset,
self.y * Config.cellSize - Config.cellSize / 2 self.y * Config.cellSize - Config.cellSize / 2
end end
@ -25,7 +25,7 @@ end
---@param point Point ---@param point Point
---@return GridPoint ---@return GridPoint
function GridPoint.snapCoords( point ) function GridPoint.snapCoords( point )
local x = math.ceil( point.x / Config.cellSize ) local x = math.ceil( (point.x - Config.gridOffset) / Config.cellSize )
local y = math.ceil( point.y / Config.cellSize ) local y = math.ceil( point.y / Config.cellSize )
return GridPoint:new( x, y ) return GridPoint:new( x, y )
end end