Convert Processing sketch to openFrameworks

Hello,

I am trying to port a processing sketch to openFrameworks
No problem with porting types and other specific language usage but there is one thing i don’t know how to deal with :

paths = (pathfinder[]) append(paths, new pathfinder(this));

in the pathfinder update() method.

the paths array is declared elsewhere. And how this ‘paths’ can be used inside the pathfinder class ?
Sorry i don’t really know how to explain the problem… hope i am making myself clear :slight_smile:

here is the Processing code.

thanks a lot

class pathfinder {
  PVector location;
  PVector velocity;
  float diameter;
  pathfinder() {
    location = new PVector(width/2, height);
    velocity = new PVector(0, -1);
    diameter = 32;
  }
  pathfinder(pathfinder parent) {
    location = parent.location.get();
    velocity = parent.velocity.get();
    float area = PI*sq(parent.diameter/2);
    float newDiam = sqrt(area/2/PI)*2;
    diameter = newDiam;
    parent.diameter = newDiam;
  }
  void update() {
    if (diameter>0.5) {
      location.add(velocity);
      PVector bump = new PVector(random(-1, 1), random(-1, 1));
      bump.mult(0.1);
      velocity.add(bump);
      velocity.normalize();
      if (random(0, 1)<0.02) {
        paths = (pathfinder[]) append(paths, new pathfinder(this));
      }
    }
  }
}
pathfinder[] paths;
void setup() {
  size(800, 600);
  background(0);
  ellipseMode(CENTER);
  fill(255);
  noStroke();
  smooth();
  paths = new pathfinder[1];
  paths[0] = new pathfinder();
}
void draw() {
  for (int i=0;i<paths.length;i++) {
    PVector loc = paths[i].location;
    float diam = paths[i].diameter;
    ellipse(loc.x, loc.y, diam, diam);
    paths[i].update();
  }
}
void mousePressed() {
  background(0);
  paths = new pathfinder[1];
  paths[0] = new pathfinder();
}

Working in C++ you shouldn’t really need to use “new” keywords, unless you want to do specific things.

In your case, you just want to append a pathfinder the the end of the paths list so I would recommend changing:

pathfinder[] paths;
to
vector<pathfinder> paths;

paths = new pathfinder[1];
to
paths.resize(1);

remove paths[0] = new pathfinder();

paths = (pathfinder[]) append(paths, new pathfinder(this));
to
paths.emplace_back(this);

There are a few other errors probably.

If you do not know this you do not have a basic understanding of the c++ language syntax. There are so many good tutorials out there. Just read up on them. This is a good starting point www.cplusplus.com/doc/tutorial

hello,

thanks for the answer.

As i said, i have no problem converting it into C++ and changing arrays into vectors and so on.

The problem i have is where do i have to declare the paths array (or vector in C++)

 pathfinder[] paths;

when this paths array is used into the pathfinder class itself

 paths = (pathfinder[]) append(paths, new pathfinder(this));

Oh I see, I thought the code you posted was already converted to C++, my bad!

You can declare it inside the class itself, in the .h

yes i tried but no luck.
paths is also used in the main app in setup(), draw() and mouse event

  paths = new pathfinder[1];
  paths[0] = new pathfinder();
  ...

Then it is a global variable, which is not the best design really… I would recommend to pass in the variable into the pathfinder class:

class pathfinder
{
    pathfinder(vector<pathfinder> * paths) : m_paths(paths) 
    {}
    //...
    void update()
    {
        //..
        // use m_paths here
        //...
    }
};

And define the vector<pathfinder> paths in the .h of the ofApp

ok i will have a look at it.
or maybe i shouldn’t try to translate it word by word but redesign some parts like this paths logic…

That, my friend, is a very good idea