Thread: drawing a circle in C

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    drawing a circle in C

    I need to write a program that draws a grid with different symbols and spaces. The overall shape that is printed on the screen shoudl be a circle that looks like a tire and wheel. There should be different symbols for the hub, the wheel, and then the outer tire and the rest all spaces. The user reads in the circumference of the tire and the program converts it into a radius that is used for the grid to draw the tire. I know I need a for loop to draw the shape, but I am confused as to how to start the drawing from the center of the grid? Can anyone help.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Show us the code you have so far and I'm sure you'll get lots of advice...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you trying to draw "graphics" on a console screen, using #+= etc as symbols to represent different aspects (hub, tyre) of your wheel?



    How To Ask Questions The Smart Way
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you want to do this on a graphics window, or a rougher text based window?

    Best if you could show us a drawing or picture of what you want. You can attach it with the button down below the editing window "manage attachments", or you can load it to a site like ImageShack, and then post the link for us to see it.

    Again, with the ninja-posting!!

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You didn't say anything about spokes so it seems you want a "solid" wheel and everything within the outer circumference will be colored with some symbol. Since you're dealing with a circular shape, every point of the wheel can be defined in terms of its distance from the center. You can do the entire thing with a single pass through the entire grid: first define the center point, then for each point, calculate its distance from the center and based on that distance decide which symbol to write at that point.

    There may be more efficient methods (in terms of amount of computation), but I think this approach is probably the easiest to implement.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    R.Stiltskin, what you have discribed is exactly what I am trying to do. However, I do not know how to start the loop from the center of the grid. I know how to write the loop to print some symbol based on the column and row the line is in, but I dont know how to start at the center of the grid. I do need the wheel to be solid as well. I am at work right now and can not access my code till this afternoon when I get home. Can you tell me how to set the center of the grid?
    Thanks

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Why are you concerned with starting the loop at the center of the grid? It makes no difference where you start the loop -- start at the upper left corner, [0,0] and draw the first row, the second row, ... and so on until the image is complete.

    The center of the wheel can be, but isn't necessarily, at the center of the image. You can choose any arbitrary point, let's call it [x0,y0], to be the center of the wheel. Then for each point [x,y] on the grid you calculate its distance from the center:

    d = sqrt( (x - x0)^2 + (y - y0)^2 )

    and based on the value of d decide which symbol goes at [x,y]. (Create a lookup table that tells which symbol to use for each range of distances.)

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thanks, for all your help. I just got your message. I figured it out using the pathagoen theorem to judge the distance from the center. I used 2 for loops and three if statements to assign the different characters to the graphic. I also read in an inner and outer radius for the wheel and the tire portion. Worked out great! I just had a brain cramp in the beginning.
    Thanks Again!

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Do you think I could see your code? I'm trying something similar and I am at a total loss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing a circle with ASCII character
    By bnkslo in forum C Programming
    Replies: 15
    Last Post: 04-03-2008, 12:56 PM
  2. drawing circle to console
    By golom in forum C Programming
    Replies: 2
    Last Post: 12-20-2006, 10:57 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. Drawing a circle using glut...
    By bennyho03 in forum C Programming
    Replies: 6
    Last Post: 10-18-2004, 10:06 AM
  5. Drawing a circle in a video captured frame
    By skyhigh in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 01:00 AM