处理代码如下:
int maxCircle = 200;
float minDistance=2;
float distance;
Circle [] circles= new Circle[maxCircle];
void setup(){
size(800,800);
smooth();
for(int i=0;i<maxCircle;i++){
circles[i] = new Circle(random(width),random(height),random(2,20));
}
}
void draw(){
background(255,255);
for(int i=0;i<maxCircle;i++){
circles[i].update(width,height);
for(int j=0;j<maxCircle;j++){
distance = dist(circles[i].x,circles[i].y,circles[j].x,circles[j].y);
if(distance<minDistance){
stroke(0,50);
noFill();
line(circles[i].x,circles[i].y,circles[j].x,circles[j].y);}}
circles[i].display();
}
}
void mouseMoved(){
for(int i = 0; i<maxCircle;i++){
circles[i].x+=(mouseX-circles[i].x)*.2;
circles[i].y+=(mouseX-circles[i].y)*.2;}}
class Circle{
float x,y,vx,vy,r,speed;
Circle(float tempx, float tempy, float tempr){
x=tempx;
y=tempy;
vx=random(-1,1);
vy=random(-1,1);
r=tempr;
}
void update(int w,int h){
x+=vx;
y+=vy;
if(x<r || x>w-r){
vx*=-1;};
if(y<r || y>h-r){
vy*=-1;};
}
void display(){
fill(0,50);
noStroke();
ellipse(x,y,r,r);
}
}
两个问题:
为什么线功能不起作用?
我怎样才能使圆圈跟随我的鼠标平滑移动(分别)而不是突然挤压到一个点?