Comp Sci Making a circle within a square in C++

AI Thread Summary
The discussion revolves around creating a C++ program to draw a filled circle inside an empty square using specific characters. The user is struggling with both the coding aspect and the underlying geometry, particularly how to ensure the circle fits within the square. Suggestions include using for loops and conditionals to determine the positions of the circle and square, with a focus on defining functions to check if points are inside the circle or on the square. The user acknowledges the need for the circle's radius to be less than half the square's side length to fit properly. Overall, the conversation emphasizes the importance of understanding both the geometric relationships and the coding structure needed for the solution.
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.
 

Similar threads

Replies
12
Views
2K
Replies
5
Views
2K
Replies
5
Views
2K
Replies
2
Views
3K
Replies
4
Views
2K
Replies
2
Views
4K
Replies
3
Views
1K
Back
Top