Hi!
I always had the issue with objects. I want to replicate
PVector a = new PVector(x,y,z)
with the class Circles. So something like
ArrayList<Circles> circle = new ArrayList<Circles>()
circle.add(new Circles( x, y, id) );
int n = 0;
ArrayList<Circles> circle = new ArrayList<Circles>();
void setup() {
size(600,600);
}
void draw() {
}
void addNew() {
//circle.add(new Circles(random(width),random(height),(float)0,circle.size());
}
class Circles {
float x, y, r;
int id;
boolean canGrow = true;
void Circles(float x_, float y_, float r_, int id_) {
x = x_;
y = y_;
r = r_;
id = id_;
}
void grow() {
if(canGrow) {
for(int i = 0; i < circle.size(); i++) {
float dst = dist(x,y,circle.get(i).x,circle.get(i).y);
if(dst != 0 && dst < r + circle.get(i).r) {
canGrow = false;
circle.get(i).canGrow = false;
} else {
r++;
}
}
}
}
void display() {
noFill();
stroke(255);
circle(x,y,r*2);
}
}
I know there is a way to do so, but I forgot how : P
micycle
December 29, 2020, 5:54pm
2
Simply remove void
from void Circles
to turn it from a method to a constructor.
Circles(float x_, float y_, float r_, int id_) {
x = x_;
y = y_;
r = r_;
id = id_;
}
Consider renaming the class to Circle
and the array that contains them to circles
.
Also, including trailing underscores for the constructor arguments (to denote internal variables) isn’t something that’s usually done in Java, since we declare variable scope explicitly!
2 Likes
Chrisir
December 29, 2020, 6:02pm
3
Very good answer
tutorial: https://www.processing.org/tutorials/objects/
int n = 0;
ArrayList<Circle> circles = new ArrayList<Circle>();
void setup() {
size(600, 600);
addNew();
addNew();
addNew();
addNew();
}//func
void draw() {
for (Circle currentCircle : circles) {
currentCircle.grow();
currentCircle.display();
}//for
}//func
// -----------------------------------------------------------------------------------------------
void addNew() {
circles.add(new Circle(random(30, width-30), random(30, height-30),
(float)0,
circles.size() ));
}
// ==============================================================================================
class Circle {
float x, y, r;
int id;
boolean canGrow = true;
Circle(float x_, float y_,
float r_,
int id_) {
x = x_;
y = y_;
r = r_;
id = id_;
}
void grow() {
if (canGrow) {
for (int i = 0; i < circles.size(); i++) {
float dst = dist(x, y, circles.get(i).x, circles.get(i).y);
if (dst != 0 && dst < r + circles.get(i).r) {
canGrow = false;
circles.get(i).canGrow = false;
} else {
r++;
}
}
}
}
void display() {
noFill();
stroke(255);
ellipse(x, y,
r*2, r*2);
}
//
}//class
//