Thread: Creating a bitmap Circle

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    Creating a bitmap Circle

    Hello everybody.
    I have an assigment and amongst other questions on of them specifies:
    Create a bmp image 256x256 with 225(color) backround and a circle of varying radius given by the user with 64(color). Circle's center will be image's center.

    Since the center will be the images center we know that circle's center would be 128,128, and getting radius r from the user i suppose i can use pythagoras (x-xo)^2 +(y-yo)^2=r^2~~~~~xo,yo=128.
    so scanning the whole 256x256 "map" of the image i want to color every image[i][j] that validates (x-xo)^2 +(y-yo)^2< r^2
    to create the disc of the circle.
    As much as i try i always get something like a diagonal line from top right to top left, and not a circle. my code is:

    Code:
    for(int i=0; i<256; i++)
     {
       di=(i-128)^2;
            for(j=0; j<256; j++)
            {
                  dj=(j-128)^2;
                         
                         if((di+dj)<(r^2))
                         {
                         image[i][j]=(unsigned char*)64;
                         }
            }
    }
    any hints?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well the first thing to note is that ^ isn't "raise to power of", but is in fact bitwise exclusive-or (cf & and | as other bitwise operators).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    kill me now >.< thanks mate

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Cursius View Post
    kill me now >.< thanks mate
    Also you should just iterate over the bounding box of the circle. Otherwise the code is too inefficient for big images and small circles.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Bitmap from byte[]
    By xds4lx in forum C# Programming
    Replies: 4
    Last Post: 01-15-2010, 04:03 PM
  2. Creating ICON form BITMAP files
    By Uwar in forum Windows Programming
    Replies: 0
    Last Post: 12-21-2009, 05:32 AM
  3. Drawing circle into a bitmap file
    By brokensail in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2006, 01:26 AM
  4. Copying Bitmap Resources without Creating HDC's
    By bartybasher in forum Windows Programming
    Replies: 1
    Last Post: 08-02-2004, 06:35 AM
  5. Help creating a Bitmap
    By Kristian25 in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2003, 05:17 AM