2015-02-06 3 views
1

무작위로 움직이는 일정량의 포인트를 생성하는 간단한 프로세싱 프로그램을 만들고 마우스를 클릭하면 포인트가 마우스 위치로 이동합니다.프로세싱 포인트 관리

포인트를보다 쉽게 ​​볼 수 있도록 타원으로 만들었습니다.

//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(); 
    } 
} 

포인트가 상호 작용할 수 있도록하려고합니다. 예를 들어, 서로 접촉 할 수 없습니다. 누군가 나를 알아낼 수있게 도와 줄 수 있습니까? 나는 이것에 약간 두뇌 방구를 가지고있다. 누구든지 코드에 대한 제안 사항이 있으면 기꺼이 들려 줄 것입니다.

감사합니다. 도움을 많이 주셔서 감사합니다.

답변

1

그리기 기능에서 각 지점의 위치를 ​​계산하면됩니다. 그런 다음 for 루프를 실행하여 충돌이 있는지 확인하십시오. 그리기 기능이 초당 여러 번 실행되므로 결과가 충분합니다.

충돌이 발생하면 각 지점의 방향을 180 도씩 변경할 수 있습니다.