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.

  • and what you specifically do not like? Well, the colors would be nice to pass in the parameters, and the last line looks weird. and the square root is counted at each point, and you can only once per line. but the result of this code should give, apparently, the correct, despite the non-optimality. (disclaimer - I am not a php expert) - VladD
  • So I need optimization. That the result is correct - I know. The last line is the center. How to square the root count once per line? We check the distance to each point! - knes
  • @knes: well, for example, as in the answer below. - VladD

2 answers 2

 function getCellsByRadius($x,$y,$r,$ccircle,$ccenter){ $r2 = $r * $r; for($i=floor($y-$r);$i<=ceil($y+$r);$i++){ $dxmax2 = $r2 - ($y - $i) * ($y - $i); if ($dxmax2 < 0) continue; $dxmax = sqrt($dxmax2); $lower = floor($x-$dxmax); $upper = ceil($x+$dxmax); for($j=$lower;$j<=$upper;$j++){ $this->cells[$j][$i]=$ccircle; } } $this->cells[$x][$y]=$ccenter; } 

(I'm not sure that the code is correct, I am not a php expert. Maybe somewhere I need to declare local variables.)

  • By the way, yes, we can already easily calculate on the line what the maximum spread will be. Fine. =) - knes
  • @knes: thanks! - VladD
  • So I have to say thanks ... Thank you. =) - knes

purely sporting interest. I decided to just fill in the matrix 1 if in a circle

did like this

 $x=50; $y=25; $r=10; //грубо говоря тело вашей функции for ($i=-$r;$i<=$r;$i++) { $y1=ceil(sqrt(pow($r,2)-pow($i,2))); $cells[$x+$i]=array_fill($y-$y1,2*$y1+1,1); } 

brought in so here just decided to see whether it would look like a circle

 for ($i=-$r;$i<=$r;$i++) { for ($j=-$r;$j<=$r;$j++) { if (array_key_exists($j+$y,$cells[$x+$i])) { echo $cells[$x+$i][$y+$j]; } else { echo'0'; } } echo '<br>'; } 

result

 000000000010000000000 000001111111111100000 000011111111111110000 001111111111111111100 001111111111111111100 011111111111111111110 111111111111111111111 111111111111111111111 111111111111111111111 111111111111111111111 111111111111111111111 111111111111111111111 111111111111111111111 111111111111111111111 111111111111111111111 011111111111111111110 001111111111111111100 001111111111111111100 000011111111111110000 000001111111111100000 000000000010000000000 

Probably somewhere with rounding messed up, try. The only difference is that I assign 1, and you color the cell. Edintsveenoe that there is no code - this is the assignment of the color of the center point)))

That is not understanding. He himself made integers in the code, knowing that fractions could not be. Then how do you make the computer color the center point with coordinates that are not integers? Where are you in your neighborhood inscribed in a 4x4 square, put the center and paint it in a different color? According to your logic, it will be between pixels or what?

  • the result looks right; your code, however, does not handle the case of non-integer coordinates. - VladD
  • in the sense of? and your code also does not work with fractional coordinates. And ultimately, co-ordinates will always be whole as nikruti) - Ale_x
  • @Ale_x: works - for this there is ceil and floor . They are just in case the coordinates of the center and / or radius are not integers. The coordinates of the calculated points, of course, are integers. And because of this, I could not optimize: do not count the lower half, but take the corresponding line from the upper one. - VladD
  • so my ceil is used) you have built the array elements with handles through the loop, I use the built-in function. I simply calculate the range of Y limited to integers which, at a given X coordinate, will always belong to the complex, and ignite it without unnecessary checks and cycles) - Ale_x
  • Regarding the coordinates of the center are non-integer - if the scale is not used - and roughly speaking the axis pitch is equal to one point - you won’t draw a computer less, what will the fractional coordinates mean? - again, they will be translated into a whole when drawing (I remember Pascal with drawing graphs and working with images). - Ale_x 2:19