Polygon made from mouse clicks

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nico
Prole
Posts: 6
Joined: Mon Jul 30, 2012 6:14 pm

Polygon made from mouse clicks

Post 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
   
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Polygon made from mouse clicks

Post 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:
User avatar
Nico
Prole
Posts: 6
Joined: Mon Jul 30, 2012 6:14 pm

Re: Polygon made from mouse clicks

Post by Nico »

Thanks you :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 6 guests