Thread: Filling up a circle

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    Filling up a circle

    Hi, I am working on a program, for which I wrote a function to draw a circle based on a given color, x and y coordinate and a radius, this function however doesn't have the possibility to be filled with this given color, no matter what I come up with, nothing really seems to do the trick, here is what I have so far:

    Code:
    void PlotCircle(Color color, int xcenter,int ycenter,int radius)
    {
        int x=0;
        int y=radius;
        int p=3 - 2 * radius;
    
        while(x <= y)
        {
            putPixelScreen(color,xcenter+x,ycenter+y);
            putPixelScreen(color,xcenter-x,ycenter+y);
            putPixelScreen(color,xcenter+x,ycenter-y);
            putPixelScreen(color,xcenter-x,ycenter-y);
            putPixelScreen(color,xcenter+y,ycenter+x);
            putPixelScreen(color,xcenter-y,ycenter+x);
            putPixelScreen(color,xcenter+y,ycenter-x);
            putPixelScreen(color,xcenter-y,ycenter-x);
    
            if(p < 0)
                p += 4 * x++ + 6;
            else
                p += 4 * (x++ - y--) + 10;
        }
    }
    the putPixelScreen function draws one pixel from a given color and x/y coordinates to the screen

    any ideas on how I can fill up the circle?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The scanline algorithm is popular
    http://www.cse.ohio-state.edu/~gurar...93s04su77.html

    The flood fill is easy, but very aggressive on stack usage if you're not very careful.
    http://www.student.kuleuven.ac.be/~m...floodfill.html

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Instead of drawing 8 pixels, draw 4 lines.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Instead of drawing 8 pixels, draw 4 lines.
    That wouldn't work, would it? Which pixel operations would you combine?
    Code:
            putPixelScreen(color,xcenter+x,ycenter+y);
            putPixelScreen(color,xcenter-x,ycenter+y);
            putPixelScreen(color,xcenter+x,ycenter-y);
            putPixelScreen(color,xcenter-x,ycenter-y);
            putPixelScreen(color,xcenter+y,ycenter+x);
            putPixelScreen(color,xcenter-y,ycenter+x);
            putPixelScreen(color,xcenter+y,ycenter-x);
            putPixelScreen(color,xcenter-y,ycenter-x);
    They look pretty different to me.

    And besides, plotting lines is slower than pixels, and the line function would probably call the pixel function anyway. I think it would be best to use 8 pixels.

    [edit]
    Look at the line-plotting code here: http://www.brackeen.com/home/vga/sou.../circle.c.html
    [/edit]
    Last edited by dwks; 01-21-2006 at 02:58 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by dwks
    That wouldn't work, would it?
    It would.

    Quote Originally Posted by ibid.
    Which pixel operations would you combine?
    Either the ones with identical effective ordinates, or the ones with identical effective abscissas. Except that horizontal lines are more optimizable than vertical ones. Lines restricted to one dimension are optimizable. Especially horizontal ones which just degenerate into memcpy()'s, and can be further optimized to use SIMD or block-move instructions.

    Quote Originally Posted by ibid.
    And besides, plotting lines is slower than pixels, and the line function would probably call the pixel function anyway.
    Still faster then either of Salem's solutions.

    Quote Originally Posted by ibid.
    I think it would be best to use 8 pixels.
    Except that the OP wants to fill the circle.
    Last edited by zx-1; 01-21-2006 at 04:54 PM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circle problem
    By gunghomiller in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:40 PM
  2. Drawing circle into a bitmap file
    By brokensail in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2006, 01:26 AM
  3. Half of the circle
    By hdragon in forum Game Programming
    Replies: 14
    Last Post: 03-10-2006, 10:15 PM
  4. circle filling
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 05:37 AM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM