why doesnt this basic fill routine work?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
777
Manic Miner
Posts: 512
Joined: Fri Jun 26, 2020 11:23 am
Location: sw uk

why doesnt this basic fill routine work?

Post by 777 »

https://pasteboard.co/Js7PRjTd.png

https://gofile.io/d/Ri7Og0

it works ok for a bit then just spills out

im scratching my head here. i often get lost with the logic of a program
Last edited by 777 on Mon Sep 21, 2020 4:34 pm, edited 1 time in total.
i started programming the spectrum when i was 8 :-

1 plot rnd*255,rnd*175
2 goto 1
User avatar
Luzie
Manic Miner
Posts: 906
Joined: Fri May 01, 2020 2:07 pm

Re: why doesnt this basic fill routine work?

Post by Luzie »

Link to Screenshot is not working?

Having a short look at your BASIC-program you use x=x+1 together with y=y+1, so you may jump straight diagonally through the pixel you test with POINT.

Also you didn´t test against x or y getting to a value under zero.
dfzx
Manic Miner
Posts: 677
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: why doesnt this basic fill routine work?

Post by dfzx »

Luzie wrote: Mon Sep 21, 2020 4:29 pm Having a short look at your BASIC-program you use x=x+1 together with y=y+1, so you may jump straight diagonally through the pixel you test with POINT.
My BASIC knowledge has about 35 years of rust on it, but yes, that's what I thought was happening too.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
User avatar
777
Manic Miner
Posts: 512
Joined: Fri Jun 26, 2020 11:23 am
Location: sw uk

Re: why doesnt this basic fill routine work?

Post by 777 »

Luzie wrote: Mon Sep 21, 2020 4:29 pm Link to Screenshot is not working?

Having a short look at your BASIC-program you use x=x+1 together with y=y+1, so you may jump straight diagonally through the pixel you test with POINT.

Also you didn´t test against x or y getting to a value under zero.
fixed pic link
i started programming the spectrum when i was 8 :-

1 plot rnd*255,rnd*175
2 goto 1
User avatar
Morkin
Bugaboo
Posts: 3264
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: why doesnt this basic fill routine work?

Post by Morkin »

I think it's found a gap... ;) Possibly, anyway...

Here's the line (green) moving towards different parts of the circle (lower left)

Image

The first two pics are fine as the POINT (x,y) identifies the pixel of the boundary (highlighted in grey).

But the third one doesn't hit a black pixel at any point.
My Speccy site: thirdharmoniser.com
User avatar
777
Manic Miner
Posts: 512
Joined: Fri Jun 26, 2020 11:23 am
Location: sw uk

Re: why doesnt this basic fill routine work?

Post by 777 »

any way round this problem?

it even does this if i plot it vertically

https://pasteboard.co/Js86PoZ.png

https://gofile.io/d/Q5zAlX
i started programming the spectrum when i was 8 :-

1 plot rnd*255,rnd*175
2 goto 1
User avatar
Luzie
Manic Miner
Posts: 906
Joined: Fri May 01, 2020 2:07 pm

Re: why doesnt this basic fill routine work?

Post by Luzie »

Whats you "d" variable used for? Used for the direction up or down of filling?

Even if we bring this to work, you only fill the right half of the circle.

Maybe you should search a little bit on the net for a good filling routine...
maybe here: https://groups.google.com/g/comp.sys.si ... bcxo?pli=1
https://web.archive.org/web/20100830101 ... asfill.htm
dfzx
Manic Miner
Posts: 677
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: why doesnt this basic fill routine work?

Post by dfzx »

777 wrote: Mon Sep 21, 2020 4:54 pm it even does this if i plot it vertically
It's doing something different if you plot vertically. If you look at the top of the circle you'll see a flat bit, then to the side it steps down a pixel to start the curve. When you hit the edge of the flat bit you then step a pixel to the right, which takes you outside the line (actually it takes you onto the line) from where your logic escapes the boundary.

Instead of going back and forth, repeatedly changing direction, try resetting y back to the centre so it always fills outwards?
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
User avatar
777
Manic Miner
Posts: 512
Joined: Fri Jun 26, 2020 11:23 am
Location: sw uk

Re: why doesnt this basic fill routine work?

Post by 777 »

Luzie wrote: Mon Sep 21, 2020 6:02 pm Whats you "d" variable used for? Used for the direction up or down of filling?

