在向量的类文件中增加一个乘法函数mult(),利用该函数对向量进行乘法运算。
class PVector{
float x;
float y;
PVector(float x_,float y_){
x = x_;
y = y_;
}
void add(PVector v){
y = y + v.y;
x = x + v.x;
}
void sub(PVector v){
x = x - v.x;
y = y - v.y;
}
void mult(float n){
x = x * n;
y = y * n;
}
}
注意,起始这里也可以做除法,把”*”换成”/”即可,或者乘以一个小于1 的数,
void div(float n){
x = x / n;
y = y / n;
}
下面的例子请分别用1.5和0.5演示
PVector mouse;
PVector center;
void setup(){
size(400,400);
smooth();
}
void draw(){
background(255);
mouse = new PVector(mouseX,mouseY);
center = new PVector(width/2,height/2);
mouse.sub(center);
mouse.mult(1.5);
//mouse.mult(0.5);
translate(width/2, height/2);
line(0,0, mouse.x,mouse.y);
}