Prcessing following control
in
Programming Questions
•
1 year ago
code as below:
- 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);
- }
- }
Two questions:
1.Why doesn't the line function work?
2.How can i make circles move smoothly(respectively) following my mouse instead of squeezing into one point abruptly?
Thank you very much.
1