Different colours to fill shapes on random coordinates

Hello there!

I have succeeded in getting the ellipse onto random coordinates onto the screen with different sizes. What I am now trying to do in this for loop is also select a different colour for each of those shapes out of the array I have written out in setup.

With this code it just chooses the last colour that’s one the list. I’m finding it hard to wrap my head around the next steps I need to take to make it run the way I want it to.

Any help would be much appreciated.

.h

vector<int> x;
vector<int> y;

vector<int> sizeA1;
vector<int> sizeA2;

ofColor colorsHD[7];

.cpp

void ofApp::setup(){
ofBackground(255);

//random coordinates
for(int i = 0; i < 1000000; i++){
x.push_back((int)ofRandom(0,ofGetWidth()));
y.push_back((int)ofRandom(0,ofGetHeight()));
    
    //brush sizes
//brush 1
sizeA1.push_back((int)ofRandom(3,6));
sizeA2.push_back((int)ofRandom(3,6));

}
    //color arrays
//high density colors
colorsHD[0] = ofColor(62, 101, 81);
colorsHD[1] = ofColor(33, 87, 50);
colorsHD[2] = ofColor(45, 93, 53);
colorsHD[3] = ofColor(24, 83, 51);
colorsHD[4] = ofColor(31, 60, 52);
colorsHD[5] = ofColor(26, 79, 67);
colorsHD[6] = ofColor(48, 95, 48);


//--------------------------------------------------------------
    void ofApp::draw(){
    for(int i = 0; i < 500000; i++){
    for(int j = 0; j < 7; j++){
    ofSetColor(colorsHD[j]);
    ofDrawEllipse(x[i], y[i], sizeA1[i], sizeA2[i]);
    }
}

}

Hi @NeoSchO,

You run the same coordinates - i - in your loop 7 times, until you reach the last color.
So you can remove the second loop ( j ), to only keep the i loop.
I think you can try to do something with the modulo operator : something like :

int indexColor = i % colorsHD.size();

your draw function will look something like this :

//--------------------------------------------------------------
void ofApp::draw(){
    for(int i = 0; i < 500000; i++){
       int indexColor = i % colorsHD.size();
       ofPushStyle(); // contains the style between push and pop
       ofSetColor(indexColor);
       ofDrawEllipse(x[i], y[i], sizeA1[i], sizeA2[i]);
       ofPopStyle();
    }
}

Hope this helps,

++
P

1 Like

Ahh yeah I understand what I did wrong.

I’ll have to do some research on Modulo Operation.

How would I define that differently in setup() or/and the header file?

I’m getting the error:

Member reference base type 'ofColor [7]' is not a structure or union

you can use sizeof(colorsHD); instead**, sorry*.

Your draw function will be looking like this :


//--------------------------------------------------------------
void ofApp::draw(){
    for(int i = 0; i < 500000; i++){
        int indexColor = i % sizeof(colorsHD);
       ofPushStyle(); // contains the style between push and pop
       ofSetColor(colorsHD[indexColor]);
       ofDrawEllipse(x[i], y[i], sizeA1[i], sizeA2[i]);
       ofPopStyle();
    }
}

++

Perfect! It runs! Thanks for all you help.

Very interesting thing happening when it does run though. The colours actually change over time, mostly when the mouse is moving over the canvas even though I haven’t mapped any parameters to it.

Also there are colours appearing that aren’t even on the list which are shaded of green, I am seeing red, pinks, and blues.

Any idea why this might be happening?

scho

this is my one, and the colors are not moving :
ofApp.h :

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();
		
		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y);
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
        vector<int> x;
        vector<int> y;

        vector<int> sizeA1;
        vector<int> sizeA2;

        
        ofColor colorsHD[7];
};

ofApp.cpp :

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    ofBackground(255);

    //random coordinates
    for(int i = 0; i < 1000000; i++){
        x.push_back((int)ofRandom(0,ofGetWidth()));
        y.push_back((int)ofRandom(0,ofGetHeight()));
            
            //brush sizes
        //brush 1
        sizeA1.push_back((int)ofRandom(3,6));
        sizeA2.push_back((int)ofRandom(3,6));

    }
        //color arrays
    //high density colors
    colorsHD[0] = ofColor(62, 101, 81);
    colorsHD[1] = ofColor(33, 87, 50);
    colorsHD[2] = ofColor(45, 93, 53);
    colorsHD[3] = ofColor(24, 83, 51);
    colorsHD[4] = ofColor(31, 60, 52);
    colorsHD[5] = ofColor(26, 79, 67);
    colorsHD[6] = ofColor(48, 95, 48);
    
}

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

}

//--------------------------------------------------------------
void ofApp::draw(){
    for(int i = 0; i < 500000; i++){
        int indexColor = i % sizeof(colorsHD);
       ofPushStyle(); // contains the style between push and pop
       ofSetColor(colorsHD[indexColor]);
       ofDrawEllipse(x[i], y[i], sizeA1[i], sizeA2[i]);
       ofPopStyle();
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

++

P