trying to output a circle drawn in "X's"

Dec 11, 2013 at 3:59pm
trying to create a program to display a circle drawn in "X's".
i'm using an array which stores the "X's" at the elements selected by x- and y-coordinates. the output doesn't look like a circle, though. please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <math.h>

using namespace std;

int xCoordinate(int rad, int raysAngle, int a)
{
    int xC;
    xC = 4+(rad*cos(raysAngle));
    cout << "angle: " << raysAngle << ". xC " << "#" <<a << ": " << xC << " ";

return xC;
}

int yCoordinate(int rad, int raysAngle, int a)
{
    int yC;
    yC = 4+(rad*sin(raysAngle));
    cout << "yC " << "#" << a << ": " << yC << " " << endl;

return yC;
}

int display(int rad, int grid)
//displays a square grid that is 'grid' units per side.  draws a  circle using X's that has a radius of rad.
//x and y coordinates computed using trigonometric functions in functions xCoordinate and yCoordinate.
{
    int powgrd = pow(grid,2);

    char dArray[powgrd];

    int rays = 4*(rad-1);
    float theta = 360/rays;
    int raysAngle;
    char * xCoords, *yCoords;
    xCoords = new char [rays];
    yCoords = new char [rays];

    for(int a=0;a<rays;a++)
        {
            raysAngle = a*theta;
            xCoords[a] = xCoordinate(rad, raysAngle, a);
            yCoords[a] = yCoordinate(rad, raysAngle, a);
        }


int n = 0;
for(int a = 0; a < powgrd; a++)
{
    dArray[a] = '.';
}

while(n < powgrd)
{
    cout << n << ": " << ((grid*yCoords[n])+xCoords[n]) << " ";
    n++;

}

cout << "\n\n\n";
int b = 0, c = 0;
    while (b < powgrd)
    {
        while (c < powgrd)
        {
            if (b == ((grid*yCoords[c])+xCoords[c]))
            {
                dArray [b] = 'X';
                c++;
            }
            else
            {
                c++;
            }
        }
        b++;
        c=0;
}


cout << "\n\n\n";

// displays the array in a grid shape
for (int a = 1;a <= powgrd; a++)
    {
        cout << dArray[a-1] << " ";
        if (a % grid == 0)
        {
        cout << endl;
        }
    }

    return 0;
}

int main()
{
    int rad = 8;
    int grid = 20;


    display(rad, grid);
    return 0;
}
[code]
[/code]
Last edited on Dec 11, 2013 at 7:17pm
Dec 11, 2013 at 5:26pm
Your code would be more readable in code tags. As far as the circle you wish to create, do you already have an idea of where the X's are supposed to be? One simple answer is to have the array filled with ones where X's are supposed to go, and zeros everywhere else. When outputting the array, when any value besides zero is found, an X can be outputted instead of a space. Of course, you'd have to initialize your array based on the value of rad, though.
Dec 11, 2013 at 7:21pm
sorry - here it is in code tags.

thanks, ill try it.
Topic archived. No new replies allowed.