June 10, 2011

Angle Reference Circle in Processing

When I think back to trig, I seam to remember 0 degrees always being horizontal and to the right of the center. That turns out to be pretty arbitrary. Different programming languages tend to have different ideas about what angle is considered 0. It's not a problem, just something you need to know in order to get the angels your expecting. The following image is of a sketch I made in Processing to show what it considers 0. Turns out it's down and vertical. I went ahead and made the whole circle with some tick marks for practice. The labels show the angle in both degrees and radians. The lines in the center are random, the numbers on them just show the order in which they were created. The code to create them later served as the base for the branches in the random recursion tree from my last post.
I made a screen shot and used it as a reference to help visualize angles while programming but without the overhead of having a Processing sketch running. Perhaps it could be useful to someone else as well. If someone wants a cleaner version without the center lines, let me know and i will add it. Or just make your own. Here is the code I used, simplified a bit though so it just draws the main circle with tick marks and labels but does not put those random lines in the center.


Circle c;

void setup(){
  size(700,700);
  noLoop();
  background(0,0,0);
  smooth();

  c = new Circle(300);
}

void draw(){

}

//This code goes in a tab named Circle////////////////////
class Circle{
  int radius;
  int xcenter;
  int ycenter;
  float angleR;
  int angleD;
  int angleOffset;
  boolean dispRadians;

  Circle(int r){
    radius = r;
    xcenter = width/2;
    ycenter = height/2;
    dispRadians = true;
  
    drawFullCirc();
    drawAll(true);
  }

  void drawAll(boolean dr){
    dispRadians = dr;
    for(int i=0;i<360;i++){
      drawPoint(i,radius,5);
    }
  }

  void drawFullCirc(){
    stroke(100,100,100);
    noFill();
    ellipse(xcenter,ycenter,radius*2,radius*2);
  
    strokeWeight(10);
    point(xcenter,xcenter);
  }

  void drawPoint(int angleD, int r, int tI){ //tI = tickInterval;
    strokeWeight(1);
    int xpos;
    int ypos;
  
    strokeWeight(3);
    stroke(255,0,0);
  
    angleR = angleD * (PI/180);

    xpos = int(float(xcenter)  +sin(angleR)*radius);
    ypos = int(float(ycenter)  +cos(angleR)*radius);
    point(xpos,ypos);
    if(angleD % tI == 0){
      if(angleD % 45 == 0){
        displayText(angleD,angleR,int(float(xcenter)+sin(angleR)*(r+30)),int(float(xcenter)+cos(angleR)*(r+30)));
      }else if(angleD % 10 == 0){
        displayText(angleD,angleR,int(float(xcenter)+sin(angleR)*(r+30)),int(float(xcenter)+cos(angleR)*(r+30)));
      }
      drawTicks(angleR,angleD);
    }
  }

  void displayText(int aD,float aR, int x, int y){
    textSize(9);
    text("D:"+(aD),x-15,y);
    if(dispRadians){
      aR = float(round(aR*100))/100;
      text("R:"+aR,x-15,y+10);
    }
  }

  void drawTicks(float aR,int aD){
    int x1;
    int y1;
    if(aD % 90 == 0){
      x1 = int(float(xcenter)+sin(aR)*(radius-100));
      y1 = int(float(xcenter)+cos(aR)*(radius-100));
    }else if(aD % 45 == 0){
      x1 = int(float(xcenter)+sin(aR)*(radius-60));
      y1 = int(float(xcenter)+cos(aR)*(radius-60));
    }else{
      x1 = int(float(xcenter)+sin(aR)*(radius-30));
      y1 = int(float(xcenter)+cos(aR)*(radius-30));
    }
    int x2 = int(float(xcenter)+sin(aR)*(radius+10));
    int y2 = int(float(xcenter)+cos(aR)*(radius+10));
    strokeWeight(2);
    stroke(100,100,100);
    line(x1,y1,x2,y2);
  }
}


If this is useful for anyone, please leave a comment. Thanks.

No comments:

Post a Comment