OK, here's the solution I came up with. I built two classes. The first is the RandomPoint class. This class not only creates the random points and draws them in the center, but it also utilizes the findTarget() function, which calculates which point on the circle is the closest to its location.
The second class is called the CirclePoint class. The only thing this class does is calculate the locations of each point on the circle. It then draws each point at that location.
I find this code to be much more readable than using a bunch of multidimensional arrays. For one, we can now just use one for() loop to access the class members. Before we had to use two or three just to access all the dimensions of the array. We can now also name our members things like "x" and "y" and we can attach them to specific points. So each CirclePoint has its own x and y, instead of having to store them all in one 3D array.
There is one problem you are going to run into. Which CirclePoints the RandomPoints connect to all depends on which RandomPoint you start with when calculating distances. As you iterate through the RandomPoints, they will start eliminating available CirclePoints, which means that one of the last RandomPoints may have to connect to a CirclePoint across the screen, simply because it was last and didn't get a chance to even have a shot at getting assigned a closer CirclePoint. I'm not sure if that will be a problem for your particular project. Anyway, here is the code. Let me know if you have any questions. I didn't really comment anything, so feel free to question anything that's in there.
Code:
ArrayList circle_points;
ArrayList random_points;
int AMOUNT = 10;
void setup(){
size(500, 500);
smooth();
ellipseMode(CENTER);
noLoop();
//initialize our CirclePoints
circle_points = new ArrayList(AMOUNT);
for(int i=0; i<AMOUNT; i++){
float degree = radians((360/AMOUNT)*i);
circle_points.add(new CirclePoint(degree));
}
//initialize our RandomPoints
random_points = new ArrayList(AMOUNT);
for(int i=0; i<AMOUNT; i++){
random_points.add(new RandomPoint());
}
}
void draw(){
background(255);
translate(width/2, height/2);
for(int i=0; i<AMOUNT; i++){
//Access a RandomPoint and find the closest CirclePoint
RandomPoint rp = (RandomPoint)random_points.get(i);
rp.findTarget(circle_points);
rp.drawPoint();
//simply draw the CirclePoints
CirclePoint cp = (CirclePoint)circle_points.get(i);
cp.drawCircle();
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
class CirclePoint{
float x, y;
float target_x, target_y;
float degree;
boolean is_not_occupied;
CirclePoint(float _degree){
degree = _degree;
x = cos(degree)*200;
y = sin(degree)*200;
target_x = 0;
target_y = 0;
is_not_occupied = true; //is this point available?
}
void drawCircle(){
noStroke();
fill(255,0,0);
ellipse(x, y, 7, 7);
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
class RandomPoint{
float x, y;
float target_x, target_y;
RandomPoint(){
x = random(-50, 50);
y = random(-50, 50);
}
void drawPoint(){
noStroke();
fill(0);
ellipse(x, y, 5, 5);
stroke(0);
line(x, y, target_x, target_y);
}
void findTarget(ArrayList _cps){
ArrayList cps = _cps;
float shortest_distance = 1000;
int index_of_shortest = 0;
for(int i=0; i<cps.size(); i++){
CirclePoint cp = (CirclePoint)cps.get(i);
if(cp.is_not_occupied){
if(i == 0){
shortest_distance = dist(x, y, cp.x, cp.y);
index_of_shortest = i;
}
float current_distance = dist(x, y, cp.x, cp.y);
if(current_distance < shortest_distance){
shortest_distance = current_distance;
index_of_shortest = i;
}
}
}
CirclePoint shortest_point = (CirclePoint)cps.get(index_of_shortest);
target_x = shortest_point.x;
target_y = shortest_point.y;
shortest_point.is_not_occupied = false;
}
}