Hi, AdehhRR
If I were going to do this, I would separate the logic for defining the circle and the logic for drawing and treat them as two different problems.
if we have a 2 dimensional 7x7 array of Boolean values all set to false called shape_elements:
1 2 3 4 5 6 7
|
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
|
we essentially have a very similar concept to a pixel buffer.
if in our draw_to_screen() code we loop over all of the elements of this array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
for (int i=0; i<Y_ELEMENTS;i++)
{
for (int j=0; j<X_ELEMENTS;j++)
{
if(shape_elements[i][j])
{
std::cout<<"*";
}
else
{
std::cout<<" ";
}
}
std::cout<<std::endl;
}
|
we have a standardized method for "rendering" the buffer out to the screen.
Then the problem becomes populating the array in the first place.
for every "pixel" we want to calculate the distance to the centre of the circle, if this value is less than the circles maximum radius we know that the point is within the circle.
the distance is just some maths
1 2 3 4 5
|
int dist_squared(int p1, int p2)
{
int dif = p1 - p2;
return abs((dif*dif));
}
|
Then we could create the function for setting all of the elements of the array like this:
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
|
void generate_circle(int radius_squared)
{
//calculate the centre of the circle
int X_centre = X_ELEMENTS/2;
int Y_centre = Y_ELEMENTS/2;
for (int i=0; i<Y_ELEMENTS;i++)
{
for (int j=0; j<X_ELEMENTS;j++)
{
int x = j;
int y = i;
dist_squared_x = dist_squared(X_centre, x);
dist_squared_y = dist_squared(Y_centre, y);
//now we can add them together to get the total distance between them squared
int distance_squared = dist_squared_x + dist_squared_y;
//now we just check to see if they are within range.
if(distance_squared<radius_squared )
{
shape_elements[i][j] = true;
}
else
{
shape_elements[i][j] = false;
}
}
}
}
|
That would give us a solid black circle, but we want a ring, so we simply add a second radius, for the center and add that to the check:
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
|
void generate_circle(int radius_squared, int centre_radius_squared)
{
//calculate the centre of the circle
int X_centre = X_ELEMENTS/2;
int Y_centre = Y_ELEMENTS/2;
for (int i=0; i<Y_ELEMENTS;i++)
{
for (int j=0; j<X_ELEMENTS;j++)
{
int x = j;
int y = i;
dist_squared_x = dist_squared(X_centre, x);
dist_squared_y = dist_squared(Y_centre, y);
//now we can add them together to get the total distance between them squared
int distance_squared = dist_squared_x + dist_squared_y;
//now we just check to see if they are within range.
if(distance_squared>centre_radius_squared && distance_squared<radius_squared )
{
shape_elements[i][j] =true;
}
else
{
shape_elements[i][j] =false;
}
}
}
}
|
That would just be my way of doing it, this code is completely untested but I really hope it helps.
Thanks -NGangst