Given two (very simplified) classes:
class Rectangle {
public:
int iL,jL; // lower left point of rectangle
int width, length; // width: in i-direction, length: in j-direction
};
class Circle {
public:
int iC,jC; // center-point of circle
int radius;
};
If I want to iterate over all elements in a Rectangle
, I can simply do that by:
for (int i = iL; i < iL-width; i--)
for (int j = jL; j < jL+length; j++)
doSomething();
My problem is to implement a smart way of iterating over all elements in Circle
. My current solution looks as follows:
for (int i = iC-radius; i <= iC+radius; i++)
for (int j = jC-radius; j <= jC+radius; j++)
if ( sqrt(pow(i-iC,2)+pow(j-jC,2)) <= r ) // checking if (i,j) lies within the circle (or its boundary)
doSomething();
However, for radius
getting large my current solution is very time-expensive (since I touch many elements which aren't in the Circle
and since I always need to evaluate pow
). Can you think of a more intelligent and efficient way of iteration over all Circle
-elements?