Converting a PDE to openFrameworks - array lists


#1

Hello - how would I go about converting the following from Processing into openFrameworks? I know it involves using vectors which I struggle with and also the processing sketch uses a thick line which doesn’t work well in oF. They can be changed rectangles with rounded ends.

float[] x=new float[50];
float[] y=new float[50];
float[] stretch = new float[50];
color[] colors = new color[50];
float speed=.4;
color[] Color = {#F01DC9, #0ABDED, #FFFFFF, #17AD9F};

void setup() {
size(960, 540);
strokeWeight(20);
for (int i=0; i<x.length; i++) {
x[i]=random(-100, 800);
}
}

void draw() {

background(0);

for (int i = 0; i < x.length; i++) {
for (int j=0; j<y.length; j++) {
x[i]=x[i]+speed;
y[i]=y[i]+speed;
stroke(colors[i]);
line(x[i], y[i], x[i]+stretch[i], y[i]+stretch[i]);
if (x[i] > 1000 ) {
x[i] =random(-100, -100);
y[i] =random(-800, 800);
stretch[i] = random(15,50);
colors[i] = Color[int(random(Color.length))];
}
}
}
}


#2

You don’t have to use std::vector you can use regular arrays without any problems as you seem to know the array sizes in advance :slight_smile:

The code would actually be very similar. Everything you have before your void setup() goes into the .h file, and ten the rest seems easy enough!

You can use ofSetLineWidth(float w) to set a width?


#3

one thing to be careful about is that java initializes all your variable to 0, so if you do this:

float[] x=new float[50];

you have a 50 float array (x[…]) where each element equals zero.

in c++ you have to initialize them to a value after you allocate them (otherwise they could have whatever value is in that memory address which could be arbitrary)

x = new float[50];
for (int i = 0; i < 50; i++){
x[i] = 0;
}


#4

Thanks Ayruos and Zach - I ended up using a Vector for the arrays.

Including edited code snippet here (removed some of the colors to save space).

in header:

vector posX, posY;
vector stretch;
float speed;
ofColor *ColorBucket;
vector colors;
int lineCount;

in setup:

ColorBucket = new ofColor[5];

//pink
ColorBucket[0].r = 234;
ColorBucket[0].g = 84;
ColorBucket[0].b = 223;

for (int i=0; i<lineCount; i++) {
    posX.push_back(ofRandom(-100, 700));
    posY.push_back(ofRandom(-800, 800));
    stretch.push_back(ofRandom(15, 50));
    colors.push_back(int(ofRandom(0, 5)));
}   

}

//--------------------------------------------------------------
void ofApp::draw(){

for (int i = 0; i < posX.size(); i++) {

posX[i]=posX[i]+speed;
posY[i]=posY[i]+speed;
ofSetColor(ColorBucket[colors[i]]);

   ....................
    
}