draw dots in a straight line from PointX to PointY

BlitzMax Forums/BlitzMax Programming/draw dots in a straight line from PointX to PointY

WERDNA(Posted 2011) [#1]
Hey folks!

I have a series of dot images that I need to draw in a straight line from PointX,
to PointY, with spaces in between the dots.



These are the dots in question. Basically I want to use each planet as a 'node',
taking the location of two planets, and drawing a line of dots in between those
two planets.(Ignore the curving dots between the last two planets ;)

Thanks in advance,


slenkar(Posted 2011) [#2]
look up bresenham in the code archives to get x and y coords in a line


_JIM(Posted 2011) [#3]
That's rather simple actually. Linear interpolation is your frind on this one:


Function DrawDots(x1#, y1#, x2#, y2#, dotcount%)

for local i% = 0 until dotcount

'here comes the math
local factor# = i / float(dotcount)

'the factor is actually a proportion, or a percentage if you want
'it indicates the position on the imaginary line between the 2 points

'now compute the position using linear interpolation (aka LERP in some languages)
local newx# = (x2 - x1) * factor + x1
local newy# = (y2 - y1) * factor + y1

'draw the pretty shiny fabulous dot at the computed position
DrawDot(newx, newy)

next

EndFunction



I wrote that in the codebox so there's a 0.000001% chance it won't work. But I hope you get the idea :)

Also, after you understand how it works, you could optimize it by removing the extra locals and just writing a single line with "DrawDot...".

Have fun!


slenkar(Posted 2011) [#4]
dotcount depends on distance, if you want to draw dots at regular intervals?


GW(Posted 2011) [#5]
Use bresenham. Alternatively you can use 2d vectors and just step from source to dest.


WERDNA(Posted 2011) [#6]
Thanks everyone!

I think I'll give _JIM's code a try, since I much prefer it's simplicity compared to bresenham.
If that doesn't work however, guess I'll give bresenham a shot :)

Cheers!


Warpy(Posted 2011) [#7]
Bresenham isn't the right algorithm for this - the dots are a lot bigger than single pixels, so 'stupid' linear interpolation should do the job satisfactorily.

However, if you want to make sure you definitely draw the start and end points of a line with all the dots spaced evenly, or if you want to draw a line made up of several segments, it gets a bit more complicated.

Here's some code, with a couple of slightly different methods:



edit: code archive entry here and maths code blog post here.

Last edited 2011


WERDNA(Posted 2011) [#8]
Thanks for the code and links Warpy :)

I've found _JIM's to be more than satisfactory. Just added an extra parameter to the
function that determines whether the dots are yellow or red, and everything works
great.

Thanks everyone!