It is currently Mon Oct 20, 2014 5:44 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Feb 05, 2012 9:52 pm 
Offline
 Profile

Joined: Sun Jul 19, 2009 10:11 pm
Posts: 9
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


Top
 
PostPosted: Mon Feb 06, 2012 9:54 am 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
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.


Top
 
PostPosted: Tue Feb 07, 2012 4:36 pm 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
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.


Top
 
PostPosted: Tue Feb 07, 2012 6:30 pm 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
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.


Attachments:
File comment: Bresenham line drawing in Mode 4 on the Beeb
Bresenham.zip [2.32 KiB]
Downloaded 40 times
Top
 
PostPosted: Wed Feb 08, 2012 7:55 am 
Offline
 Profile

Joined: Sun Jul 19, 2009 10:11 pm
Posts: 9
Morning Rich,

Many thanks, I'll have a play with this later, after breakfast :-)

Andy


Top
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron