Making a circle within a square in C++

  • Context: Comp Sci 
  • Thread starter Thread starter arkturus
  • Start date Start date
  • Tags Tags
    C++ Circle Square
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 7K views
arkturus
Messages
27
Reaction score
0

Homework Statement


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.

Homework Equations


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:
Physics news on Phys.org
So is your problem with the coding, or with the underlying geometry?

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

Code:
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 << ".";
    else
      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?
 
CompuChip said:
So is your problem with the coding, or with the underlying geometry?

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

Code:
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 << ".";
    else
      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?

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.