Less repetition and code?


#1

Hi there! I’m pretty new to OF (2 weeks in) and still just getting the hang of things. I was practicing by creating a pattern of the letter W and have it rotate from the center to mouseX. The thing is, I found myself copying and pasting this over and over.

ofPushMatrix();
ofTranslate(50,50);
ofRotate(mouseX);
drawW(-40,-40,20,80);
ofPopMatrix();

ofPushMatrix();
ofTranslate(150,50);
ofRotate(mouseX);
drawW(-40,-40,20,80);
ofPopMatrix();

ofPushMatrix();
ofTranslate(250,50);
ofRotate(mouseX);
drawW(-40,-40,20,80);
ofPopMatrix();

etc…

There’s gotta be a way to generate the pattern so my Draw function isn’t 100’s of lines long right? Perhaps something with for loop? Do you guys have any recommendations?

thanks


#2

it sounds like you are one my students :slight_smile: for these kinds of questions feel free to email the mailing list (ie, homework related questions)

you could make a function that does this:

void drawWwithRotation( float x, float y, float rotation){
   ofPushMatrix();
   ofTranslate(x,y);
   ofRotate(rotation);
   drawW(-40,-40,20,80);
   ofPopMatrix();
}

then your code in draw would look like:

drawWwithRotation(50,50, mouseX);
drawWwithRotation(150, 50, mouseX);
drawWwithRotation(250,50, mouseX);

#3

then if you felt like this was a pattern:

drawWwithRotation(50,50, mouseX);
drawWwithRotation(150, 50, mouseX);
drawWwithRotation(250,50, mouseX);

you could do:

for (int i = 0; i < 3; i++){
   drawWwithRotation(50 + 100*i,50, mouseX);
}

#4

Awesome! Thanks Zach. Yes I’m in your class now. Will email the mailing list so they can benefit from the answers.