Physical Computing Midterm Documentation A.1

//Arm Class Tab


class Arm {
  
  //CONSTRUCTOR
  Arm() {
 
  }
  
  //FUNCTIONS
  
  //how to rotate/translate/push/pop the arms?? 
  
    //void rotateArm(float r){
    //  rotate(r);
      
    //} 
      void display(){
      
  x[x.length-1] = width/3;   // Set base x-coordinate
  y[x.length-1] = height/2;  // Set base y-coordinate
    
  }
  
  void positionSegment(int a, int b) {
  x[b] = x[a] + cos(angle[a]) * segLength;
  y[b] = y[a] + sin(angle[a]) * segLength;
}

void reachSegment(int i, float xin, float yin) {
  float dx = xin - x[i];
  float dy = yin - y[i];
  angle[i] = atan2(dy, dx);  
  targetX = xin - cos(angle[i]) * segLength;
  targetY = yin - sin(angle[i]) * segLength;
}

void segment(float x, float y, float a, float sw) {
  strokeWeight(sw);
  pushMatrix();
  translate(x, y);
  //rotate(a);
  line(0, 0, segLength, 0);
  popMatrix();
}
  
}


//Object/functions


//DECLARE
Arm myArm1;
Arm myArm2;

int numSegments = 50;

float[] x = new float[numSegments];
float[] y = new float[numSegments];
float[] angle = new float[numSegments];

float segLength = 4;
float targetX, targetY;


void setup() {

  noCursor(); 
  size(640, 360);
  strokeWeight(20.0);
  stroke(255, 120, 90);
 
  
  //INITIALIZE
  myArm1 = new Arm();
  myArm2 = new Arm();

}

void draw() {
  background(0, 180, 195);
  ellipse(215, 180, 20, 20);
  
  
   pushMatrix();
   rotate (0);
  myArm1.display();
 popMatrix();
 //WHY ARE THESE NOT ROTATING SEPARATELY ACCORDING TO ROTATE FUNCTION radian VALUES????
  pushMatrix();
   rotate (PI/2);
  myArm2.display();
 popMatrix();

 
  
   //pushMatrix();
   //myArm2.rotateArm(PI/8);
   //popMatrix();
   
  // pushMatrix();

  //myArm1.rotateArm(0);
  
  //popMatrix();

  

  
  
  myArm1.reachSegment(0, mouseX, mouseY);
  for (int i=1; i<numSegments; i++) {
    myArm1.reachSegment(i, targetX, targetY);
  }
  for (int i=x.length-1; i>=1; i--) {
    myArm1.positionSegment(i, i-1);
  } 
  for (int i=0; i<x.length; i++) {
    myArm1.segment(x[i], y[i], angle[i], (i+1)*2);
  }
  
    myArm2.reachSegment(0, mouseX, mouseY);
  for (int i=1; i<numSegments; i++) {
    myArm2.reachSegment(i, targetX, targetY);
  }
  for (int i=x.length-1; i>=1; i--) {
    myArm2.positionSegment(i, i-1);
  } 
  for (int i=0; i<x.length; i++) {
    myArm2.segment(x[i], y[i], angle[i], (i+1)*2);
  }
}


Working it out Good Will Hunting style. The green and red lines and notation follow the variables to understand the code structure and the trigonomic algorithms which control the variable relationships inside each function.&nbsp;

Working it out Good Will Hunting style. The green and red lines and notation follow the variables to understand the code structure and the trigonomic algorithms which control the variable relationships inside each function. 

"Octopus arm" composed of 50 segments whose individual size and position are dictated by the cursor's x sin and y cosine. This is what creates an organinc wave effect where each segment is sequentially displaced along an oscillation graph in relatio…

"Octopus arm" composed of 50 segments whose individual size and position are dictated by the cursor's x sin and y cosine. This is what creates an organinc wave effect where each segment is sequentially displaced along an oscillation graph in relation to the mouseX and mouseY original positions.