Loop creating circles


#1

Hi, I’ve got the below code

if (byteData > 50) {
for (int i = 1; i <= 20; i++) {
ofCircle(+60, +60, 100);
}
}

However it’s currently just making one big circle rather than 20 different circles(which is what I want). I also want the circles to be scattered around the screen and after that intend to make them float around(almost like a particle effect), so if someone can point me in that direction that’d be great! For now though, I’m trying to figure out why it’s not creating multiple circles.


#2

This code is doing exactly what you are telling, drawing 20 times a circle in the same position. To draw them in different positions, you have to change it, for example with:

if (byteData > 50) {
    for (int i = 1; i <= 20; i++) {
        ofCircle(ofRandom(100, 800), (100, 800), 100);
    }
}

#3

Hi,

Thank you, funnily enough I figured this out straight after I posted this! However, I did have one other question which is fairly related. I’ve got my circles being created at random parts of the screen with the OfRandom function however they are being produced very quickly. Do you know what I’d need to do to slow them down and make them slowly fade in and out?


#4

Create a vector that contains your 20 objects that draw a circle. If you are not familiar with OOP, read this first

in the update method, call a method that decrease the alpha value of the color that you want to use to draw the circle (drawing a circle with alpha value equal to 0, will draw a circle completely transparent).

In the draw method, iterate through the vector and for each object you call the draw method. This will draw the circle. Circles with low alpha values will be transparent. Playing with alpha values allow you to create fade in and fade out effect.


#5

Hi @edapx thank you so much for your help. I’m still struggling a little bit, could you possibly post or point me to any code that may help with creating the vector and changing the alpha value? I’m also still unsure how to make the circles actually slow down as currently they’re very fast.


#6

Hello @HarryLee186, can you post here the code of your Circle class?


#7

@edapx Absolutely! It may not be too good, so please give me any feedback you can! :slight_smile:

if (newRate > 50) {
	for (int i = 1; i <= 5; i++) {
		ofCircle(ofRandom(-900, 900), ofRandom(-600, 600), 50);
	}
} else if (newRate > 70) {
	for (int i = 1; i <= 10; i++) {
		ofCircle(ofRandom(-900, 900), ofRandom(-600, 600), 60);
	}
}
else if (newRate > 90) {
	for (int i = 1; i <= 15; i++) {
		ofCircle(ofRandom(-900, 900), ofRandom(-600, 600), 70);
	}
}
else if (newRate > 110) {
	for (int i = 1; i <= 20; i++) {
		ofCircle(ofRandom(-900, 900), ofRandom(-600, 600), 80);
	}
}
else if (newRate > 130) {
	for (int i = 1; i <= 25; i++) {
		ofCircle(ofRandom(-900, 900), ofRandom(-600, 600), 90);
	}
}

#8

I think you did not read the chapter suggested http://openframeworks.cc/ofBook/chapters/OOPs!.html

In that chapter there is a class called Ball, that draws a circle. This object is really simillar to what you need. Try to implement a vector containing 20 object with your class, and post the code here.