We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Ok, well that figures - I looked some more and solved it:
It would be nice to get 360 degree measurements instead of 180, -180 though.
use the mod operator :
degreeVal = (degreeVal + 360) % 360;
See % (modulo)
You can reduce the code by passing a version parameter
never heard of a version parameter, that made things much better. thanks for the help!
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
makes more sense. The second approach often can be more readable, and therefore more maintainable.