Adding plot arc, sector, segment routines to Matrix Brandy

for discussion of bbc basic for windows/sdl, brandy and more
Post Reply
mike12f
Posts: 77
Joined: Wed Nov 03, 2021 9:40 am
Contact:

Adding plot arc, sector, segment routines to Matrix Brandy

Post by mike12f »

I've been trying to add PLOT &A5 to plot arcs with Matrix Brandy. An initial implementation has been made.

Pull the latest commit from https://github.com/stardot/MatrixBrandy to try it out. The implementation of the plot arc code is all in this function: https://github.com/stardot/MatrixBrandy ... graphsdl.c . The implementation starts from lines "3417 case PLOT_ARC" onwards, and in the function "draw_arc", which is new.

Some simple BASIC code to test it is here:

Code: Select all

   10REM >TESTARC
   20MODE 0
   30MOVE 640,512
   40SX%=-34:SY%=-200
   50OX%=0:OY%=0:OB%=0
   60MOUSE ON
   70REPEAT
   80MOUSE X%,Y%,B%
   90IF X%<>OX% OR Y%<>OY% OR B%<>OB% THEN
  100CLS
  110MOVE 640,512
  120DRAW 640+SX%,512+SY%
  130IF B%<>0 SX%=X%-640:SY%=Y%-512
  140PLOT&A5,X%,Y%
  150DRAW 640,512
  160OX%=X%:OY%=Y%:OB%=B%
  170WAIT
  180ENDIF
  190WAIT
  200UNTIL FALSE
I found there were a lot more edge cases to deal with than I expected, and hence the code is longer and more ugly than I'd have hoped. Plus, annoyingly, there still are one or two cases of pixels being missed (these occur AFAIK when starting and ending the curve at a point very close to being vertically aligned with the circle centre). So kudos to the original Acorn graphics ROM authors for getting this all right with no apparent bugs.

If anyone can take this further then please do, particularly in working out how to implement sectors and segments, and tidying up the existing implementation if possible. Segment seems to be the next easiest of the three to do.

The PLOT arc, sector and segment commands are described on page 134 onwards of this BBC Basic Reference Manual.
Last edited by mike12f on Sun Oct 30, 2022 10:06 am, edited 2 times in total.
User avatar
jgharston
Posts: 5319
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by jgharston »

A fantastic demo program.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.45
(C) Copyright J.G.Harston 1989,2005-2024
>_
mike12f
Posts: 77
Joined: Wed Nov 03, 2021 9:40 am
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by mike12f »

Thanks for trying it! :)
User avatar
dhr2
Posts: 173
Joined: Tue Oct 25, 2016 8:37 pm
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by dhr2 »

-
Last edited by dhr2 on Sat Nov 05, 2022 5:47 pm, edited 2 times in total.
User avatar
dhr2
Posts: 173
Joined: Tue Oct 25, 2016 8:37 pm
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by dhr2 »

--
Last edited by dhr2 on Thu Nov 03, 2022 10:30 pm, edited 1 time in total.
Soruk
Posts: 1136
Joined: Mon Jul 09, 2018 11:31 am
Location: Basingstoke, Hampshire
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by Soruk »

An aside to the thread, responding to the bug report in the REM statement(!), I have now fixed that *Refresh glitch, please update your git tree and try again.
Matrix Brandy BASIC VI (work in progress) The Distillery (another work in progress) Note Quiz (New educational software for the BBC and modern kit)
BBC Master 128, PiTubeDirect (Pi 3B), Pi1MHz, 5.25+3.5in dual floppy.
User avatar
jgharston
Posts: 5319
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by jgharston »

Added tests for sectors and segments. Quite a nice test demo.

Code: Select all

   10 REM >TESTARC
   20 MODE 0
   30 MOVE 640,512
   40 SX%=-34:SY%=-200
   50 OX%=0:OY%=0:OB%=0
   60 MOUSE ON
   70 REPEAT
   80   MOUSE X%,Y%,B%
   90   IF X%<>OX% OR Y%<>OY% OR B%<>OB% THEN
  100     CLS
  110     MOVE 640-480,512
  120     DRAW 640-480+SX%,512+SY%
  130     IF B%<>0 SX%=X%-640:SY%=Y%-512
  140     PLOT &A5,X%,Y%
  150     DRAW 640-480,512
  160     OX%=X%:OY%=Y%:OB%=B%
  170     
  180     MOVE 640,512
  190     MOVE 640+SX%,512+SY%
  200     IF B%<>0 SX%=X%-640:SY%=Y%-512
  210     PLOT &AD,X%,Y%
  220     
  230     MOVE 640+480,512
  240     MOVE 640+480+SX%,512+SY%
  250     IF B%<>0 SX%=X%-640:SY%=Y%-512
  260     PLOT &B5,X%,Y%
  270     
  280     WAIT
  290   ENDIF
  300   WAIT
  310 UNTIL FALSE

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.45
(C) Copyright J.G.Harston 1989,2005-2024
>_
User avatar
dhr2
Posts: 173
Joined: Tue Oct 25, 2016 8:37 pm
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by dhr2 »

