Hi need help with vector objects

hi i been trying to get my head around vectors so i like to make a grid of a simple object but i can figure out how can i pass a new position to an object on a vector
i want to make each object store on a vector to be drawn in a different location on screen
so far i came up with this which it seem to me like a dumb idea to me can someone help me up with this or some clues of how to ??
thanks

void ofApp::draw(){
    
//    for (int i = 0; i < numDots; i++) {
//        ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
//        ofPopMatrix();
//        dot[i].draw();
//        ofPushMatrix();
//        }
    // no entiendo como ???

    for (posX = 0; posX<ofGetWidth(); posX = posX + 20) {
        for (int posY = 0; posY <ofGetHeight(); posY = posY+20) {
            for (int i = 0 ; i < numDots; i++) {
                ofTranslate(posX,posY);
                dot[i].draw();

            }
        }
    }

    
    
}

Hey @cyrstem -

I think you are over-thinking this a bit (everyone does!) - but I think first it’d be best to define exactly what you want to create.

  • a grid of dots (or square, or whatever)
  • OR, a vector of positions to draw things with. (Would make sense if you want to change the positions over time, etc.)

drawing a grid of shapes (10px square rectangles in this case):

void ofApp::draw(){
  uint colSize = 20;
  uint rowSize = 20;
  for( uint x = 0; x < ofGetWidth(); x += colSize ){
    for(uint y = 0; y < ofGetHeight(); y += rowSize ){
       ofPushMatrix();
       ofTranslate( x, y);
       ofDrawRectangle(0,0,10,10);
       ofPopMatrix();
    }
}

Creating a vector of positions to use to draw anything could look something like this:

in ‘ofApp.h’

vector<ofPoint> positions;  // could be ofVec2f, ofVec3f...    

in ‘ofApp.cpp’

void ofApp::setup(){
  // initialize vector with x/y coordinates:    
  uint colSize = 20;
  uint rowSize = 20;
  for( uint x = 0; x < ofGetWidth(); x += colSize ){
    for(uint y = 0; y < ofGetHeight(); y += rowSize ){
        ofPoint p(x,y);
        positions.push_back( p );
    }
  }
}

void ofApp::update(){
   // always best to do any non-drawing specific stuff in 'update'

   // move each position by 1 px in both directions, 
   // wrapping them back to 0 if they go outside the window
    ofPoint modifier( 1, 1);
    for( uint i= 0; i < positions.size(); i++ ){
       positions[i] += modifier;
       if( positions[i].x > ofGetWidth() ) positions[i].x = 0;
       if(positions[i].y > ofGetHeight() ) positions[i].y = 0;
   }
}

void ofApp::draw(){
 // set to draw white
  ofSetColor( 255 );

  //  draw things at the positions (a 10x10 rectangle in this case)
  for( uint i = 0; i < positions.size(); i++ ){
     ofDrawRectangle( positions[i], 10, 10 );
  }
}

The rectangles should scroll down and to the right, and wrap back endlessly. You’d need to do a little more math to get the grid to be perfect, it depends on window size and the size of the thing you want to draw, but that’s the fun stuff.

Cheers!

hi, thanks i try the second one which was the idea i was going for but i have a question, here what if what my object to be in a class and each time is draw has a different value of size or something and
what is the difference between uint and int ?
thanks and sorry for so many questions

uint significa que es unsigned int, vamos que su valor puede ser negativo mientras que un int siempre es poditivo. La otra pregunta no te entiendo.

Uint means unsigned int, value can be negative as opossed to int

dale, mi idea originalmente era crear dos vectores en uno guardar las posiciones y en el otro guardar los objetos, lo que quería hacer es que cada vez que se dibuje un objecto en una nueva posición sea diferente del otro en tamaño o algo así
esa era la idea no lograba hacer que se pasen esos datos al objeto guardado dentro del vector
gracias