Processing 处理点管理

Processing 处理点管理,processing,point,Processing,Point,我正在制作一个简单的处理程序,创建一定数量的随机移动的点,然后单击鼠标时,这些点移动到鼠标的位置 我把这些点做成了椭圆,这样比较容易看 //number of points int ptnum=2; //list of points Point[] points=new Point[ptnum]; //class to create points class Point { float xpos; float ypos; //constructor Point(float x

我正在制作一个简单的处理程序,创建一定数量的随机移动的点,然后单击鼠标时,这些点移动到鼠标的位置

我把这些点做成了椭圆,这样比较容易看

//number of points
int ptnum=2;
//list of points
Point[] points=new Point[ptnum];

//class to create points
class Point
{
  float xpos;
  float ypos;

  //constructor
  Point(float x, float y){
    xpos=x;
    ypos=y;
  }

  //return x-coordinate  
  float ptx(){
    return xpos;
  }
  //return y-coordinate
  float pty(){
    return ypos;
  }

  //points randomly moving
  void randMove(){
     xpos+=random(-2,2);
     ypos+=random(-2,2);
  }

  //display points
  void display(){
    fill(0);
    ellipse(xpos,ypos,2,2);
  }

  //move points to mouse
  void move(){
    if(xpos>mouseX){
      xpos-=1;
    }
    if(ypos>mouseY){
      ypos-=1;
    }
    if(ypos<mouseY){
      ypos+=1;
    }
    if(xpos<mouseX){
      xpos+=1;
    }
  }
}


void setup(){
  size(640,360);

  //create ptnum of points
  for(int i=0; i<ptnum; i++){
    points[i]=new Point(random(1,width-1),random(1,height-1));
  }
}

//each point to random move
void randomMovement(){
  for(int i=0; i<ptnum; i++){
    points[i].randMove();
  }
}

//each point to display
void ptDisplay(){
  for(int i=0; i<ptnum; i++){
    points[i].display();
  }
}

//each point to move
void ptMove(){
  for(int i=0; i<ptnum; i++){
    points[i].move();
  }
}

void draw(){
  //start
  background(255,255,255);
  noFill();
  ptDisplay();
  //==========

  //if mouse clicked, move points to mouse XandY, if not-randommove
  if(mousePressed){
    ptMove();
  }
  else{
    randomMovement();
  }
}
//点数
int ptnum=2;
//要点清单
点[]点=新点[ptnum];
//类来创建点
类点
{
浮动XPO;
浮动YPO;
//建造师
点(浮动x、浮动y){
xpos=x;
ypos=y;
}
//返回x坐标
浮动ptx(){
返回XPO;
}
//返回y坐标
浮点数{
返回YPO;
}
//随机移动的点
无效移动(){
xpos+=随机(-2,2);
ypos+=随机(-2,2);
}
//显示点
无效显示(){
填充(0);
椭圆(xpos,ypos,2,2);
}
//将点移动到鼠标
无效移动(){
如果(xpos>mouseX){
xpos-=1;
}
如果(ypos>鼠标){
ypos-=1;
}

如果(ypos只需计算绘图函数中每个点的位置。 然后,运行for循环以查看是否存在任何碰撞。 由于draw函数每秒运行多次,因此结果将足够好

发生碰撞时,可以将每个点的方向更改180度