1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Making a circle within a square in C++

  1. Feb 24, 2010 #1
    1. The problem statement, all variables and given/known data
    Write a program which draws a filled circle inside an empty square using "*" and ".", respectively. You should prompt for the radius r of the circle and the side length s of the square, and check that the circle will fit completely inside.

    2. Relevant equations

    3. The attempt at a solution

    I've honestly got nothing so far. I figure I need to use for loops and if statements, and I can easily make the top and bottom of the square.

    The issue is, I've got nothing to work it. No gist of where to begin. I'm also looking at the problem one line at a time, as I don't know of any other way to look at it.

    Any tips would be great, thanks.
    Last edited: Feb 24, 2010
  2. jcsd
  3. Feb 25, 2010 #2


    User Avatar
    Science Advisor
    Homework Helper

    So is your problem with the coding, or with the underlying geometry?

    For the C++ code, you could simply write something like

    Code (Text):

    const int MAX_COORDINATE = 50;
    int i, j;
    for(i = 0; i <= MAX_COORDINATE; ++i)
      for(j = 0; j <= MAX_COORDINATE; ++j)  
        if(is_on_rectangle(i, j))
          cout << "*";
        elseif(is_in_circle(i, j))
          cout << ".";
          cout << " ";
    where is_on_rectangle(int x, y) and is_in_circle(int x, y) are up to you to write.

    As for the geometry, let's start with the simplest question... suppose you have a circle with radius r and a square with sides L, both centered at the origin. If you draw the circle with maximal r, can you relate r to L through the use of triangles?
  4. Feb 25, 2010 #3
    I suppose my problem is with the geometry. If I knew that I'm sure I'd be able to figure out an approach. I figure that the radius has to be less than half of the side. So some way to check would be: if (radius > (side/2))...do something

    I'm also not sure what you mean by the functions is_on_rectangle and is_in_circle. I'm not sure how to write functions yet either, so I figured I could fill those if statements with specific conditions.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook