constantly resizing ellipse based on distance
in
Programming Questions
•
2 years ago
I am trying to write a code that draws an ellipse wherever I click on the screen. This is not a problem. Then I want to add some new functionality to that code.
1. Loop through all pts - if distance is less than x, then connect with line.
2. Loop through all pts - draw ellipse at pts based on closest pt (meaning if new pt inserted rescale already drawn ellipses).
Now, I believe that the issue with function 2 is that I don't know how to make it not measure on itself (making shortest distance = 0.0) ... any ideas? I think it might be the structure that isn't really working.
Thanks in advance,
/Claus
1. Loop through all pts - if distance is less than x, then connect with line.
2. Loop through all pts - draw ellipse at pts based on closest pt (meaning if new pt inserted rescale already drawn ellipses).
Now, I believe that the issue with function 2 is that I don't know how to make it not measure on itself (making shortest distance = 0.0) ... any ideas? I think it might be the structure that isn't really working.
Thanks in advance,
/Claus
- float[] mPosX = new float[0]; // declare array for x pos
float[] mPosY = new float[0]; // declare array for y pos
float[] shortest = new float[0]; // declare array for shortest distance
int n = 0;
void setup() {
// general setup
size(600, 400);
background(255);
smooth();
fill(0, 100, 200, 20);
stroke(0);
strokeWeight(0.25);
ellipseMode(CENTER);
}
void draw() {
if (n > 0) { // making sure the loop will run only when second pt has been clicked
for (int i = 0; i < n; i++) {
float[] distance = new float[n]; // loop through each pt
for (int j = 0; j < n-1; j++) { // for each pt, loop through all other pts
distance[j] = dist(mPosX[i], mPosY[i], mPosX[j], mPosY[j]);
if (distance[j] < 100) {
line(mPosX[i], mPosY[i], mPosX[j], mPosY[j]);
}
}
distance = sort(distance);
shortest = append(shortest, distance[0]);
ellipse(mPosX[i], mPosY[i], shortest[i], shortest[i]);
}
}
}
void mousePressed() {
// write coordinates to array
mPosX = append(mPosX, mouseX);
mPosY = append(mPosY, mouseY);
//println("x" + n + ": " + mPosX[n]);
//println("y" + n + ": " + mPosY[n]);
// redim counter
n = n+1;
}
1