Lets say we have a Circle with Center(float_x,float_y) and Radius float_r. This is located on a grid or array like plane and we want to find every Cell(int i,int j) that gets intersected by the circle.
These Cells should be ordered in an array according to their angle to the x axis (x axis is horizontal positive to the right, y vertical positive in up direction).
The problem is that the center of the circle doesn't have to be exactly in the middle of a cell, I guess this denies the use of "point" symmetry of a cell.
I thought about pretending to have a cell symmetric circle and iterating through 90° and adding the offset to each point i am calculating but i am unsure if that would yield good results.
I would appreciate any Help or Ideas
Thanks
EDIT:
The following Code is for finding every point in the first quadrant(x+,y+) i haven't tried it yet but i am pretty sure it will work. Can be applied to the other quadrants too but then x/y iteration order and direction has to be changed. Since we start with xmax/pt_y the points will be ordered by their angle.
As soon as i have tested this i will mark this question as solved.
float pt_x,pt_y are the circle middle coordinates
float searchradius is the radius of the circle
float map_info.scale is the size of one cell in the grid
int maxx=round((pt_x+searchradius)/map_info.scale) getting max possible cell
j=round(pt_y/map_info.scale);
for (i=maxx; i >round(pt_x/map_info.scale); i--) {
while(true)
{
Point.x=i;
Point.y=j;
Points.push_back(Point);
if(sqrt(pow(i*map_info.scale,2)+pow((j+1)*map_info.scale,2))<=searchradius)
{
j++;
}
else break;
}
}
So lets test the intersection with grid lines computed on
floats
(as I suggested in comments). I would start with dividing the circle into 4 sections ("quadrants") like this:In each section only one axis is major (has always bigger or equal difference to the other one) so we can iterate it directly by incrementing (without creating holes in curve). The minor axis is then computed using circles equation:
so simply loop the major axis in order by 4x (not nested)
for
loops and add the points. However whenever the minor axis computed is not directly on the grid we need to add 2 points where the second point has decremented major axis. This can cause duplicate points so to avoid duplicates we should also check if added point is not already added to the end of the list. At the end of last section we should also check against the start of the list (or remove last few duplicate points) I think there would be up to 4 points like this max.When put all this together in C++/VCL it look like this:
This is the output (including my debug rendering):
The result is in
pnt[pnts]
array holding thex,y
grid coordinates in order ...This is far from optimized for example:
sqrt
is computed twice you could memoize them ...sqr
of just incremented value can be computed without multiplication