Input - X, Y, R. Accordingly, the coordinates of the center and radius. We must find everything inside the circle with such parameters.
There is a big suspicion of bydlokod:
function getCellsByRadius($x,$y,$r){ for($i=floor($y-$r);$i<=ceil($y+$r);$i++){//высота квадрата со стороной 2r. Искомый круг точно не больше. for($j=floor($x-$r);$j<=ceil($x+$r);$j++){//ширина квадрата со стороной 2r $distance = sqrt(pow(($j - $x),2)+pow(($i - $y),2));//расстояние до каждой точки квадрата if($distance<$r){ $this->cells[$j][$i]='#999'; } } } $this->cells[$x][$y]='#f00'; }
Please help me clear the code. = (
PS you can check the distance to the points that are inside the square + -r, but all the diamond
x+r,y xr,y x,y+r x,yr
But it seems to me that an extra check of this diamond can only aggravate the situation.
You can still check only for a quarter, and then multiply the whole circle. But then again, turning can be expensive.