1 (edited by EricE 2017-02-20 07:34:17)

Topic: How to fill the area between two circles?

I am trying to color fill the area between two concentric circles.

My code is below, but it does not work.
The color fill goes beyond the outer circle.

!--------------------------------------------------------------------------------------
program main
use appgraphics
implicit none

    integer::myscreen
    myscreen = initwindow(800, 800,title='Graphics test 2',closeflag=.TRUE.)
    call setbkcolor(WHITE)
    call clearviewport()
    
    call setcolor(BLACK)
    call circle(300,300,100)     ! inner circle
    call circle(300,300,200)     ! outer circle
    
    call setfillstyle(SOLID_FILL, GREEN)
    call floodfill(300,450,BLACK)

    call loop()
    
    call closewindow(myscreen)
    
end program main
!--------------------------------------------------------------------------------------

2 (edited by EricE 2017-02-21 03:15:45)

Re: How to fill the area between two circles?

Now this does work!
Any ideas why?
All I did was add an additional circle with radius=201.

Did the original outer circle (R=200) have some gaps that the second outer circle (R=201) closed?
Increasing R=201 to 202 and the program still works in filling in only the annular region, but at R=203 the code fails again and the color floods past the outermost circle

!--------------------------------------------------------------------------------------
program main
use appgraphics
implicit none

    integer::myscreen
    myscreen = initwindow(800, 800,title='Graphics test 2',closeflag=.TRUE.)
    call setbkcolor(WHITE)
    call clearviewport()
    
    call setcolor(BLACK)
    call circle(300,300,100)
    call circle(300,300,200) 

    call circle(300,300,201)  
    
    call setfillstyle(SOLID_FILL, GREEN)
    call floodfill(300,450,BLACK)

    call loop()
    
    call closewindow(myscreen)
    
end program main
!--------------------------------------------------------------------------------------

Re: How to fill the area between two circles?

Flood-filling circles can be problematic because of the nature how AppGraphics (or really any graphics library) draws circles.  Often, like you've witnessed, the circumference line is not continuous, allowing the flood fill to "escape."

I would suggest perhaps using fillellipse instead to create an already-filled circle.  To create the opposite effect for the inner circle, you could change the fill color and call fillellipse again.  It would probably be more reliable than relying on a fill algorithm that is bound to be problematic whenever lines are not exactly horizontal or vertical.

Jeff Armstrong
Approximatrix, LLC

Re: How to fill the area between two circles?

Thank you jeff.