hello,
three days ago i had my first contact with processing.
and well - i like it.
at the moment i try to change sketches i found in the web.
and until yet this was no bad idea - i've learned a lot.
but with this project i need help.
i try to change following code into classes on two tabs
and make it as simple as possible
(get rid of everything i don't need to get the same result)
it's a very simple program...
three lines move with different speed on the y-axis
and two of them changes color from red to yellow/cyan
the slowest is always yellow.
beyond this i'm asking myself
on how many ways you can write this
by getting the same result?
what is the simplest way to do it?
this is the state of play
Code:
// Declare and construct tree objects (h1, h2, h3) from the class HLine
// Objects habe different kind of color behavior.
HLine h1 = new HLine(color(255), color(0), color(0), -100, 2.0);
HLine h2 = new HLine(color(255), color(0), color(0), -30, 2.5);
HLine h3 = new HLine(color(0), color(255), color(0), -30, 0.4);
void setup()
{
size(255, 255);
frameRate(24);
smooth();
}
void draw() {
background(170);
h1.update();
h2.update();
h3.update();
h1.tempColor01();
h2.tempColor02();
h3.tempColor03();
}
class HLine {
color r, g, b;
float ypos, speed;
HLine (color tempR, color tempG, color tempB, float y, float s) {
r = tempR;
g = tempG;
b = tempB;
ypos = y;
speed = s;
}
void update() {
ypos += speed;
if (ypos > width) {
ypos = 0;
}
line(0, ypos, width, ypos);
}
void tempColor01() {
float g = ypos;
if (g > height) {
g = (0);
}
stroke(255,g,b);
strokeWeight(4);
line(0, ypos, width, ypos);
}
void tempColor02() {
float b = ypos;
if (b > height) {
b = (0);
}
stroke(255-b,b,b);
strokeWeight(2);
line(0, ypos, width, ypos);
}
void tempColor03() {
stroke(255,255,0);
strokeWeight(0);
line(0, ypos, width, ypos);
}
}
and it looks like this with two tabs... but it does not work yet.
Code:
HLine myh1;
HLine myh2;
HLine myh3;
void setup(){
size(255, 255);
frameRate(24);
smooth();
}
void draw() {
background(170);
myh1.update();
myh1.display();
myh2.update();
myh2.display();
myh3.update();
myh3.display();
float c = color();
myh1 = new HLine(color(255, c, c), -100, 2.0);
myh2 = new HLine(color(255-c, c, c), -30, 2.5);
myh3 = new HLine(color(255, 255, 0), -30, 0.4);
}
and the 2nd tab named HLine"
Code:
class HLine{
color c;
float ypos, speed;
HLine(color tempC, float y, float s) {
c = tempC = color();
ypos = y;
speed = s;
}
void display() {
fill (c);
line(0, ypos, width, ypos);
stroke (s);
}
void update() {
ypos += speed;
if (ypos > width) {
ypos = 0;
}
}
}