Difference between revisions of "love.graphics.line"

(Draw a line from the center of the screen to the mouse pointer)
(Draw a zigzag line from a single table)
Line 62: Line 62:
 
end
 
end
 
</source>
 
</source>
 +
 +
 +
=== Draw grid ===
 +
<source lang="lua">
 +
w = love.graphics.getWidth() -- window width
 +
h = love.graphics.getHeight() -- window height
 +
grid = {}
 +
distance = 30 -- distance between lines
 +
for x = distance, w, distance do
 +
local line = {x, 0, x, h} -- vertical line
 +
table.insert (grid, line)
 +
end
 +
for y = distance, h, distance do
 +
local line = {0, y, w, y} -- horizontal line
 +
table.insert (grid, line)
 +
end
 +
 +
function draw_lines (lines)
 +
for i, line in pairs (lines) do
 +
love.graphics.line(line)
 +
end
 +
end
 +
 +
function love.draw()
 +
draw_lines (grid)
 +
end
 +
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]

Revision as of 13:58, 13 June 2021

Draws lines between points.

Function

Synopsis

love.graphics.line( x1, y1, x2, y2, ... )

Arguments

number x1
The position of first point on the x-axis.
number y1
The position of first point on the y-axis.
number x2
The position of second point on the x-axis.
number y2
The position of second point on the y-axis.
number ...
You can continue passing point positions to draw a polyline.

Returns

Nothing.

Function

Synopsis

love.graphics.line( points )

Arguments

table points
A table of point positions, as described above.

Returns

Nothing.

Examples

Draw the outline of a simple trapezoid

function love.draw()
   love.graphics.line(200,50, 400,50, 500,300, 100,300, 200,50)   -- last pair is a repeat to complete the trapezoid
end

Draw a line from the center of the screen to the mouse pointer

w = love.graphics.getWidth() / 2   -- half the window width
h = love.graphics.getHeight() / 2   -- half the window height
function love.draw()
   local mx, my = love.mouse.getPosition()  -- current position of the mouse
   love.graphics.line(w, h, mx, my)
end

Draw horizontal and vertical lines to the mouse pointer

w = love.graphics.getWidth() -- window width
h = love.graphics.getHeight() -- window height
function love.draw()
	local mx, my = love.mouse.getPosition() -- current position of the mouse
	love.graphics.line(0, my, w, my) -- horizontal line
	love.graphics.line(mx, 0, mx, h) -- vertical line
end

Draw a zigzag line from a single table

sometable = {
   100, 100,
   200, 200,
   300, 100,
   400, 200,
}
function love.draw()
   love.graphics.line(sometable)
end


Draw grid

w = love.graphics.getWidth() -- window width
h = love.graphics.getHeight() -- window height
grid = {}
distance = 30 -- distance between lines
for x = distance, w, distance do
	local line = {x, 0, x, h} -- vertical line
	table.insert (grid, line)
end
for y = distance, h, distance do
	local line = {0, y, w, y} -- horizontal line
	table.insert (grid, line)
end

function draw_lines (lines)
	for i, line in pairs (lines) do
		love.graphics.line(line)
	end
end

function love.draw()
	draw_lines (grid)
end

See Also


Other Languages