Making a Dotted/Dashed Line

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
kexisse
Citizen
Posts: 56
Joined: Wed Jun 13, 2012 2:52 pm

Making a Dotted/Dashed Line

Post by kexisse »

I'm trying to draw a dotted/dashed line.
I just found setLineStipple but it's removed in Löve 0.8.
Is there an alternative method?
Anickyan
Prole
Posts: 27
Joined: Sun Aug 05, 2012 8:42 am

Re: Making a Dotted/Dashed Line

Post by Anickyan »

You can create your own line drawing function. It would typically look something like this:

Code: Select all

function drawLine(x1, y1, x2, y2)
  love.graphics.setPointSize(1)

  local x, y = x2 - x1, y2 - y1
  local len = math.sqrt(x^2 + y^2)
  local stepx, stepy = x / len, y / len
  x = x1
  y = y1

  for i = 1, len do
    love.graphics.point(x, y)
    x = x + stepx
    y = y + stepy
  end
end
Now think of how you could make a strippled line. Thinking it up yourself always helps learning!
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making a Dotted/Dashed Line

Post by darkfrei »

Based on diagonal marching, between two points is distance as longest coordinate difference:

Code: Select all

function getLinePoints(x1, y1, x2, y2)
  local points = {}

  local deltaX, deltaY = x2 - x1, y2 - y1
  local len = math.max (math.abs(deltaX), math.abs(deltaY))

  for iStep = 0, len do -- including first and last points
    table.insert (points, x1 + deltaX * iStep/len)
    table.insert (points, y1 + deltaY * iStep/len)
  end
  return points
end
For dash line check

Code: Select all

if iStep%4 < 2 then -- add point 
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Making a Dotted/Dashed Line

Post by milon »

Here's a pretty elegant method that still works!
viewtopic.php?t=83295

EDIT - Wow, just realized this is quite a necro! >_<
But it's amazing that a draw method from 7 years ago still works!
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Making a Dotted/Dashed Line

Post by togFox »

Also noting love.graphic.point doesn't work any more.

love.graphic.points can be used exactly the same way.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 20 guests