draw dots in a straight line from PointX to PointY
BlitzMax Forums/BlitzMax Programming/draw dots in a straight line from PointX to PointY
| ||
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, |
| ||
look up bresenham in the code archives to get x and y coords in a line |
| ||
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! |
| ||
dotcount depends on distance, if you want to draw dots at regular intervals? |
| ||
Use bresenham. Alternatively you can use 2d vectors and just step from source to dest. |
| ||
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! |
| ||
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 |
| ||
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! |