Difference between revisions of "love.graphics.line"

m (Fix typo.)
 
(5 intermediate revisions by 2 users not shown)
Line 23: Line 23:
 
Nothing.
 
Nothing.
 
== Examples ==
 
== Examples ==
Draw the outline of a simple trapezoid.
+
 
 +
=== Draw the outline of a simple trapezoid ===
 
<source lang="lua">
 
<source lang="lua">
 
function love.draw()
 
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
+
love.graphics.line(200,50, 400,50, 500,300, 100,300, 200,50)  -- last pair is a repeat to complete the trapezoid
 
end
 
end
 
</source>
 
</source>
Draw a line from the center of the screen to the mouse pointer.
+
 
 +
=== Draw a line from the center of the screen to the mouse pointer ===
 
<source lang="lua">
 
<source lang="lua">
w = love.graphics.getWidth() / 2  -- half the window width
 
h = love.graphics.getHeight() / 2  -- half the window height
 
 
function love.draw()
 
function love.draw()
  local mx, my = love.mouse.getPosition() -- current position of the mouse
+
local mx, my = love.mouse.getPosition()
  love.graphics.line(w, h, mx, my)
+
 
 +
local windowWidth, windowHeight = love.graphics.getDimensions()
 +
 
 +
love.graphics.line(windowWidth/2, windowHeight/2, mx, my)
 
end
 
end
 
</source>
 
</source>
Draw a zigzag line from a single table.
+
 
 +
=== Draw a zigzag line using a table ===
 
<source lang="lua">
 
<source lang="lua">
sometable = {
+
local zigzagLine = {100,100, 200,200, 300,100, 400,200}
  100, 100,
+
 
  200, 200,
 
  300, 100,
 
  400, 200,
 
}
 
 
function love.draw()
 
function love.draw()
  love.graphics.line(sometable)
+
love.graphics.line(zigzagLine)
 
end
 
end
 
</source>
 
</source>
 +
 +
=== Generate and draw a grid ===
 +
<source lang="lua">
 +
local cellSize  = 30 -- Width and height of cells.
 +
local gridLines = {}
 +
 +
local windowWidth, windowHeight = love.graphics.getDimensions()
 +
 +
-- Vertical lines.
 +
for x = cellSize, windowWidth, cellSize do
 +
local line = {x, 0, x, windowHeight}
 +
table.insert(gridLines, line)
 +
end
 +
-- Horizontal lines.
 +
for y = cellSize, windowHeight, cellSize do
 +
local line = {0, y, windowWidth, y}
 +
table.insert(gridLines, line)
 +
end
 +
 +
function love.draw()
 +
love.graphics.setLineWidth(2)
 +
 +
for i, line in ipairs(gridLines) do
 +
love.graphics.line(line)
 +
end
 +
end
 +
</source>
 +
 +
 +
=== Draw temp line with mouse ===
 +
 +
<source lang="lua">
 +
tempLine = {}
 +
 +
 +
local function drawLine (line)
 +
 +
end
 +
 +
function love.draw()
 +
-- check if line has 4 coordinates:
 +
if #tempLine > 3 then
 +
love.graphics.line (tempLine)
 +
end
 +
end
 +
 +
function love.mousepressed(x, y)
 +
-- set first point:
 +
tempLine = {x, y}
 +
end
 +
 +
function love.mousemoved (x, y)
 +
-- update second point:
 +
tempLine[3] = x
 +
tempLine[4] = y
 +
end
 +
 +
function love.mousereleased ( x, y)
 +
-- get your temp line coordinates as:
 +
-- local x1, y1 = tempLine[1], tempLine[2]
 +
-- local x2, y2 = tempLine[3], tempLine[4]
 +
 +
-- clean temp line:
 +
tempLine = {}
 +
end
 +
 +
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
* [[love.graphics.setLine]]
 
 
* [[love.graphics.setLineWidth]]
 
* [[love.graphics.setLineWidth]]
 
* [[love.graphics.setLineStyle]]
 
* [[love.graphics.setLineStyle]]
Line 59: Line 126:
 
{{#set:Description=Draws lines between points.}}
 
{{#set:Description=Draws lines between points.}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 +
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.line}}
 
{{i18n|love.graphics.line}}

Latest revision as of 08:19, 21 July 2023

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

function love.draw()
	local mx, my = love.mouse.getPosition()

	local windowWidth, windowHeight = love.graphics.getDimensions()

	love.graphics.line(windowWidth/2, windowHeight/2, mx, my)
end

Draw a zigzag line using a table

local zigzagLine = {100,100, 200,200, 300,100, 400,200}

function love.draw()
	love.graphics.line(zigzagLine)
end

Generate and draw a grid

local cellSize  = 30 -- Width and height of cells.
local gridLines = {}

local windowWidth, windowHeight = love.graphics.getDimensions()

-- Vertical lines.
for x = cellSize, windowWidth, cellSize do
	local line = {x, 0, x, windowHeight}
	table.insert(gridLines, line)
end
-- Horizontal lines.
for y = cellSize, windowHeight, cellSize do
	local line = {0, y, windowWidth, y}
	table.insert(gridLines, line)
end

function love.draw()
	love.graphics.setLineWidth(2)

	for i, line in ipairs(gridLines) do
		love.graphics.line(line)
	end
end


Draw temp line with mouse

tempLine = {}


local function drawLine (line)

end

function love.draw()
--	check if line has 4 coordinates:
	if #tempLine > 3 then
		love.graphics.line (tempLine)
	end
end

function love.mousepressed(x, y)
--	set first point:
	tempLine = {x, y}
end

function love.mousemoved (x, y)
--	update second point:
	tempLine[3] = x
	tempLine[4] = y
end

function love.mousereleased ( x, y)
-- get your temp line coordinates as:
--	local x1, y1 = tempLine[1], tempLine[2]
--	local x2, y2 = tempLine[3], tempLine[4]

-- clean temp line:	
	tempLine = {}
end

See Also



Other Languages