| www.retrosoftware.co.uk http://www.retrosoftware.co.uk/forum/ |
|
| Machine Code Line Drawing http://www.retrosoftware.co.uk/forum/viewtopic.php?f=73&t=731 |
Page 1 of 1 |
| Author: | TopBanana [ Sun Feb 05, 2012 9:52 pm ] |
| Post subject: | Machine Code Line Drawing |
Does anyone have any pointers for 6502 line drawing on a Beeb ? I've been looking at Bresenhams algorithm to start with (http://en.wikipedia.org/wiki/Bresenham's_line_algorithm), and others ? Ta |
|
| Author: | RichTW [ Mon Feb 06, 2012 9:54 am ] |
| Post subject: | Re: Machine Code Line Drawing |
Yep, Bresenham is the way to go. If you use MODE 4 and a 32 character wide screen (by CRTC trickery), you get pixel dimensions of 256x256 which means everything can be done with 8-bit arithmetic. |
|
| Author: | RichTW [ Tue Feb 07, 2012 4:36 pm ] |
| Post subject: | Re: Machine Code Line Drawing |
Just got a second to expand on this. In rough, the algorithm is as follows: Code: DrawLine(startx, starty, endx, endy): dx = ABS(endx-startx) dy = ABS(endy-starty) dirx = SGN(endx-startx) diry = SGN(endy-starty) IF (dx>dy) THEN ; Plot a 'shallow' line, i.e. one which is more horizontal than vertical accum = dx DIV 2 FOR count=0 TO dx PlotPixel(startx, starty) startx += dirx accum -= dy IF (accum<0) accum += dx starty += diry ENDIF NEXT ELSE ; Plot a 'steep' line, i.e. one which is more vertical than horizontal accum = dy DIV 2 FOR count=0 TO dy PlotPixel(startx, starty) starty += diry accum -= dx IF (accum<0) accum += dy startx += dirx ENDIF NEXT ENDIF In practice there are some optimisations to make, e.g. you can just calculate the screen address of the first pixel, and modify it as you increase startx/starty, instead of redoing the calculation each time in PlotPixel. You may want to swap the start and end points at the start so you always deal with a line going from top to bottom (for example), which removes the need to store diry. If you're EORing pixels, you might find that it's better not to plot the endpoint in a line, so that subsequent lines which start at the endpoint of the last one don't replot the pixel, causing it to disappear. You can do this simply by starting the FOR counter from 1 instead of 0 in both cases ('shallow' and 'steep' lines). I had a good optimised 6502 Bresenham line drawing routine somewhere, but have no idea where to look at the moment. If I can dig it out, I'll post it here. |
|
| Author: | RichTW [ Tue Feb 07, 2012 6:30 pm ] | ||
| Post subject: | Re: Machine Code Line Drawing | ||
Found it! Just patched it up to assemble in BeebAsm, with a quick BASIC demo. It works in MODE 4, with a 32 character wide screen, so acceptable coordinates are (0-255, 0-255). There's no clipping or anything like that, as that'd make it many times more complicated. Pixels are EORed to the screen, and the endpoint of the line is omitted. As it goes, it's fairly optimised - in particular in the case of a 'shallow' line, where it tries not to do too many reads/writes to screen, but instead caches entire bytes of the screen and writes all the pixels at once when it's finished with that byte.
|
|||
| Author: | TopBanana [ Wed Feb 08, 2012 7:55 am ] |
| Post subject: | Re: Machine Code Line Drawing |
Morning Rich, Many thanks, I'll have a play with this later, after breakfast Andy |
|
| Page 1 of 1 | All times are UTC [ DST ] |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|