trying to deduce radians and degrees from cartesian polar coordinates

I am working through the futurelearn.com Creative Coding class and am on the 'emergence' section. After playing with the code a bit, I am trying to get circles to show how many radians and degrees are being covered as the ellipse rotates around it. My code is below, look for the comment that says "show degrees and show radians".

I've spent a couple of hours trying to get it right, I just can't seem to reverse engineer the movements:

// variable used to store the current frame rate value
int frame_rate_value, total_num, move_me;
float node_x_one, node_y_one, node_x_two, node_y_two;

void setup() {
  //size(800, 800);
  size(displayWidth, displayHeight);
  move_me = 1;
  total_num = 30;
  frame_rate_value = 18;
  frameRate(frame_rate_value);
  rectMode(CENTER);
  background(255);
}


void draw() {

  background(255);

  int num = total_num;
  int margin = 0;
  float gutter = 0; //distance between each cell
  float cellsize = ( width - (2 * margin) - gutter * (num - 1) ) / (num - 1);

  int circleNumber = 0; // counter
  for (int i=0; i<num; i++) {
    for (int j=0; j<num; j++) {
      circleNumber = (i * num) + j; // different way to calculate the circle number from w2_04

      float ty = margin + cellsize * i + gutter * i;
      float tx = margin + cellsize * j + gutter * j;

      if (circleNumber % 2 == 0) {
        movingCircle(tx, ty, cellsize, circleNumber);
      } else {
        movingCircle2(tx, ty, cellsize, circleNumber);
      }

      if (node_x_one >= 0 && node_x_two >= 0 
        && node_x_one <= displayWidth-5 && node_x_two <= displayWidth-5 
        && node_y_one <= displayHeight-5 && node_y_two <= displayHeight-5) {
        line(node_x_one, node_y_one, node_x_two, node_y_two);
      }

      //text(circleNumber, tx-10, ty+5);
    }
  }
}


void movingCircle(float x, float y, float size, int circleNum) {

  float finalAngle;
  finalAngle = frameCount + circleNum;

  //the rotating angle for each tempX and tempY postion is affected by frameRate and angle;  
  //float tempX = x + (size / 2) * sin(PI / frame_rate_value * (circleNum + move_me));
  //float tempY = y + (size / 2) * cos(PI / frame_rate_value * (circleNum + move_me));

  float tempX = x + (size / 2) * sin(PI / frame_rate_value * finalAngle);
  float tempY = y + (size / 2) * cos(PI / frame_rate_value * finalAngle);

  node_x_one = tempX;
  node_y_one = tempY;

  stroke(155);
  line(x, y, tempX, tempY);

  noFill();
  stroke(tempX, tempY/8, circleNum/50, 50);
  ellipse(x, y, size, size);

  fill(x/6, y/6, (x+y)/2);
  ellipse(tempX, tempY, size/5, size/5);
}

void movingCircle2(float x, float y, float size, int circleNum) {

  float finalAngle;
  finalAngle = frameCount + circleNum;

  //the rotating angle for each tempX and tempY postion is affected by frameRate and angle;  
  //float tempX = x + (size / 2) * sin(PI / frame_rate_value * (circleNum + move_me));
  //float tempY = y + (size / 2) * cos(PI / frame_rate_value * (circleNum + move_me));

  float tempX = x + (size / 2) * sin(PI / frame_rate_value * finalAngle);
  float tempY = y + (size / 2) * cos(PI / frame_rate_value * finalAngle);

  node_x_two = tempX;
  node_y_two = tempY;

  noFill();
  stroke(tempX, tempY/8, circleNum/50, 50);
  ellipse(x, y, size, size);

  fill(x/6, y/6, (x+y)/2);
  ellipse(tempX, tempY, size/5, size/5);

  textAlign(CENTER, CENTER);
  textSize(10);

  //show degrees and also show radians
  //text(((sin(PI / frame_rate_value * finalAngle))/TWO_PI), x, y);
}


void keyPressed() {

  // INCREASE
  if (keyCode == RIGHT && frame_rate_value < 60) {
    frame_rate_value++;
  }
  if (keyCode == UP) {
    total_num++;
  }


  // DECREASE
  if ( keyCode == LEFT && frame_rate_value > 1) {
    frame_rate_value--;
  }
  if (keyCode == DOWN) {
    total_num--;
  }

  // basically 'HIT PLAY'
  //if (key == 'a') {
  //  move_me = frameCount;
  //}

  // set the frameRate and print current value on the screen
  frameRate(frame_rate_value);
  println("Current frame Rate is: " + frame_rate_value + 
    ", speed is: " + move_me + ", number of particles is: " + total_num );
}

Answers

  • edited July 2014

    Ok, well that figures - I looked some more and solved it:

      //show degrees and also show radians
      text(nfp(degrees((atan2(y-tempY,x-tempX))),0,0), x, y);
      //text(degrees((atan((y-tempY)/(x-tempX)))), x, y);
    

    It would be nice to get 360 degree measurements instead of 180, -180 though.

  • edited July 2014 Answer ✓

    use the mod operator :

    degreeVal = (degreeVal + 360) % 360;

    See % (modulo)

  • edited July 2014 Answer ✓

    You can reduce the code by passing a version parameter

          if (circleNumber % 2 == 0) {
            movingCircle(tx, ty, cellsize, circleNumber, 1);
          } else {
            movingCircle(tx, ty, cellsize, circleNumber, 2);
          }
    

    void movingCircle(float x, float y, float size, int circleNum, int version) {
    
      float finalAngle;
      finalAngle = frameCount + circleNum;
    
      //the rotating angle for each tempX and tempY postion is affected by frameRate and angle;  
      //float tempX = x + (size / 2) * sin(PI / frame_rate_value * (circleNum + move_me));
      //float tempY = y + (size / 2) * cos(PI / frame_rate_value * (circleNum + move_me));
    
      float tempX = x + (size / 2) * sin(PI / frame_rate_value * finalAngle);
      float tempY = y + (size / 2) * cos(PI / frame_rate_value * finalAngle);
    
    
      if (version == 1)
      {
        node_x_one = tempX;
        node_y_one = tempY;
    
        stroke(155);
        line(x, y, tempX, tempY);
      }
      if(version == 2)
      {
        node_x_two = tempX;
        node_y_two = tempY;
      }
      noFill();
      stroke(tempX, tempY/8, circleNum/50, 50);
      ellipse(x, y, size, size);
    
      fill(x/6, y/6, (x+y)/2);
      ellipse(tempX, tempY, size/5, size/5);
    
      if (version == 2)
      {
        textAlign(CENTER, CENTER);
        textSize(10);
    
        //show degrees and also show radians
        //text(((sin(PI / frame_rate_value * finalAngle))/TWO_PI), x, y);
      }
    }
    
  • edited July 2014

    never heard of a version parameter, that made things much better. thanks for the help!

  • edited July 2014

    In general if you find yourself repeating code segments more than once then there may be a way to improve the code organization. Sometimes adding a paramater, call it version or flavor or somesuch and adding if statements to a copy of the repeated code will do the trick.

    Other times splitting the repeated code , X and X' into separate modules A, B, B', and C with control outside as in

    A();
    if(somesuch)
    {
      B();
    } else
    {
      B'();
    }
    C();
    

    makes more sense. The second approach often can be more readable, and therefore more maintainable.

Sign In or Register to comment.