Getting Started with Processing 第五章的easing问题(2)

时间:2023-03-10 02:30:42
Getting Started with Processing 第五章的easing问题(2)

程序代码清单如下:

float x;
float y;
float px;
float py;
float easing = 0.05; void setup(){
size(480,120);
stroke(0,102);
} void draw(){
float targetX = mouseX;
x+=(targetX-x)*`easing`;
float targetY = mouseY;
y+=(targetY-y)*`easing`
line(x,y,px,py);
py=y;
px=x;
}

首先,在这里,作者首先将上一个例子中的一维变成了二维

float targetX = mouseX;
x+=(targetX-x)*easing;
float targetY = mouseY;
y+=(targetY-y)*easing;

然后,作者通过利用一个参数 weight,从 dist() 函数返回一个值,然后将之作为 line 的weight.

同时,当程序进行的时候,line(x,y,px,py);是从目前这一点到上一点画线。

关于px =x;py=y;

但是,程序进行到这里, 这个时候 px 和 py 没有被赋值,所以将目前还未进行下一次循环的 x 和 y 值赋值给 px 和 py.
程序进行到下一次循环的时候,x 和 y 的值通过鼠标的移动,于是 x 和 y 的值再次改变。进行下面的程序。