Circles and Computers

This is a discussion on Circles and Computers within the Tech Board forums, part of the Community Boards category; Hey all, While this is connected with C, I put it here, since it's technically a computer quiestion. So on ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    Circles and Computers

    Hey all,

    While this is connected with C, I put it here, since it's technically a computer quiestion. So on we go.

    When you tell the computer to draw a circle with a diameter x on the screen, it won't draw a perfect circle. Why? Well, let's skip the "Pi is irrational part" for a second. The screen is made of pixels. Pixels are squares. So, ultimately, the shape we see is lots of tiny squares put together so as to form something resembling a circle. But it's not a circle. It's squares. Now here's what I need; how does a computer know, based on the diameter of the circle that we want it to draw, how many squares to draw, and what shape to make? I'm writing a program where I need to be able to say "Okay, the diameter is two (in pixels), so that means that we're going to have 5 squares, and it will look like a plus sign.". So I took a squared paper, and tried to draw the shapes up myself, and to be honest, I'm pretty sure I got 'circles' with diameters three and four (in squares) right, but anything higher than that, and I don't have a clue how it's supposed to look.

    Could anybody here tell me, given a radius x, how many pixels, or "squares" the computer needs to draw to form the shape we perceive to be a circle? And how that shape looks? Or at least tell me how to draw up the shapes myself, I can calculate the formula from there.

    Cheers,

    Gabe

    EDIT: I've been doing some drawings, based on intuition, and here's what I got:

    Radii 2-8

    Radius 9

    Am I on the right track?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    There are algorithms like the midpoint-circle algorithm which you can use to draw circles on screen. A quick google search for Midpoint-circle Algorithm will give you the necessary links explaining the algorithm.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    generalyl based on the width of the pixel it determines the radial velocity of teh drw function so that it doesnt leave gaps. It ten itteratively calculates the coordinates based on an angle until it draws all 360 degrees. There are also optimized non-trig methods for drawing a cirlce.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Thanks a million

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21