ofPath in function - why so slow?

Hi there, I’m trying to draw an arc, in a box, that rotates at the centre.

I have the code below, which ‘works’ but the frame rate becomes progressively more sluggish.

Is there a way to do this more economically? I eventually want to draw several of this shape at once which is why I’ve used a function and not established the path in setup();

Any advice would be very welcome!

//--------------------------------------------------------------
void ofApp::setup() {
angle = 0;
ofSetFrameRate(60);
}

//--------------------------------------------------------------
void ofApp::arc() {
ofPushMatrix();

ofTranslate(250, 325);
ofRotate(angle);

path.setColor(0);
path.arc(0, -125, 125, 125, 0, 180);
path.setCircleResolution(100);
path.setStrokeWidth(3);
path.setFilled(false);

path.draw();

ofNoFill();
ofSetLineWidth(3);
ofSetRectMode(OF_RECTMODE_CENTER);
ofDrawRectangle(0, 0, 250, 250);


ofPopMatrix();

}

//--------------------------------------------------------------
void ofApp::update(){
angle++;
}

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

}

Hi @k_tindle,
Your arc() function add an new arc to the ofPath each time. You see only one because they are all in the same place, but after 100 calls the ofPath is built with 100 arc.
You can either:

  • put a path.clear() at the beginning of the function arc()
  • define the path only once in setup(), if you don’t want to change its shape and only draw it at different position, scale or angle.

This is really helpful - thank you!
I fixed it by defining it once in setup.