在上一篇的类文件中增加一个减法函数——sub
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;
}
}
利用减法函数计算屏幕鼠标位置和中心点的距离并实时显示鼠标位置到中心点的线条。
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);
translate(width/2, height/2);
line(0,0, mouse.x,mouse.y);
}