Page 1 of 1

Polygon made from mouse clicks

Posted: Thu Aug 09, 2012 6:13 am
by Nico
Hello, im trying to make a polygon from the user clicks, but i cant figure out how to do it correctly.
Here is what i did.

Code: Select all

function love.mousepressed(x, y, button)
	if button == 'l' then
	
		if(first == true) then
			first = false
			fx = x
			fy = y
		end
		table.insert(points,{px = x-fx,py = y-fy})

		
	elseif button == 'r' then
		local pol = {}
		for i=0,# points,2 do
			pol[i] = points[i].px
			pol[i+1] = points[i].py
		end
		-- pol is the table with the array to draw the polygon

		points = {}
	end
   

Re: Polygon made from mouse clicks

Posted: Thu Aug 09, 2012 7:22 am
by Roland_Yonaba
Shouldn't this be enough ?

Code: Select all

function love.load()
	polygon = {}
end

function love.draw()
	if #polygon> 4 then 
		love.graphics.polygon('line',polygon)
	end
end

function love.mousepressed(x,y,button)
	if button =='l' then
		table.insert(polygon,x)
		table.insert(polygon,y)	
	end
end
The #polygon>4 check is just because love.graphics.polygon requires at least 3 vertices. See the wiki, useful stuff there. :crazy:

Re: Polygon made from mouse clicks

Posted: Thu Aug 09, 2012 2:37 pm
by Nico
Thanks you :)