Even if we bring this to work, you only fill the right half of the circle.

Maybe you should search a little bit on the net for a good filling routine...
maybe here: https://groups.google.com/g/comp.sys.si ... bcxo?pli=1
https://web.archive.org/web/20100830101 ... asfill.htm
d is direction

+ not if i start it from further left
i started programming the spectrum when i was 8 :-

1 plot rnd*255,rnd*175
2 goto 1
User avatar
ZXDunny
Manic Miner
Posts: 498
Joined: Tue Nov 14, 2017 3:45 pm

Re: why doesnt this basic fill routine work?

Post by ZXDunny »

While a floodfill doesn't have to make use of recursion, you generally need some sort of stack to store visited areas. This one:

Code: Select all

10 CIRCLE 128,88,40: CIRCLE 128,88,30
20 LET x=100: LET y=100
30 GO SUB 1000: STOP
1000 PLOT x,y
1010 IF NOT POINT (x+1,y) THEN LET x=x+1: GO SUB 1000: LET x=x-1
1020 IF NOT POINT (x-1,y) THEN LET x=x-1: GO SUB 1000: LET x=x+1
1030 IF NOT POINT (x,y+1) THEN LET y=y+1: GO SUB 1000: LET y=y-1
1040 IF NOT POINT (x,y-1) THEN LET y=y-1: GO SUB 1000: LET y=y+1
1050 RETURN
Edit: This seems to be the same one linked above, originally from Geoff Wearmouth :)

Will do the job, but slowly. You can do it with stacks instead of GOSUB/RETURN but it gets more complex pretty quickly.
BenHanson
Drutt
Posts: 9
Joined: Sat Oct 03, 2020 2:22 pm

Re: why doesnt this basic fill routine work?

Post by BenHanson »

Here is the ASM version:

Code: Select all

  ORG 32768
  LD C, 94 ; X
  LD B, 100 ; Y
Plot:
  CALL SetBit
Right:
  INC C
  CALL GetBit
  AND (HL)
  JR NZ, SkipRight
  PUSH BC
  CALL Plot
  POP BC
SkipRight:
  DEC C
Left:
  DEC C
  CALL GetBit
  AND (HL)
  JR NZ, SkipLeft
  PUSH BC
  CALL Plot
  POP BC
SkipLeft:
  INC C
Up:
  INC B
  CALL GetBit
  AND (HL)
  JR NZ, SkipUp
  PUSH BC
  CALL Plot
  POP BC
SkipUp:
  DEC B
Down:
  DEC B
  CALL GetBit
  AND (HL)
  JR NZ, SkipDown
  PUSH BC
  CALL Plot
  POP BC
SkipDown:
  INC B
  RET
GetBit:
  PUSH BC
  CALL 22AAh ; PIXEL-ADD
  POP BC
  PUSH BC
  LD B, A
  INC B
  XOR A
  SCF
Loop:
  RRA
  DJNZ Loop
  POP BC
  RET
SetBit:
  CALL GetBit
  OR (HL)
  LD (HL), A
  RET
Then:

Code: Select all

10 CIRCLE 128,88,40: CIRCLE 128,88,30
20 RANDOMIZE USR 32768
hikoki
Manic Miner
Posts: 576
Joined: Thu Nov 16, 2017 10:54 am

Re: why doesnt this basic fill routine work?

Post by hikoki »

Another way of filling is DRAWing INVERSEd lines between PLOTs

Code: Select all

10 BORDER 0: PAPER 0: INK 0: BRIGHT 0: CLS 
9001 LET x$="055197195055055192189054051043048041037018029043052039106066057088078055048066016"
9002 LET y$="159131124151142116108135088090136137095071065088085066044019024043048030033047065"
9100 GO TO 9400
9200 LET x2=VAL (x$(g TO g+2)): LET y2=VAL (y$(g TO g+2)): RETURN 
9300 LET x1=x2: LET y1=y2: RETURN 
9400 LET g=1: GO SUB 9200: FOR g=1 TO LEN x$ STEP 3: GO SUB 9300: GO SUB 9200: IF x2<0 THEN LET g=g+1: GO SUB 9200: GO SUB 9300
9500 PLOT INVERSE 1;x1,y1: DRAW INVERSE 1; PAPER 2;x2-x1,y2-y1: NEXT g
Post Reply