code redesign
- Started
- Last post
- 4 Responses
- DeIntegro
so i'm thinking that perhaps the best way to learn some programming is to redesign code. there are some differences between the code below. I believe in the idea of designing one thing and then move on to the next. I've also started working backwards and forwards simultaneously. i think i have a better understanding of what i'm doing that way...perhaps it's a quirk.
notes: i started of using nested parentheses, then adjusting the values. The original code doesn't use nested parentheses (minor point, i guess for my thinking it makes sense! but for the original it isn't required. perhaps not succinct enough in my version.
c1dir = c1dir * -1; //in my version
c1dir * = -1; //in 'official' versionthese are obviously not different, but the 'official' version's syntax was a bit perplexing...mine made more sense to me...i guess it was reading the c1dir * that through me off.
let me know what you guys think...beauty is also a criteria!
Here's my version:
float c1 = 0;
float c2 = 255;float c1dir = 0.1;
float c2dir = -0.1;void setup() {
size(200, 200);
}void draw() {
noStroke();//Adjust values
c1 = c1 + c1dir;
c2 = c2 + c2dir;//if color 1 is less than 0 or color 1 is greater than 255 reverse direction
if ((c1 < 0) || (c1 > 255)) {
c1dir = c1dir * -1;
}// if color 2 is less than 0 or color 2 is greater than 255 reverse direction
if ((c2 < 0) || (c2 > 255)) {
c2dir = c2dir * -1;
}//Draw left rectangle
fill(c1,0,c2);
rect(0,0,100,200);//Draw right rectangle
fill(c2,0,c1);
rect(100,0,100,200);
}Here is the official version
// Two variables for color.
float c1 = 0;
float c2 = 255;// Start by incrementing c1.
float c1dir = 0.1;
// Start by decrementing c2.
float c2dir = -0.1;void setup() {
size(200,200);
}void draw() {
noStroke();// Draw rectangle on left
fill(c1,0,c2);
rect(0,0,100,200);// Draw rectangle of right
fill(c2,0,c1);
rect(100,0,100,200);// Adjust color values
c1 = c1 + c1dir;
c2 = c2 + c2dir;// Instead of reaching the edge of a window, these variables reach the "edge" of color:
// 0 for no color and 255 for full color.
// When this happens the direction is reversed.// Reverse direction of color change
if (c1 < 0 || c1 > 255) {
c1dir *= -1;
}if (c2 < 0 || c2 > 255) {
c2dir *= -1;
}
}
- DeIntegro0
Call me crazy but
c1dir *= -1;for me i've never written 1*= -1
it makes sense for me to write:
c1dir = c1dir * -1;just saying as a beginner...the code does exactly the same thing but which one is better to write?
and writing my variables first and then writing my conditionals, then writing my assignment operations then drawing my rectangles worked for me...i don't know it made more sense to work that way...rather than just the traditional way of writing from top to bottom...maybe i'm crazy!
- uan0
This has nothing to do with redesign. It's the same thing.
Redesign is when you rewrite a procedure/algorithm in a better way (faster processing, less use of the machine power, less use of memory, no bugs,...). Shorthand code will help a bit, but probably you will see no difference.
