Life as Clay

Silly small Processing sketch for trig reference

leave a comment »


This is just a small Processing sketch that I’m throwing up here for reference. It seems that every time I’m trying to calculate the point on a circle, I forget whether I’m working in angles or radians, etc…


int     radius, lines;
float   angle;
PVector center;

void setup() {
  size(600, 600); 
  
  lines = (int)random(4, 20);
  
  if (lines % 2 > 0) {
    lines += 1;
  }
  
  radius = 200;
  angle  = 360.0 / lines;
  center = new PVector(width/2.0,height/2,0);
  
  smooth();
}

void draw() {
  background(255);
  fill(0);
  noStroke();
  ellipse(width/2, height/2, radius*2, radius*2);
  
  stroke(255);
  strokeWeight(2);
  
  for(int i = 0; i < lines; i++){
    float thisAngle = i * angle;
    float ptX = center.x + sin(radians(thisAngle))*radius;
    float ptY = center.y + cos(radians(thisAngle))*radius;
    line(center.x,center.y,ptX,ptY);
  }
}

Advertisements

Written by Clay

February 20, 2014 at 10:55

Posted in Processing, Reference

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: