FANDOM


line( [x0,] [y0,] [x1,] [y1,] [color] )
Draws a line between two points.
x0
The x coordinate of the start of the line. If omitted, the x coordinate of the end of the previous line is used, or 0 if no previous line has been drawn.

y0
The y coordinate of the start of the line. If omitted, the y coordinate of the end of the previous line is used, or 0 if no previous line has been drawn.

x1
The x coordinate of the end of the line.

y1
The y coordinate of the end of the line.

color
The color of the line. If omitted, the color from the draw state is used. This also sets the color in the draw state.

The line() function is very versatile, with different operations depending on how many arguments are supplied. Line segments, strips, and loops are all easily created with this function.

This is how PICO-8 interprets each possible set of arguments:

  • line(x0, y0, x1, y1, color) - Draws a line from (x0, y0) to (x1, y1) in the given color. Remembers (x1, y1) as the current endpoint and color as the current pen color.
  • line(x0, y0, x1, y1) - Draws a line from (x0, y0) to (x1, y1) in the current pen color. Remembers (x1, y1) as the current endpoint.
  • line(x1, y1, color) - Draws a line from the current endpoint to (x1, y1) in the given color. If there is no current endpoint, nothing is drawn. Remembers (x1, y1) as the current endpoint and color as the current pen color.
  • line(x1, y1) - Draws a line from the current endpoint to (x1, y1) in the current pen color. If there is no current endpoint, nothing is drawn. Remembers (x1, y1) as the current endpoint.
  • line(color) - Invalidates the current endpoint. Remembers color as the current pen color.
  • line() - Invalidates the current endpoint.

Examples Edit

-- draw 20 white lines in random locations
color(7)
for x=1,20 do
  line(rnd(128), rnd(128), rnd(128), rnd(128))
end
 
-- draw a red diamond shape using separate line segments
line(63, 0, 126, 63, 8)  -- this first line sets color to 8/red
line(126, 63, 63, 126)   -- no need to set color again
line(63, 126, 0, 63)
line(0, 63, 63, 0)
 
-- draw the same red diamond shape using line continuation
line(63, 0, 126, 63, 8)
line(63, 126)
line(0, 63)
line(63, 0)
 
-- draw a regular n-gon using line continuation
function ngon(x, y, r, n, color)
  line(color)            -- invalidate current endpoint, set color
  for i=0,n do
    local angle = i/n
    line(x + r*cos(angle), y + r*sin(angle))
  end
end
ngon(64, 64, 50, 8, 12)  -- a blue octagon

See also Edit

Community content is available under CC-BY-SA unless otherwise noted.