Problem drawing triangulated non-convex polygon

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
bzSteve
Prole
Posts: 34
Joined: Tue May 21, 2013 2:31 am

Problem drawing triangulated non-convex polygon

Post by bzSteve »

I'm trying to work with concave geometries by splitting the polygons into triangles. Often, this works just fine. Sometimes, I get weird results when drawing the triangles like that line that goes out of bounds at the top right. I think this is a problem with love.graphics.polygon, but I'm not certain.

What's the proper way to do this?
Screen Shot 2016-12-19 at 2.36.01 PM.png
Screen Shot 2016-12-19 at 2.36.01 PM.png (15.64 KiB) Viewed 4054 times
Here's the whole program for this test:

Code: Select all

local concavePolygon = {
	0, 170,
	300, 100,
	600, 120,
	900, 100,
	1200, 170,
}


local triangles = love.math.triangulate ( concavePolygon )

local colors = {
	{ 255, 128, 128, 255 },
	{ 128, 255, 128, 255 },
	{ 128, 128, 255, 255 },
	{ 255, 255, 128, 255 },
	{ 255, 128, 255, 255 },
	{ 128, 255, 255, 255 },
}

love.window.setMode ( 1920, 1080 )


love.draw = function ( )
	for triNo, triangle in ipairs ( triangles ) do
		love.graphics.setColor ( colors [ triNo ] )
		love.graphics.polygon ( "line", triangle )
	end
	
	-- highlight the vertices...
	love.graphics.setColor ( 255, 255, 0, 255 )
	for ptNo = 1, #concavePolygon, 2 do
		local x = concavePolygon [ ptNo ]
		local y = concavePolygon [ ptNo + 1 ]
		
		love.graphics.circle ( "line", x, y, 3 )
	end
end
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem drawing triangulated non-convex polygon

Post by pgimeno »

Try [wiki]love.graphics.setLineJoin[/wiki]("bevel") (or "none", see [wiki]LineJoin[/wiki])

Edit: Note that the default is "miter". It's nice, but when the angle between lines is small, this happens: https://www.bignerdranch.com/assets/img ... imit-1.gif

(from https://www.bignerdranch.com/blog/core- ... ree-lines/)
Last edited by pgimeno on Mon Dec 19, 2016 11:27 pm, edited 1 time in total.
User avatar
bzSteve
Prole
Posts: 34
Joined: Tue May 21, 2013 2:31 am

Re: Problem drawing triangulated non-convex polygon

Post by bzSteve »

Thanks, pgimeno! That solved my problem. Much appreciated.

Just to understand, why does miter fail?
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem drawing triangulated non-convex polygon

Post by pgimeno »

See edit ^

The problem is basically that lines have thickness, i.e. they have a left and a right border, and that is taken into account even for 1-pixel lines.
User avatar
bzSteve
Prole
Posts: 34
Joined: Tue May 21, 2013 2:31 am

Re: Problem drawing triangulated non-convex polygon

Post by bzSteve »

Thanks for your help and for explaining that. I've been using LÖVE since 0.8.0 and I'm still learning!
Post Reply

Who is online

Users browsing this forum: No registered users and 198 guests