Failing to follow cursor

please format code with </> button * homework policy * asking questions

Hi all,

I am a newbie. I was trying to create a trapezoid that follows the cursor. Yet, I failed. I have given it many tries but it seems that it is just impossible. I am also trying to create straight lines between the centroid of the trapezoid to the mouse cursor while it moves. The shape should start moving once the mouse is clicked and shall stop when it is clicked again or when it reaches one of the boundaries of the display window. I am really stuck with this and I have tried many combinations. As you see from the code, I got it to work very partially. I would really appreciate your feedback regarding this :slight_smile: Here is my code.


float dx,dy,x,y,x1,x2,x3,x4,y1,y2,y3,y4,x1_dist,y1_dist,x2_dist,y2_dist,x3_dist,y3_dist;
float x4_dist,y4_dist;
float dim_x,dim_y;
boolean click,x_change,y_change;
int dir_x,dir_y;


void setup(){
  size(400,400);
  background(255);
  stroke(0);
  strokeWeight(3);
  fill(255,0,0);

  frameRate(10);
  click=false;
  noLoop();
     x1=random(width/4,3*width/4);
  x2=random(width/4,3*width/4);
  x3=random(width/4,3*width/4);
  x4=random(width/4,3*width/4);
  y1=random(height/4,3*height/4);
  y2=random(height/4,3*height/4);
  y3=random(height/4,3*height/4);
  y4=random(height/4,3*height/4);
  x=(x1+x2+x3+x4)/4;
  y=(y1+y2+y3+y4)/4;
  if (x1<x){x1_dist=x-x1;}
  else{x1_dist=x1-x;}
    if (x2<x){x2_dist=x-x2;}
  else{x2_dist=x2-x;}
    if (x3<x){x3_dist=x-x3;}
  else{x3_dist=x3-x;}
    if (x4<x){x4_dist=x-x4;}
  else{x4_dist=x4-x;}
   if (y1<y){y1_dist=y-y1;}
  else{y1_dist=y1-y;}
    if (y2<y){y2_dist=y-y2;}
  else{y2_dist=y2-y;}
    if (y3<y){y3_dist=y-y3;}
  else{y3_dist=y3-y;}
    if (y4<y){y4_dist=y-y4;}
  else{y4_dist=y4-y;}
  dim_x=x;
  dim_y=y;
x_change=y_change=false;
}
void mouseClicked(){
  
  click=!click;
  if (click==true){
    loop();
  r=(int) random (255);
  g= (int) random (255);
  b= (int) random (255);
 
  dx=mouseX;
  dy=mouseY;
  }
  else{noLoop();}
}

void draw (){
   background(255);
  float difference_x, difference_y;
  int step;
   dir_x=1;
   dir_y=1;
  step=5;

  difference_x=mouseX-x;
  difference_y=mouseY-y;

  draw_trapezoid(x_change,y_change,dim_x,dim_y);
  x_change=y_change=false;
  difference_x= mouseX-dim_x;
  difference_y= mouseY-dim_y;
  if ((abs(difference_x)<=abs(difference_y) && difference_x!=0) || (abs(difference_x)>abs(difference_y) && difference_y == 0)){
    if(difference_x<0){
      dim_x=(dim_x-abs(difference_x))*-1;
    }else{
         dim_x=dim_x+(difference_x);
    }
    x_change=true;
  }else{
    if(difference_y !=0){
      if(difference_y<0){
        dim_y=(dim_y-abs(difference_y))*-1;
       
      }else{
        dim_y=dim_y+difference_y;
      }
       println(dim_y);
      y_change=true;
    }
  }

}
void draw_trapezoid(boolean x_status,boolean y_status,float posX, float posY){
if (x_status){
   quad(x1+posX,y1,x2+posX,y2,x3+posX,
    y3,x4+posX,y4);
  
}
else if(y_status){
      quad(x1,y1+posY,x2,y2+posY,x3,
      y3+posY,x4,y4+posY);
}
 }

1 Like

It´s easy in fact, but I believe you want something else as well. Please explain, because this appears to me a little spaghetti like.