How do I set a dynamic Y coord?

Hi everyone,

I am having trouble setting Y coordinates dynamically. For example below I am trying to set posY in draw() if posX is above 600. In essence all I am trying to do is make a new row of circles. Am I setting posY in the wrong place?

Thank you!

int posX;
int posY;
int thesize;
int noofcircles;

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofBackground(ofColor:: gray);
    
    posX = 30;
    posY = 30;
    thesize = 10;
    noofcircles = 256;
    
}

//--------------------------------------------------------------
void ofApp::update(){
}

//--------------------------------------------------------------
void ofApp::draw(){
    
   for (int i=0; i < noofcircles; i++){
       
       posX = (i * (thesize * 2));
           
           if(posX > 600){
               posX = 30;
               //posY = 90; 
           }
   
            ofSetColor(255, 0, 255);
            ofCircle(posX, posY, 0, thesize);
        
    }

}

it is moving them to a new row. Since ‘posY’ is a global variable, and you’re altering it, it’s changing the posY for every circle. When you run the program, the first frame has some circle at Y = 30… but once Y is altered to 90, all circles move to y = 90

void ofApp::setup(){
theSize = 10;
noofcircles = 256;

ofBackground(ofColor::black);

}

void ofApp::update(){

}
void ofApp::draw(){
ofVec2f tPos;

int numAcross = 25;
for(int i = 0 ; i < noofcircles ; i ++ ) {
    
    tPos.x = (i % numAcross) * 2.5 * theSize + 30;
    tPos.y = (i / numAcross) * 2.5 * theSize + 30;
    ofSetColor(ofColor::white);
    ofCircle(tPos, theSize);
}
1 Like

Outstanding thank you @BlindElephants Making the var local to draw() worked. Your code is much cleaner. Thank you for the elegant solution!