gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Graphics Algorithms

#103723 - TheWopr - Sun Sep 24, 2006 4:26 am

I'm working on creating a Tronish game for my first DS app, and I've come across a roadblock. When you move a dot, well its a dot for now, that leaves a trail behind it, it needs to be drawn as a line. Controlling the speed of the player is vital, it would be impractical for the player to move at 1 pixel an update. The only thing i can think of is every step i have to draw a line from the old point to the new one. Does anyone have any code i could use for drawing a line from point A to point B on a 16bit_256x256 background?

#103730 - Yare - Sun Sep 24, 2006 6:43 am

Search for the Digital Differential Analyzer, Bresenham, or Xiaolin Wu's Two-Step algorithms.

I listed them in ascending order of difficulty and performance.

#103737 - OOPMan - Sun Sep 24, 2006 8:52 am

Bresenham's is the standard starting off one :-)

Gah, I remember cutting my teeth on that stuff :-)

I recall being highly peeved off that the basic calculus formula y=mx+c doesn't work perfectly on PC platforms (Because you have to take certain special cases into account and so forth :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#103767 - bear - Sun Sep 24, 2006 1:48 pm

You could google for "graphics programming black book" and read about/rip the bresenham line algorithm from Michael Abrash's old classic asm/graphics programming book that is now freely available in .pdf form.

#103822 - Yare - Sun Sep 24, 2006 8:24 pm

OOPMan wrote:
Gah, I remember cutting my teeth on that stuff :-)


There's a way to implement Bresenham so you don't actually need to handle lines or special cases. Check the Wikipedia article on it under the optimization section.

Basically you step in the direction of dx or dy, whichever is greater. Then after a certain amount of error accumulates, you take an additional perpindicular step. It can all be done with integers which keeps it pretty fast.

#103850 - TheWopr - Sun Sep 24, 2006 11:42 pm

Thanks for the help, I've read over it and i understand enough of the Bresenham Algortihm to make it work even though i'm not sure how much i understand the optimized code, but it works so thanks a ton.

#104786 - sasq - Mon Oct 02, 2006 5:00 pm

Bresenham is pretty outdated and is not optimal on modern hardware.
The faster (and easier) way is to just used fixed point values X and Y, and add a DELTA to X or Y and 1 to the other while stepping along the line.

Allthough since its a TRON-game you're only doing horizontal or vertical lines I take it? So then it should be very simple, just something like

Quote:

while(oldx < x) putpixel(oldx++, oldy);
while(oldx > x) putpixel(oldx--, oldy);
while(oldy < y) putpixel(x, oldy++);
while(oldy > y) putpixel(x, oldy--);