Difference between revisions of "love.graphics.polygon"
(→Rectangle with angle) |
(→Examples: Cleaned up examples.) |
||
Line 26: | Line 26: | ||
Nothing. | Nothing. | ||
== Examples == | == Examples == | ||
+ | |||
+ | |||
=== Two ways of drawing the same triangle === | === Two ways of drawing the same triangle === | ||
[[File:Polygon_triangle.png|upright=0.5|thumb|right|top|Triangle drawn using love.graphics.polygon]] | [[File:Polygon_triangle.png|upright=0.5|thumb|right|top|Triangle drawn using love.graphics.polygon]] | ||
This example shows how to give the coordinates explicitly and how to pass a table argument. | This example shows how to give the coordinates explicitly and how to pass a table argument. | ||
+ | |||
<source lang="lua"> | <source lang="lua"> | ||
− | -- | + | -- Giving the coordinates directly. |
− | love.graphics.polygon( | + | 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} | + | local vertices = {100,100, 200,100, 150,200} |
− | -- | + | -- Passing the table to the function as a second argument. |
− | love.graphics.polygon( | + | love.graphics.polygon("fill", vertices) |
</source> | </source> | ||
− | === | + | === Draw concave polygon === |
− | |||
<source lang="lua"> | <source lang="lua"> | ||
− | local vertices = {100,100,200,100,200,200,300,200,300,300,100,300} -- | + | local vertices = {100,100, 200,100, 200,200, 300,200, 300,300, 100,300} -- Concave "L" shape. |
local triangles = love.math.triangulate(vertices) | local triangles = love.math.triangulate(vertices) | ||
− | for i, triangle in | + | |
− | love.graphics.polygon( | + | for i, triangle in ipairs(triangles) do |
+ | love.graphics.polygon("fill", triangle) | ||
end | end | ||
− | |||
− | |||
</source> | </source> | ||
− | === | + | === Draw rotated rectangle === |
− | + | This is one way to draw a rotated [[love.graphics.rectangle|rectangle]]. | |
<source lang="lua"> | <source lang="lua"> | ||
− | function | + | function drawRotatedRectangle(mode, x, y, width, height, angle) |
− | |||
local cosa, sina = math.cos(angle), math.sin(angle) | local cosa, sina = math.cos(angle), math.sin(angle) | ||
− | local dx1, dy1 = width*cosa, width*sina | + | |
+ | local dx1, dy1 = width*cosa, width*sina | ||
local dx2, dy2 = -height*sina, height*cosa | local dx2, dy2 = -height*sina, height*cosa | ||
− | local px1, py1 = x, y | + | |
− | local px2, py2 = x + dx1, y + dy1 | + | local px1, py1 = x, y |
− | local px3, py3 = x + dx1 + dx2, y + dy1 + dy2 | + | local px2, py2 = x+dx1, y+dy1 |
− | local px4, py4 = x + dx2, y + dy2 | + | local px3, py3 = x+dx1+dx2, y+dy1+dy2 |
+ | local px4, py4 = x+dx2, y+dy2 | ||
− | love.graphics.polygon( mode, px1, py1, px2, py2, px3, py3, px4, py4) | + | love.graphics.polygon(mode, px1,py1, px2,py2, px3,py3, px4,py4) |
end | end | ||
</source> | </source> |
Revision as of 02:38, 16 July 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.
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+. |
Contents
Function
Synopsis
love.graphics.polygon( mode, ... )
Arguments
Returns
Nothing.
Function
Synopsis
love.graphics.polygon( mode, vertices )
Arguments
Returns
Nothing.
Examples
Two ways of drawing the same triangle
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)
Draw concave polygon
local vertices = {100,100, 200,100, 200,200, 300,200, 300,300, 100,300} -- Concave "L" shape.
local triangles = love.math.triangulate(vertices)
for i, triangle in ipairs(triangles) do
love.graphics.polygon("fill", triangle)
end
Draw rotated rectangle
This is one way to draw a rotated rectangle.
function drawRotatedRectangle(mode, x, y, width, height, angle)
local cosa, sina = math.cos(angle), math.sin(angle)
local dx1, dy1 = width*cosa, width*sina
local dx2, dy2 = -height*sina, height*cosa
local px1, py1 = x, y
local px2, py2 = x+dx1, y+dy1
local px3, py3 = x+dx1+dx2, y+dy1+dy2
local px4, py4 = x+dx2, y+dy2
love.graphics.polygon(mode, px1,py1, px2,py2, px3,py3, px4,py4)
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info