Drawing Figures in C++: Put Circle Pattern in Square

In summary: Today, and for quite some time now, "drawing" is done at the pixel level, not with characters. Most compilers include some graphics functions and I think all IDE's include a basic graphics library. Folks who want more complicated drawings usually use an extended graphics library.Don't be alarmed: I'm showing nothing new and just dabbling on what's given above. The OP has already posted their code. I'm proposing you think of your nested for loop (a for loop inside of a for loop) as a big coordinate system just like in your mathematics class. Then you can use your if function as a test: Does this cell (x, y) intersect with my equations for a square or circle?
  • #1
martusa
3
0
Homework Statement
Draw a circle inside a square
Relevant Equations
figures c+*
i only managed to draw a square and don’t know how to put circle pattern inside of it

C:
#include <stdio.h>

int main()

{
    int totalrowcol;
    printf("Enter the number of rows: ");
    scanf_s("%d", &totalrowcol);
    for (int rowno = 1; rowno <= totalrowcol; rowno++)
    {
        for (int colno = 1; colno <= totalrowcol; colno++)
        {
            if ((colno==1)|| (colno==totalrowcol)||
                (rowno==1)||(rowno==totalrowcol)||
                ())
            {
                printf("* ");
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
By the way, welcome to PF!
Your education will require you to generate ideas and test them. What are your thoughts? What have you tried? What were the results? One thing to stimulate idea generation-- forget about drawing a square (for the moment) and figure out how to draw just a circle.
 
Last edited:
  • #3
lewando said:
By the way, welcome to PF!
Your education will require you to generate ideas and test them. What are your thoughts? What have you tried? What were the results? One thing to stimulate idea generation-- forget about drawing a square (for the moment) and figure out how to draw just a circle.
my programming level isn’t that high,i wasn’t able to find more information how to do this on the internet
 
  • #4
martusa said:
i wasn’t able to find more information how to do this on the internet
That's astounding since a simple search for "how to draw circle in c++" gave a TON of information.
 
  • #5
phinds said:
That's astounding since a simple search for "how to draw circle in c++" gave a TON of information.
i have already searched for it,but i don’t know how to make square and circle work together . I have been studying code for less than a month, and it’s hard to find out how things work
 
  • #6
@phinds is more than right as usual. The fact that you are using ASCII symbol * implies you are doing an "ASCII-art" style image. Updating the search string to "how to draw an ASCII circle in C++", gives you something (among others) like this result.

I am not an educator so I do not presume to know the best way to learn anything these days. You can always learn though studying examples, like the one above. But what about when you are faced with a novel problem (one without examples)? IMHO, you need to forget about programming constructs and think about how you would solve this manually. One you have a manual process, computerizing it (in any language) will speed it up a bit and allow you to meet your homework assignment obligation.
 
  • #7
One more thing-- looks like you are printing out the square symbols using loops and stuff, and not really defining a "canvass" of sorts. The example in the previous post makes use of an array to serve as the canvass. When all done with updating the array/canvass with all of your computer generated imagery, you can simply print out the array/canvass.
 
  • #8
@martusa what you are doing is 60 year old programming. My college roommate and I did those in 1964. That doesn't make it a bad thing to do, since you are learning, but you'd be better off getting with the times.

Today, and for quite some time now, "drawing" is done at the pixel level, not with characters. Most compilers include some graphics functions and I think all IDE's include a basic graphics library. Folks who want more complicated drawings usually use an extended graphics library.
 
  • #9
Don't be alarmed: I'm showing nothing new and just dabbling on what's given above. The OP has already posted their code. I'm proposing you think of your nested for loop (a for loop inside of a for loop) as a big coordinate system just like in your mathematics class. Then you can use your if function as a test: Does this cell (x, y) intersect with my equations for a square or circle? Would this help? Maybe you can turn this into a more achievable math problem than a programming problem ;)

Code:
#include<stdio.h>

int main()
{

    int Height = 20, Length = 10;

    for(int y=0; y<Height; y++)
    {
 
        for(int x=0; x<Length; x++)
        {
     
            printf("(%d, %d)\t", x, y);
         
            // Should a print a symbol here?
            /* if(squareTest)  // Use math equations to determine if cell intersect with square
             * {
             *         printf("-");
             * }
             *
             * if(circleTest)  // Use math equations to determine if cell intersects with a circle
             * {
             *        printf("*");
             * }*/
         
        }
     
        // New row
        printf("\n");
     
    }
 
    return 0;
 
}

print_array.png


You can also start your loops from a negative number and loop through -0.5*Length to 0.5*Length if you don't want to deal with any offsets from equations you might be more comfortable with.

Since it's discrete you might have to do some type of tolerance like IF() it's within some tolerance that it prints so that you don't have to worry about decimal problems. Tolerance can be described with the greater than or less than equations like is it within if(minimum < test value < maximum)
 
  • #10
phinds said:
Most compilers include some graphics functions and I think all IDE's include a basic graphics library.
The OP is coding in C, as evidenced by the use of printf and the inclusion of stdio.h. The C standard library doesn't include any graphics capabilities, nor does the C++ standard library. The usual IDEs, MSFT Visual Studio and GNU, don't include graphics capabilities, as far as I know. Of course, there are graphics libraries for C and C++, but these are components that would have to be downloaded and linked to.
 
  • Informative
Likes phinds
  • #11
Mark44 said:
The OP is coding in C, as evidenced by the use of printf and the inclusion of stdio.h. The C standard library doesn't include any graphics capabilities, nor does the C++ standard library. The usual IDEs, MSFT Visual Studio and GNU, don't include graphics capabilities, as far as I know. Of course, there are graphics libraries for C and C++, but these are components that would have to be downloaded and linked to.
Huh. I do graphics in Visual Studio using VB.NET and have not downloaded any libraries. I just assumed that the same functions were available to the C/C++ compiler.

Example (a program I wrote years ago for my kids). This produces an infinite variety of patterns by selecting various options and then moving the mouse around.
1632945157896.png
 
  • Like
Likes berkeman
  • #12
phinds said:
Huh. I do graphics in Visual Studio using VB.NET and have not downloaded any libraries.
Did you use DirectX or Direct2D/Direct3D APIs in your code? If so, these are not part of standard C or C++, and they probably aren't part of VB itself. It's been a long while since I've written any VB code, but how it would be done in C or C++ is to #include the appropriate header files, which will cause the linker to bring in the associated graphics libraries.
 
  • #13
Mark44 said:
Did you use DirectX or Direct2D/Direct3D APIs in your code?
Not on purpose :smile: What I mean is, I have no idea. I wrote that program 20 years ago and haven't done graphics since and am too lazy to dig out the source code.
 

FAQ: Drawing Figures in C++: Put Circle Pattern in Square

What is the purpose of drawing figures in C++?

The purpose of drawing figures in C++ is to create visual representations of data or images using code. This can be useful in creating graphs, diagrams, or animations in a program.

How do I put a circle pattern in a square using C++?

To put a circle pattern in a square using C++, you can use a combination of loops and mathematical equations to plot and draw the circles at specific coordinates within the square. You can also use built-in graphics libraries or functions to simplify the process.

Can I customize the size and color of the circles in the pattern?

Yes, you can customize the size and color of the circles in the pattern by adjusting the parameters in your code. This can include changing the radius of the circles, the number of circles, and the color of each circle.

Is it possible to animate the circle pattern in C++?

Yes, it is possible to animate the circle pattern in C++ by continuously redrawing the circles at different coordinates or by using animation functions. This can create a moving or rotating effect for the pattern.

Are there any resources or tutorials available for drawing figures in C++?

Yes, there are many resources and tutorials available online for drawing figures in C++. These can include step-by-step guides, video tutorials, and sample code for different types of figures and patterns. It is also helpful to refer to the documentation for any graphics libraries or functions you may be using.

Similar threads

Replies
3
Views
908
Replies
3
Views
1K
Replies
12
Views
1K
Replies
1
Views
9K
Replies
4
Views
1K
Replies
3
Views
975
Replies
2
Views
2K
Back
Top