只是缩放一个矩形
我正在尝试从处理开始。
现在我只是想缩放一个矩形。我发现了这个例子:
float a = 0.0;
float s = 0.0;
void setup()
{
size(200,200);
rectMode(CENTER);
frameRate(30);
}
void draw()
{
s = s + 0.04;
translate(width/2, height/2);
scale(s);
rect(0, 0, 50, 50);
}
它可以平滑地缩放矩形,但我有点惊讶,因为为了调整矩形的大小,我预计 rect() 的大小参数(第三个和第四个)被修改,就像这样:
float r = 1;
void setup()
{
size(200,200);
rectMode(CENTER);
frameRate(30);
}
void draw(){
rect(width/2, height/2, r, r);
r += 1;
}
但这种方式不会平滑地缩放矩形。那么,即使我发现第二种方法更自然,我是否应该使用第一种方法而不是第二种方法?任何评论都将受到欢迎。
问候
哈维尔
I'm trying to start with processing.
Now I'm just trying to scale a rectangle. I have found this example:
float a = 0.0;
float s = 0.0;
void setup()
{
size(200,200);
rectMode(CENTER);
frameRate(30);
}
void draw()
{
s = s + 0.04;
translate(width/2, height/2);
scale(s);
rect(0, 0, 50, 50);
}
It scales smoothly the rectangle but I'm a bit surprise because for resizing a rectangle I expected the sizing parameters (3rd and 4th) of rect() were modified, something like this way:
float r = 1;
void setup()
{
size(200,200);
rectMode(CENTER);
frameRate(30);
}
void draw(){
rect(width/2, height/2, r, r);
r += 1;
}
but this way doesn't scale the rectangle smoothly. So, should I use the first approach instead of the second one even if I find it the second one more natural? Any comment will be wellcome.
Regards
Javier
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个解决方案非常复杂,您实际上并不是缩放矩形,而是缩放投影矩阵。投影矩阵是在将某些内容绘制到屏幕上之前应用的一系列数学运算。你可以把它想象成一台相机,但它远比这复杂。
实际上,我将代码并排放置,并没有注意到平滑度有多大差异。我还添加了一些调整,因此这可能有所帮助:)
我所做的主要更改是打开平滑。我认为因为你没有打开它,顶部的方形绘图会跳转到每个像素,但通过平移/缩放操作,它会平滑这些像素。我做的另一件事是在进行缩放操作后平移回左上角。这样做很重要,这样事情就会按照您期望的方式进行。您会注意到缩放矩形现在从中心向外扩展。
The first solution is quite complex, you are not actually scaling the rectangle, but rather the projection matrix. The projection matrix is the series of mathematical operations that are applied before something is drawn to the screen. You can think of it as a camera, but it is far more complex than that.
I actually put the code side by side and didn't notice that much difference in the smoothness. I also added a few tweaks, so that may have helped :)
The main change that I made was to turn on smooth. I think that because you didn't have it on, the top square drawing was jumping to each pixel, but with the tranlation / scale operation, it would smooth those over. The other thing I did, was to translate back to the top left after doing the scale operation. This is important to do so that things draw where you expect them to. You will notice that the scaling rect now scale out from the center.