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
Click For Summary
SUMMARY

The discussion focuses on creating a C++ program that draws a filled circle inside an empty square using asterisks ("*") and dots ("."). Participants emphasize the need to utilize for loops and conditional statements to achieve this. Key functions to implement include is_on_rectangle(int x, y) and is_in_circle(int x, y), which determine the positions of the square and circle, respectively. A critical geometric relationship is established: the radius of the circle must be less than half the side length of the square for it to fit properly.

PREREQUISITES
  • Basic knowledge of C++ programming, including loops and conditional statements.
  • Understanding of geometric relationships between circles and squares.
  • Familiarity with function creation in C++.
  • Ability to manipulate coordinate systems in a programming context.
NEXT STEPS
  • Implement the is_on_rectangle(int x, y) function to define the boundaries of the square.
  • Develop the is_in_circle(int x, y) function to determine if a point lies within the circle.
  • Research how to handle user input in C++ for dynamic radius and side length.
  • Explore graphical libraries in C++ for visual representation of geometric shapes.
USEFUL FOR

Students learning C++ programming, educators teaching geometry through coding, and developers interested in graphical representation of mathematical concepts.

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 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K