// upper right hand corner of a circle center point High Long
radius = 0;
q = High;
t = Long + 1 ;
a = 0;
b = 0;
if (q <= height && t <= length) // height and length are the grids parameters
{
i = 0;
do{
g = Long + 2 + i;
b = 1 + i;
h = 0;
do{
e = High + 2 + h;
a = 1 + h;
alpha = pow (a,2.0);
beta = pow (b,2.0);
if (alpha > 0 && beta > 0)
{
add = alpha + beta;
radius = sqrt(add);
if (radius < hop) // i don't want anything outside of the circle
{
if (e <= height && g <= length)
{
if (grid[High][Long] == s && grid[e][g] == s)
{
cout << "ur(" << g << "," << e << "), "; // tell me witch ones are in this quadrant
}
}
}
}
h = h + 1;
}while (h <= height);
i = i + 1;
}while (i <= length);
}
I think you can do it much simpler: Just check whether the coordinates you want, e and g, are within range of the circle, and within range of your grid. Why do you do it in such a complicated fashion?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if (q <= height && t <= length)
{
i = 0;
do{
do{
alpha = pow (i-Long,2.0);
beta = pow (h-High,2.0);
//don't check that ->if (alpha > 0 && beta > 0)
add = alpha + beta;
radius = sqrt(add);
if (radius <= hop && e <= height && g <= length)
if(grid[e][g] == s)
cout << "ur(" << g << "," << e << "), ";
//too many nested if's make it hard to read/edit
h = h + 1;
}while (h <= height);
i = i + 1;
}while (i <= length);
}
On the side, and possibly not applicable to the current problem: pow and sqrt are much more computationally expensive than simple multiplication. If you're applying the pythagorean theorum, there's a simple shortcut you can take.
Insead of a = pow(a,2.0); do a = a*a;
and instead of sqrting the radius, just do your calculations on the square of the radius.
This way works:
1 2 3 4 5
distance = sqrt( pow(x,2) + pow(y,2) );
if(distance <= radius)
{
// in circle
}
But this way is equivilent, and involves fewer computations:
1 2 3 4 5
distance_sq = (x*x) + (y*y);
if(distance_sq <= (radius*radius))
{
// in circle
}
Because after all, if distance <= radius, then distance^2 must also be <= radius^2 (assuming distance and radius are positive -- which would always be the case here). This lets you remove the expensive sqrt.