【发布时间】:2012-06-13 18:18:59
【问题描述】:
我从一篇精彩的文章中改编了一些代码,该文章是 Mukund Sivaraman 绘制的关于圆绘制的文章,以便为给定圆上的每个点执行传递的函数:
template<class Function>
static void For_each_point_on_circle(Image *image, int radius, Function function)
{
int x, y;
int l;
l = (int) radius * cos (M_PI / 4);
for (x = 0; x <= l; x++)
{
y = (int) sqrt ((double) (radius * radius) - (x * x));
function(image, x, y);
function(image, x, -y);
function(image, -x, y);
function(image, -x, -y);
function(image, y, x);
function(image, y, -x);
function(image, -y, x);
function(image, -y, -x);
}
}
但是,我真正需要的是按顺序计算圆周围的点,因此对 function(image, x, y) 的调用将从 0 到 360 度按顺序进行,而不是跳过,这是可以接受的画圆。
我可以计算所有点并对它们进行排序,但我希望有人知道一种正确的方法,也许使用多个循环,每个循环计算一个段?
非常感谢。
【问题讨论】:
-
圆上的点相距多远? (1度?)
-
彼此相距仅 1 个像素,不一定有任何固定度数。谢谢。
-
如果你想在均匀的空间中绕圆(即使是关于弧长,而不是 x 轴),我建议使用极坐标和 2*PI 的合适除法,基于多少您要绘制的点。
-
显而易见的
function(image, radius*cos(angle), radius*sin(angle))有什么问题? -
@Danny:停留在像素坐标然后尝试进入极坐标更容易。角度像素间距在圆周围不是恒定的。如果你想计算它,它将是角度和分辨率的函数。