开发者

Just scaling a rectangle

开发者 https://www.devze.com 2023-02-22 06:51 出处:网络
I\'m trying to start with processing. Now I\'m just trying to scale a rectangle. I have found this example:

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);开发者_如何学Python 
  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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消