How do I center and secure one end of a trail of segments in the middle of the screen? ... Kinematic

https://processing.org/examples/arm.html

This is an example of how I want my arm to move. I want it to have one end stuck in the center and the rest of the segments will extend if I move the mouse to the other end of the screen.

This is what I have so far:

    //Driver
    //Segment a;
    Trail b;
    void setup(){
      size(800,600);

      //Segment at center of screen with length 80 pixels.
      //a=new Segment(width/2,height/2,20);
      b= new Trail(100,5);

    }
    void draw()
    {
      background(0);
      //a.drag(mouseX,mouseY);
      //a.display();

      b.display();
      b.drag(mouseX,mouseY);
    }


    //segment class
    public class Segment{
      float x,y; //initial point
      float len; //length of segment
      float angle; //angle of segment
      public Segment(float x, float y, float len){
        this.x = x;
        this.y = y;
        this.len = len;
        angle=0;
      }

      public float endX(){
        return x+cos(angle)*len;

      }
      public float endY(){
        return y+sin(angle)*len;
      }
      public void rotateToXY(float X, float Y){
        angle = atan2(Y-y, X-x);
      }
      public void drag(float x, float y){    
        rotateToXY(x,y);
        this.x = x - cos(angle)*len;
        this.y = y - sin(angle)*len;
      }
      public void display(){
        strokeWeight(50);
        line(x, y, endX(), endY());
        stroke(random(255), random(255), random(255));
      }


}
        //trail class
public class Trail{
  float x,y;
  int len;//number of Segments 
  ArrayList<Segment> trail;
  //add a connected trail of segments
  //hint: use endX and endY of Segment.
  public Trail(int numSeg, int segLength){
    x=width/4;
    y=height/2;
    trail = new ArrayList<Segment>();
    for(int i=0; i<numSeg; i++){
      if(trail.size()<1)
      trail.add(new Segment(x,y,segLength));
      else
      trail.add(new Segment(x+trail.get(i-1).endX(),y+trail.get(i-1).endY(),segLength));

    }
  }

  //use drag of Segment to drag trail of segments.
  public void drag(float x, float y){

   for(int i=0; i<trail.size(); i++){
      if(i == 0){
       trail.get(i).drag(x,y); 
      }
      else {
        trail.get(i).drag(trail.get(i-1).x,trail.get(i-1).y);
      }
   }

   for(int j = trail.size()-1; j>=0; j--) {
     if(j==0) {
       trail.get(j).drag();
     }
   }



  }

  //display trail (use display of Segment)
  public void display(){
    for(int i=0; i<trail.size(); i++){
       trail.get(i).display(); 
    }

  }


}

I know that I need to fix the drag method in trail, but I am not really sure how to drag the other end to the center of the screen...

Thank u

Tagged:

Answers

  • Sample demo below.

    Kf

    float n=20;
    int rad=15;
    void setup() {
    
      size(600, 600);
      ellipseMode(RADIUS);
      fill(255);
    }
    
    void draw() {
      background(0);
      float dw=(mouseX-width/2)/n;
      float dh=(mouseY-height/2)/n;
    
      translate(width/2,height/2);
      for(int i=0;i<n;i++){
        ellipse(dw*i,dh*i,rad,rad);
      }  
    }
    
  • ok thank u. But how do i do that using my trail class

Sign In or Register to comment.