Weird behavior with polygon fill vs line

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
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Weird behavior with polygon fill vs line

Post by clofresh »

Hey, I'm getting weird behavior for love.graphics.polygon:
polygon-fill-vs-line.png
polygon-fill-vs-line.png (11.11 KiB) Viewed 1722 times
1. The line segment from vertex 1 to 2 isn't connecting.
2. The fill is skipping vertex 2, 3 and 4.

Here is the code:

Code: Select all

function love.load()
    love.window.setMode(800, 600)
    vertices = {
        50, 136,
        114, 181,
        321, 107,
        422, 87,
        579, 57,
        609, 54,
        639, 52,
        653, 52,
        686, 61,
        750, 136,
        750, 380,
        50, 380,
        50, 136,
    }
end

function love.draw()
    math.randomseed(0)
    love.graphics.setColor(255, 0, 0)
    love.graphics.polygon('fill', vertices)
    love.graphics.setColor(0, 255, 0)
    love.graphics.polygon('line', vertices)

    love.graphics.setPointSize(5)
    for i = 1, #vertices, 2 do
        love.graphics.setColor(0, 0, 255)
        love.graphics.point(vertices[i], vertices[i + 1])
        love.graphics.print(i/2 + 0.5, vertices[i] + math.random(-1, 1), vertices[i + 1] + math.random(-10, 10))
    end

    love.graphics.setColor(255, 255, 255)
    love.graphics.print(string.format('convex: %s', love.math.isConvex(vertices)))
end
Running love 0.9.1 on Windows 7.


Edit: Resolved in IRC thanks to bartbes. The issue is with last vertex, which is a repeat of the first one. I thought you had to close the polygon that way, but turns out you don't. If I remove the repeat vertex, the line segments get drawn correctly, but the fill is still wrong because the polygon is not convex, as reflected correctly by love.math.isConvex().
polygon-fill-vs-line2.png
polygon-fill-vs-line2.png (13.89 KiB) Viewed 1715 times
Attachments
weird-polygon.love
(508 Bytes) Downloaded 90 times
Last edited by clofresh on Wed Nov 19, 2014 3:23 pm, edited 1 time in total.
----------------------------------------
Sluicer Games
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Weird behavior with polygon fill vs line

Post by Nixola »

[wiki]love.graphics.polygon[/wiki] can only draw convex polygons. You'll need to [wiki]love.math.triangulate[/wiki] it.
The bug isn't in love.graphics.polygon, but either in love.math.isConvex or in your code.
EDIT: Well, good to know everything's solved!
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 204 guests