Get Rectangle to follow moving Arc

edited November 2014 in How To...

I'm trying to stick a rectangle on my moving arcs and I can't seem to get the logic down. Could someone help me out please?

To be specific, I want a rectangle attached to the arc and as the arc rotates the rectangle moves with it. Thanks so much!

Arc a1;
void setup(){
 size(600,600);
 background(0);
 strokeWeight(4);

 a1=new Arc(width/2,height/2,150,0,QUARTER_PI+PI);
 noLoop();
}
void draw(){
 clear();
 float r1=0;
 float r2=0;
 stroke(153,255,51);
 a1.display();

 r1+=PI/25;
 r2+=PI/25; 
 a1.rotate(r1,r2);
}


void mousePressed(){
  loop();
}
void mouseReleased(){
  noLoop();
}

class Arc
{
  float xpos;
  float ypos;
  float size;
  float astart;
  float aend;
  Arc(float x,float y,float asize,float start, float end){
    xpos=x;
    ypos=y;
    size=asize;
    astart=start;
    aend=end;
  }
  void rotate(float posx, float posy){
    astart+=posx;
    aend+=posy;
  }
  void display(){
    noFill();

    arc(xpos,ypos,size,size,astart,aend);
  }
}
Tagged:

Answers

  • Answer ✓

    There is a great Processing tutorial on some ideas related positioning things like this: https://www.processing.org/tutorials/trig/

    void display() {
      noFill();
      arc(xpos, ypos, size, size, astart, aend);
      rect(xpos+cos(astart)*size/2-5, ypos+sin(astart)*size/2-5, 10, 10);
    }
    
  • I would've never gotten that far, trig wasn't my strongest suit. Thanks a lot man!

Sign In or Register to comment.