some how my parameters are off

the g is not going high enough it stops right before the last row

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
radius = 0;
q = High;
t = Long + 1 ;
a = 0;
b = 0;
if (q <= height && t <= length)
{
  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)
	{
	  if (e <= height && g <= length)
	  {
	    if (grid[High][Long] == s && grid[e][g] == s)
	    {
	      cout << "ur(" << g << "," << e << "), ";
	    }
	  }
	}
      }	 
      h = h + 1;
    }while (h <= height);
    i = i + 1;
  }while (i <= length);
}
Actually I did try to understand your code but couldn't. Can you put some context to it? What is it supposed to do?
sorry hows 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
34
35
36
37
38
39
// 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);
}
What you wrote is way too complicated for me.

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);
}

Last edited on
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.
I don't mean to be complicated, I normally simplify after I know it works.
Topic archived. No new replies allowed.