Difference between revisions of "love.graphics.polygon"

m (Newin template)
(Examples)
Line 39: Line 39:
 
-- passing the table to the function as a second argument
 
-- passing the table to the function as a second argument
 
love.graphics.polygon('fill', vertices)
 
love.graphics.polygon('fill', vertices)
 +
</source>
 +
 +
 +
=== Concave polygon ===
 +
 +
<source lang="lua">
 +
local vertices = {100,100,200,100,200,200,300,200,300,300,100,300} -- concave
 +
local triangles = love.math.triangulate(vertices)
 +
for i, triangle in pairs (triangles) do
 +
love.graphics.polygon( 'fill', triangle)
 +
end
 +
love.graphics.setColor(1, 1, 0, 1)
 +
love.graphics.polygon('line', vertices)
 
</source>
 
</source>
  

Revision as of 21:42, 6 June 2021

Available since LÖVE 0.4.0
This function is not supported in earlier versions.

Draw a polygon.

Following the mode argument, this function can accept multiple numeric arguments or a single table of numeric arguments. In either case the arguments are interpreted as alternating x and y coordinates of the polygon's vertices.

O.png When in fill mode, the polygon must be convex and simple or rendering artifacts may occur. love.math.triangulate and love.math.isConvex can be used in 0.9.0+.  


Function

Synopsis

love.graphics.polygon( mode, ... )

Arguments

DrawMode mode
How to draw the polygon.
number ...
The vertices of the polygon.

Returns

Nothing.

Function

Synopsis

love.graphics.polygon( mode, vertices )

Arguments

DrawMode mode
How to draw the polygon.
table vertices
The vertices of the polygon as a table.

Returns

Nothing.

Examples

Two ways of drawing the same triangle

Triangle drawn using love.graphics.polygon

This example shows how to give the coordinates explicitly and how to pass a table argument.

-- giving the coordinates directly
love.graphics.polygon('fill', 100, 100, 200, 100, 150, 200)

-- defining a table with the coordinates
-- this table could be built incrementally too
local vertices = {100, 100, 200, 100, 150, 200}

-- passing the table to the function as a second argument
love.graphics.polygon('fill', vertices)


Concave polygon

local vertices = {100,100,200,100,200,200,300,200,300,300,100,300} -- concave
local triangles = love.math.triangulate(vertices)
for i, triangle in pairs (triangles) do
	love.graphics.polygon( 'fill', triangle)
end
love.graphics.setColor(1, 1, 0, 1)
love.graphics.polygon('line', vertices)

See Also


Other Languages