Making a circle within a square in C++

In summary: If you want to implement those functions, you could simply use the following:bool is_on_rectangle(int x, int y);bool is_in_circle(int x, int y);If you want to implement those functions, you could simply use the following:bool is_on_rectangle(int x, int y);bool is_in_circle(int x, int y);
  • #1
arkturus
27
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
  • #2
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?
 
  • #3
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.
 

Related to Making a circle within a square in C++

1. How do I create a circle within a square in C++?

To create a circle within a square in C++, you will need to use mathematical equations to determine the coordinates of the circle's center and the radius. Then, use a loop to iterate through the square's pixels and check if they fall within the circle's boundary. If they do, you can color them to create the circle.

2. What is the mathematical equation to determine the coordinates of the circle's center?

The mathematical equation to determine the coordinates of the circle's center is (x,y), where x is the x-coordinate of the square's center and y is the y-coordinate of the square's center. These values can be calculated by dividing the length and width of the square by 2.

3. How can I calculate the radius of the circle within the square?

The radius of the circle can be calculated by taking the length or width of the square and dividing it by 2. This will give you the distance from the center of the square to the edge, which is also the radius of the circle.

4. Can I create a circle within a square without using loops?

While it is possible to create a circle within a square using mathematical equations without using loops, it is much more efficient to use a loop to iterate through the pixels and color them. This method is also more customizable as you can adjust the number of iterations to create a smoother or more detailed circle.

5. Is it possible to create a circle within a square with a different shape or size?

Yes, it is possible to create a circle within a square with a different shape or size. As long as you have the coordinates of the square's center and the radius of the circle, you can use the same mathematical equations and loop method to create a circle within any shape or size of a square.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
Replies
9
Views
898
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top