我正在嘗試在 PHP 生成的圖片中的隨機位置繪制圓圈,從圖像的中心點開始并包含在“N”像素半徑中。我知道如何在方形邊界中進行操作(我也很清楚如何以菱形進行操作),但是圓形的方法對我來說有點難以理解。這是我到目前為止的代碼。圖像為 600*600px,我在 300*300px 的正方形區域中水平和垂直居中生成 100 個 25*25px 的白色圓圈。$w = $h = 600;$img = imagecreatetruecolor($w,$h);$cl = imagecolorallocatealpha($img,255,255,255,0);for ($i=0;$i<=100;$i++){ // GENERATE 100 CIRCLES $x = rand(150,450); $y = rand(150,450); imagefilledellipse($img, $x, $y, 25, 25, $cl);}header('Content-Type: image/jpeg');imagejpeg($img);imagedestroy($img);在圓形邊界中生成這些圓圈的最佳方法是什么?因此,例如,如果 $x=155 它不可能在 $y=155,或者如果 $x=445 它不可能是 $y=445感謝幫助 !編輯這是帶有極坐標的新代碼$w = $h = 600;$img = imagecreatetruecolor($w,$h);$cl = imagecolorallocatealpha($img,255,255,255,0);for ($i=0;$i<=100;$i++){ // GENERATE 100 CIRCLES $r = rand(25,(300-25)); $a = rand(0,360); $x = $r * cos(deg2rad($a)); $y = $r * sin(deg2rad($a)); imagefilledellipse($img, $x, $y, 25, 25, $cl);}header('Content-Type: image/jpeg');imagejpeg($img);imagedestroy($img);
1 回答

桃花長相依
TA貢獻1860條經驗 獲得超8個贊
您可以隨機化極坐標,而不是試圖找到 (x,y) 坐標:
0° 到 360° 之間的角度
半徑在 25 和 300-25 之間:有了這些限制,您可以確定您的小圓圈將始終位于圓圈邊界內。
然后通過標準公式轉換這些極坐標
x = r * cos(t) y = r * sin(t)
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消