Shape = class("Shape")

function Shape:new(col, row, tag)
	local shape = instance(self)
	shape.mask = {}
	shape.col = col
	shape.row = row
	shape.tag = tag
	shape.buffer = 0
	shape.bounds = Bounds:new(0,0,0,0)
	return shape
end

function Shape:update_bounds()
	local bounds = self.bounds
	bounds:set(0,0,0,0)
	for col, cells in pairs(self.mask) do
		for row, v in pairs(cells) do
			self.bounds:expand(col, row, col, row)
		end
	end
end

function Shape:set_layout_buffer(v)
	self.buffer = v	
end


function Shape:set(col, row, v)
	col = math.round(col)
	row = math.round(row)
	local mask = self.mask
	if not mask[col] then
		mask[col] = {}
	end
	mask[col][row] = v
end

function Shape:set_rectangle(x, y, w, h, v)
	for col=x,x+w do
		for row=y,y+h do
			self:set(col,row, v)
		end
	end
end

function Shape:set_centered_rectangle(x, y, w, h, v)
	for col=x-w*0.5,x+w*0.5 do
		for row=y-h*0.5,y+h*0.5 do
			self:set(col,row, v)
		end
	end
end

-- This should look like ____/~~~~
function Shape:set_line(x1, y1, x2, y2, v)
	while true do
		self:set(x1,y1, v)
		if x1 == x2 and y1 == y2 then
			break
		end
		local xd = math.abs(x1-x2)
		local yd = math.abs(y1-y2)
		if xd == yd then
			x1 = math.approach(x1,x2, 1)
			y1 = math.approach(y1,y2, 1)
		elseif xd > yd then
			x1 = math.approach(x1,x2, 1)
		else
			y1 = math.approach(y1,y2, 1)
		end
	end
end

function Shape:set_circle(x, y, r, v)
	for col=x-r,x+r do
		for row=y-r,y+r do
			local dist = math.distance(x,y,col,row)
			if dist <= r then
				self:set(col,row, v)
			end
		end
	end
end
