只是缩放一个矩形

发布于 2024-10-30 07:10:48 字数 709 浏览 1 评论 0原文

我正在尝试从处理开始。

现在我只是想缩放一个矩形。我发现了这个例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-11-06 07:10:48

第一个解决方案非常复杂,您实际上并不是缩放矩形,而是缩放投影矩阵。投影矩阵是在将某些内容绘制到屏幕上之前应用的一系列数学运算。你可以把它想象成一台相机,但它远比这复杂。

实际上,我将代码并排放置,并没有注意到平滑度有多大差异。我还添加了一些调整,因此这可能有所帮助:)

float r = 1;
float s = 0.0;

void setup()
{
  size(200,200);
  rectMode(CENTER);
  frameRate(30);
  smooth();
}

void draw(){
   rect(width/4, height/2, r, r);  
   r += 1.4;
   translate(width/2, height/2);
   scale(s);
   translate(-width/2, -height/2);
   rect(width/2, height/2, 50, 50);
   s += 0.04;
}

我所做的主要更改是打开平滑。我认为因为你没有打开它,顶部的方形绘图会跳转到每个像素,但通过平移/缩放操作,它会平滑这些像素。我做的另一件事是在进行缩放操作后平移回左上角。这样做很重要,这样事情就会按照您期望的方式进行。您会注意到缩放矩形现在从中心向外扩展。

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 :)

float r = 1;
float s = 0.0;

void setup()
{
  size(200,200);
  rectMode(CENTER);
  frameRate(30);
  smooth();
}

void draw(){
   rect(width/4, height/2, r, r);  
   r += 1.4;
   translate(width/2, height/2);
   scale(s);
   translate(-width/2, -height/2);
   rect(width/2, height/2, 50, 50);
   s += 0.04;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文