旋转函数后渲染时出现间隙
我正在渲染一个正弦函数,它在角度 0 下渲染得很好,但在旋转时有间隙。查看图片:
45°:
0°:
这些值是使用 PHP 生成的。每个方块都从 PHP 生成的矩阵中获取其状态。
for ($i = 4; $i < 125; $i++) {
$angle = deg2rad(0);
$x1 = $i;
$y1 = round(8*sin($i/8),0)+50;
$x = round($x1*cos($angle) - $y1*sin($angle),0);
$y = round($x1*sin($angle) + $y1*cos($angle),0);
if (($x > 1 and $x < 120) and ($y > 1 and $y < 120)){
$this->mapArray[$x][$y]->set_value(1); // square rendered.
}
}
我认为这与四舍五入有关,但我没有使用四舍五入,也得到了相同的结果。有什么建议吗?
I'm rendering a sine function which renders fine with angle 0, but has gaps when rotating it. See pictures:
45º:
0º:
The values are generated with PHP. Each square picks up its status from the matrix generated with PHP.
for ($i = 4; $i < 125; $i++) {
$angle = deg2rad(0);
$x1 = $i;
$y1 = round(8*sin($i/8),0)+50;
$x = round($x1*cos($angle) - $y1*sin($angle),0);
$y = round($x1*sin($angle) + $y1*cos($angle),0);
if (($x > 1 and $x < 120) and ($y > 1 and $y < 120)){
$this->mapArray[$x][$y]->set_value(1); // square rendered.
}
}
I thought it had something to do with the round, but I use no rounding and also have the same results. Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你正在渲染点并且你希望它是曲线。你用 121 个点来近似曲线,当然有洞,因为它的长度大于 121 像素。
你需要做的就是从上一个点到下一个点画线。它仍然是近似值,但不会有间隙。
you are rendering points and you want it to be curve. you are approximating curve with 121 points and there are ofcourse holes because it's length is bigger than 121 pixels.
what you need to do is to draw lines from last point to the next. it will still be approximation, but you won't have gaps.
这只是一个想法,还需要进一步细化。假设曲线是从左到右、从上到下绘制的。我们可以记住最后设置的像素(x0,y0)。绘制像素 (x, y) 时,如果 (x0, y0) 是像素 (x-1, y)、(x, y-1)、(x-) 之一,则 (x, y) 之前没有间隙1,y-1)。如果不是这种情况,则 (x0, y0) 和 (x, y) 之间存在间隙。这个间隙可以通过插值来填补。如果只有一个像素缺失,那么可以通过案例分析来设置。如果像素 (x0, y0) 和 (x, y) 之间的距离较大,则可以使用 Bresenham 算法。
This is only an idea, that would need a further elaboration. Suppose that the curve is drawn from left to right and from top to bottom. We could remember the last pixel (x0, y0) that was set. When drawing the pixel (x, y), there is no gap before (x, y) if (x0, y0) is one of the pixels (x-1, y), (x, y-1), (x-1, y-1). If it is not the case, then there is a gap between (x0, y0) and (x, y). This gap can be filled by interpolation. If only one pixel is missing, then it can be set by a case analysis. If the distance between the pixels (x0, y0) and (x, y) is larger, then an interpolating line could be drawn between them using the Bresenham's algorithm.