Multiple random straight lines

Hi,
iam pretty new to coding and in this project i want to draw multiple random straight lines when i press the mouse. I did it somehow, but i wonder if there is a way to only once describe the line once, and then do it as many times as i want wihtout having to wright it ?

Thanks

void ofApp::draw(){
    ofSetColor(0);
	ofDrawBitmapString(lp, 2, 100);
	ofDrawBitmapString(rad, 2, 120);

	myLine.draw();
	myLine2.draw();
	myLine3.draw();
	myLine4.draw();
	myLine5.draw();
	myLine6.draw();
	myLine7.draw();
	myLine8.draw();
	myLine9.draw();
	myLine10.draw();
	myLine11.draw();
	myLine12.draw();
	
	ofDrawCircle(myLine.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine2.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine3.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine4.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine5.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine6.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine7.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine8.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine9.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine10.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine11.getPointAtPercent(lp), rad * 50);
	ofDrawCircle(myLine12.getPointAtPercent(lp), rad * 50);

}

//--------------------------------------------------------------
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){
	lp = 0;
	rad = 0;
	ofSetLineWidth(5);
	myLine.clear();
	myLine2.clear();
	myLine3.clear();
	myLine4.clear();
	myLine5.clear();
	myLine6.clear();
	myLine7.clear();
	myLine8.clear();
	myLine9.clear();
	myLine10.clear();
	myLine11.clear();
	myLine12.clear();

//iknow this does not makes any sense
	for (int i = 0; i < 1; i++) {
		myLine.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine2.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine2.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine3.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine3.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine4.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine4.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine5.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine5.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine6.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine6.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine7.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine7.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine8.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine8.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine9.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine9.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine10.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine10.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine11.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine11.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine12.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		myLine12.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
	}
	myLine.draw();
	myLine2.draw();
	myLine3.draw();
	myLine4.draw();
	myLine5.draw();
	myLine6.draw();
	myLine7.draw();
	myLine8.draw();
	myLine9.draw();
	myLine10.draw();
	myLine11.draw();
	myLine12.draw();

Hi, of course. That is what coding is all about.

Instead of storing individual line objects (i am guessing these are ofPolylines) store all of these in a vector (an array, an indexed collection)
So in your ofApp.h declare

vector<ofPolyline> lines;
void ofApp::setup(){
   // make sure you resize your lines collection on setup.
    lines.resize(12);
// you can actually resize it at any point during run time without needing to change any of the rest of the code. it will just work.
}
//-----------------------
void ofApp::draw(){
    ofSetColor(0);
	ofDrawBitmapString(lp, 2, 100);
	ofDrawBitmapString(rad, 2, 120);
// Draw lines firts
          ofSetLineWidth(5);
      // this is an easier way to iterate over collections. Useful when you dont need the index
       for(auto& line: lines){
             line.draw();
        }       
// Then draw circles
       for(auto& line: lines){
        	ofDrawCircle(line.getPointAtPercent(lp), rad * 50);
        }
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
	lp = 0;
	rad = 0;
       for(auto& line: lines){
                line.clear();
		line.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
		line.addVertex(ofRandom(ofGetHeight() / 2), ofRandom(ofGetWidth() / 2));
	}
1 Like