Comparing multiple object instances.
in
Programming Questions
•
2 years ago
Hi all.
I have a programming question about a problem that I have encountered multiple times regarding using variables specific to an object's instance, outside of the object.
What is the best way of extracting the location of multiple object instances, and then comparing them to determine factors such as their distance from one another?
I have written a very simple example to illustrate my question:
I have an array of objects. Each object simply consists of a circle of random size moving linearly across the screen. I would like to extract the x and y position from each instance of the object and then compare all of the object's locations. My objective is to display connections between ellipses that are close together, or perhaps not display the objects themselves, but only display ellipses when objects touch.
I appreciate that I may be thinking about this in the wrong way, so any help is much appreciated.
Thank you in advance.
Code:
- Circle[] circles;
- void setup() {
- size(1000, 600);
- background(255);
- smooth();
- circles = new Circle[1000];
- for(int i = 0; i < circles.length; i++) {
- circles[i] = new Circle(random(width), random(height), random(-1, 1), random(-1, 1), random(30));
- }
- }
- void draw() {
- background(255);
- for(int i = 0; i < circles.length; i++) {
- circles[i].update();
- circles[i].display();
- circles[i].borders();
- }
- }
- class Circle {
- float x, y, randXdir, randYdir, randSize;
- Circle(float _x, float _y, float _randXdir, float _randYdir, float _randSize) {
- x = _x;
- y = _y;
- randXdir = _randXdir;
- randYdir = _randYdir;
- randSize = _randSize;
- }
- void update() {
- x += randXdir;
- y += randYdir;
- }
- void display() {
- stroke(255);
- fill(0);
- ellipse(x, y, randSize, randSize);
- }
- void borders() {
- if(x > width) { x = 0; }
- if(x < 0) { x = width; }
- if(y > height) { y = 0; }
- if(y < 0) { y = height; }
- }
- }
1