-
Last edited by dhr2 on Sat Nov 05, 2022 5:47 pm, edited 1 time in total.
mike12f
Posts: 77
Joined: Wed Nov 03, 2021 9:40 am
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by mike12f »

The way the arc routine works is as follows:
It uses the same maths as the brandy circle plotter (which itself piggy backs off the ellipse plotter, but sets width=height of the ellipse to get a circle). This plots the circle one row at a time, I.e. the main loop counter is the y coordinate.

But with the arc routine, we just filter (based on the y coordinates) which set of points do we want including on the left hand side, and which set of points do we want including on the right hand side? These two sets are deduced from the start and finish angles of the arc. Once found, each set of points is specified by 3 numbers, Y1l, Y2L and Y3L for the left hand set, and for the right hand set we have Y1R, Y2R and Y3R.

We then plot any point on the left side if its y coordinate satisfies Y1L<y<Y2L or Y>Y3L. And similar logic for the right hand side. There are some devils in the details though! But that is a rough overview.

I guess we could hopefully extend the arc plotter into a segment plotter while reusing a lot of this logic: for the current y coordinate, if we have both sides of the circle included in their sets, then we connect them with an hline. If only one of the sides is included, then we calculate the x coordinate of the straight edge of the segment (for the current y coordinate), and draw an h line from that x to the one valid side of the arc. If neither side of the arc is valid, then we do nothing.
User avatar
dhr2
Posts: 173
Joined: Tue Oct 25, 2016 8:37 pm
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by dhr2 »

after I saw this thread I went and wrote a circle sector plotting routine in bbc basic, I think it works. maybe it has some bug I didn't notice.
I don't think anyone cares but I might as well post it at this point. I spent like three days on it.
I thought about offering to write an implementation for brandy but I felt unsure it would be accepted
sect6_circle_sectors.txt
(3.65 KiB) Downloaded 41 times
Soruk
Posts: 1136
Joined: Mon Jul 09, 2018 11:31 am
Location: Basingstoke, Hampshire
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by Soruk »

dhr2 wrote: Sun Nov 06, 2022 1:26 am I thought about offering to write an implementation for brandy but I felt unsure it would be accepted
sect6_circle_sectors.txt
If it works I'll accept it!
Matrix Brandy BASIC VI (work in progress) The Distillery (another work in progress) Note Quiz (New educational software for the BBC and modern kit)
BBC Master 128, PiTubeDirect (Pi 3B), Pi1MHz, 5.25+3.5in dual floppy.
mike12f
Posts: 77
Joined: Wed Nov 03, 2021 9:40 am
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by mike12f »

dhr2 wrote: Sun Nov 06, 2022 1:26 am after I saw this thread I went and wrote a circle sector plotting routine in bbc basic, I think it works. maybe it has some bug I didn't notice.
I don't think anyone cares but I might as well post it at this point. I spent like three days on it.
I thought about offering to write an implementation for brandy but I felt unsure it would be accepted
sect6_circle_sectors.txt
Hi I've run the sector routine and it looks to be working, but let's test it more before you go to the effort of converting to C. By the way, "sector" is the harder of the three (compared to arc and segment) so it would be great to get this done. There might yet be a problem in the fact that that start and end angles are currently specified in integer degrees, according to the current demo program. Hence, can you try to change the demo interface for us so that we can specify the centre point, then the start point, then the end point, so that it is consistent with the PLOT codes requirement; see the BBC basic demo in included at the top of this thread for example code.
mike12f
Posts: 77
Joined: Wed Nov 03, 2021 9:40 am
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by mike12f »

dhr2 wrote: Sun Nov 06, 2022 1:26 am after I saw this thread I went and wrote a circle sector plotting routine in bbc basic, I think it works. maybe it has some bug I didn't notice.
I don't think anyone cares but I might as well post it at this point. I spent like three days on it.
I thought about offering to write an implementation for brandy but I felt unsure it would be accepted
sect6_circle_sectors.txt
Hi Did you get any further with this? I think it looks good. There's a couple of weird lines in there that could do with some comments to explain them.

Also, Brandy works with drawing a series of connected horizontal lines; I think your current code connects vertical lines currently; can that easily be flipped?

Finally, can your routine be adapted perhaps so it could draw arc/segments/sectors with an extra argument? That would be great for debugging code if we could unify all 3 somehow (and I'd be happy if the arc routine I implemented in Brandy was kicked out as a result), and we get 3-for-the-price-of-1.
mike12f
Posts: 77
Joined: Wed Nov 03, 2021 9:40 am
Contact:

Re: Adding plot arc, sector, segment routines to Matrix Brandy

Post by mike12f »

It looks like the easiest way to implement these routines would be to copy the methods used in the graphics extension rom which was analysed and published recently in this thread. The arc, sector and segment routines are described at https://tobylobster.github.io/GXR-pages/gxr/S-s16.html

I will do these conversions into matrix brandy in the near future, unless anyone else can do it sooner.
Post Reply

Return to “modern implementations of classic programming languages”