draw shapes in C

Dear all,
This code below draw a triangle in C
Could someone help me about drawing square,brill,rectangular


#include <stdio.h>
int main()
{
int t;
int x = 0;
int y;
t = 0;
while ( t < 7 )
{
y = 0;
while ( y < t)
{
printf("* ");
y++;
}
if(x == t)
{
printf("\n");
x = t + 1;
}
t++;
}

return 0;
}
A square should be easier to draw than a triangle...
1
2
3
4
5
6
7
8
9
typedef struct point {
    int x;
    int y;
}   point_t;

typedef struct rectangle {
    point_t point1;
    point_t point2;
}   rect_t;

That's how I would start; so to define a rectangle:
1
2
3
4
5
6
    rect_t myRectangle;
    myRectangle.point1.x = 0;
    myRectangle.point1.y = 0;
    myRectangle.point2.x = 5;
    myRectangle.point2.y = 5;
    /* Defined a 5x5 rectangle */

That's a start; you can probably continue form there...

Circles are harder... I'm not sure about them.
circles actually sound like a challenge
Is not that difficult. An easy algorithm to draw circles is this: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
Last edited on
I know but I meant just doing it yourself.
Hello, this is my first post, so i'll try to explain the best I can.

Drawing a circle (without using a graphic library, and by using "*") can be thought as drawing a circle on a math paper. You take your center point, and a radius. Then go through all the points forming the square containing the desired circle, and set a condition to be followed so that the point can be on the circle. Ideally, the condition should be the algebraic definition of the circle: square(x)+square(y)=square(r). But, then again, we're in a finite space, with integer coordinates, thus making following that rule a bit harder. So, instead of equality, we'll use inequalities. I've also fine tuned the points resulted, giving it a more "round" look. Here's the code I've written, and the output for a C(10, 10, 8):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])
{   char matrix[20][20];
    int i, j, cx, cy, r;
    cx=10; cy=10; r=8;
    for (i=0; i<=20; i++)
        for (j=0; j<=20; j++) matrix[i][j]='O';
    for (i=cx-r; i<=cx+r; i++)
        for (j=cy-r; j<=cy+r; j++)
            if (((cx-i)*(cx-i)+(cy-j)*(cy-j)<=r*r+sqrt(r)) && ((cx-i)*(cx-i)+(cy-j)*(cy-j)>=r*r-r)) 
               matrix[i][j]='*';
    for (i=0; i<=20; i++)
    {
        for (j=0; j<=20; j++) cout<<matrix[i][j];
        cout<<'\n';
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


OOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOO
OOOOOOOOO***OOOOOOOOO
OOOOOO**OOOOO**OOOOOO
OOOOO*OOOOOOOOO*OOOOO
OOOO*OOOOOOOOOOO*OOOO
OOO*OOOOOOOOOOOOO*OOO
OOO*OOOOOOOOOOOOO*OOO
OOOOOOOOOOOOOOOOOOOOO
OO*OOOOOOOOOOOOOOO*OO
OO*OOOOOOOOOOOOOOO*OO
OO*OOOOOOOOOOOOOOO*OO
OOOOOOOOOOOOOOOOOOOOO
OOO*OOOOOOOOOOOOO*OOO
OOO*OOOOOOOOOOOOO*OOO
OOOO*OOOOOOOOOOO*OOOO
OOOOO*OOOOOOOOO*OOOOO
OOOOOO**OOOOO**OOOOOO
OOOOOOOOO***OOOOOOOOO
OOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOO


PS: You need to set a symmetrical font so it becomes a real circle :)
Topic archived. No new replies allowed.