如何逐点绘制圆,并沿着圆周顺序移动?

问题描述 投票:0回答:1

请告诉我如何逐点构建一个圆,并确保像时钟的指针一样按顺序围绕圆移动。

现在我可以构建一个圆,但在我的循环中,点以随机顺序放置,最后我得到一个圆。

theta = 0, x = 16, y = 6
theta = 1, x = 11, y = 14
theta = 2, x = 2, y = 15
theta = 3, x = -4, y = 7
theta = 4, x = -1, y = -2
theta = 5, x = 9, y = -4

我需要依次构建一个圆,这样我就可以只画半个圆或画一个具有某些变化颜色的圆。我假设都是弧度,但我们需要以某种方式使用度数。但我没有更多的想法了。谢谢。

$radius = 10;
$centerX = 6;
$centerY = 6;

$theta = 0;

$white = imagecolorallocate($im, 0, 255, 0);

while ($theta <= 360) {
    $x = round($centerX + $radius * cos($theta));
    $y = round($centerY + $radius * sin($theta));
    $theta += 1;

    imagesetpixel($im, $x, $y, $white);
}

我尝试使用 deg2rad() 函数以某种方式使用度数,但我的数学知识还不够。

php algorithm math
1个回答
0
投票

你做得几乎正确,你只需要使用

imagecreate()
函数来创建图像,并在
while
循环之后使用
imagepng()
来渲染图像,并且不要忘记使用添加适当的
Content-Type
标头可在浏览器中正确呈现图像。

这是修改后的代码,我做了一些更改:

header('Content-type: image/png'); 

$im = imagecreate(500, 500);

$radius = 100;
$centerX = 250;
$centerY = 250;

$theta = 0;

$white = imagecolorallocate($im, 0, 255, 0); // use a background color for the image
$black = imagecolorallocate($im, 0, 0, 0); // use a black color for the line

while ($theta <= 360) {
    $x = round($centerX + $radius * cos($theta));
    $y = round($centerY + $radius * sin($theta));
    $theta += 1;

    imagesetpixel($im, $x, $y, $black); // draw the perimeter as dots
}

imagepng($im); // send the image to the browser

您使用的线条颜色与背景颜色相同,因此您看不到圆圈。在这里,我使用了不同的颜色。

这是结果的图像:

注意: 您可以将

$theta
增加较小的值(例如 0.5、0.2、0.1)以获得更平滑的外观。

© www.soinside.com 2019 - 2024. All rights reserved.