Searching the closest point in a double for loop.
in
Programming Questions
•
10 months ago
Hi,
I have a double "for() loop" in order to create a grid of points:
The problem I have I want to find distance between every point in a grid (between brackets I wrote float distance and if statement).
But it does not search for every point in a grid, it measures only distance between vec1 and vec2....
So the question is how to measure distance between all points in a grid, in order to draw a line if the distance is between 0 and 40? Is it possible to do that?
I want my gridpoints to have separation/flocking behavior (see the links below).
I need this because points overlap when attraction or repulsion forces are stronger than the default value.
To visualise what I want is picture link ---> SEARCHING THE CLOSEST
The picture is from Chose Sanchez tutorial ----> VIDEO
In the script there are main tab and two classes named: Electron and Vector.
I have a double "for() loop" in order to create a grid of points:
The problem I have I want to find distance between every point in a grid (between brackets I wrote float distance and if statement).
But it does not search for every point in a grid, it measures only distance between vec1 and vec2....
So the question is how to measure distance between all points in a grid, in order to draw a line if the distance is between 0 and 40? Is it possible to do that?
I want my gridpoints to have separation/flocking behavior (see the links below).
I need this because points overlap when attraction or repulsion forces are stronger than the default value.
To visualise what I want is picture link ---> SEARCHING THE CLOSEST
The picture is from Chose Sanchez tutorial ----> VIDEO
In the script there are main tab and two classes named: Electron and Vector.
- for (int y = 0; y<gridDim-1; y++) {
for (int x = 0; x<gridDim; x++) {
Vector vec1 = field.get(x+y*gridDim);
Vector vec2 = field.get(x+(y+1)*gridDim);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float distance = dist(vec1.pos.x+(vec1.dir.x)*value/multi, vec1.pos.y+(vec1.dir.y)*value/multi, vec2.pos.x+(vec2.dir.x)*value/multi, vec2.pos.y+(vec2.dir.y)*value/multi);
if (distance > 0 && distance< 40) {
stroke(1);
line(vec1.pos.x+(vec1.dir.x)*value/multi, vec1.pos.y+(vec1.dir.y)*value/multi, vec2.pos.x+(vec2.dir.x)*value/multi, vec2.pos.y+(vec2.dir.y)*value/multi);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ellipse(vec1.pos.x+(vec1.dir.x)*value/multi, vec1.pos.y+(vec1.dir.y)*value/multi, 20, 20);
}
}
whole script including the part written above:
- int gridDim = 15;
int numCharges = 3;
float maxStrengthPerCharge = 100/numCharges;
ArrayList <Vector> field = new ArrayList <Vector> ();
ArrayList <Electron> charges = new ArrayList<Electron>();
PVector speed = new PVector();
boolean fullGrid;
float value = 1;
int multi = 400;
void setup() {
size(600, 600);
smooth();
strokeWeight(0.5);
int w = width/gridDim;
int h = height/gridDim;
for (int y = 0; y<gridDim; y++) {
for (int x = 0; x<gridDim; x++) {
field.add(new Vector((x+0.5)*w, (y+0.5)*h));//ARRAY OF GRID POINTS
}
}
resetElectrons();
}
void draw() {
background(255);
for (Electron e : charges) {
e.display();
}
for (Vector v : field) {
v.update();
}
for (int y = 0; y<gridDim-1; y++) {
for (int x = 0; x<gridDim; x++) {
Vector vec1 = field.get(x+y*gridDim);
Vector vec2 = field.get(x+(y+1)*gridDim);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float distance = dist(vec1.pos.x+(vec1.dir.x)*value/multi, vec1.pos.y+(vec1.dir.y)*value/multi, vec2.pos.x+(vec2.dir.x)*value/multi, vec2.pos.y+(vec2.dir.y)*value/multi);
if (distance > 0 && distance< 40) {
stroke(1);
line(vec1.pos.x+(vec1.dir.x)*value/multi, vec1.pos.y+(vec1.dir.y)*value/multi, vec2.pos.x+(vec2.dir.x)*value/multi, vec2.pos.y+(vec2.dir.y)*value/multi);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ellipse(vec1.pos.x+(vec1.dir.x)*value/multi, vec1.pos.y+(vec1.dir.y)*value/multi, 20, 20);
}
}
move();
}
void move() {
value = 1 + value*0.98;
}
void resetElectrons() {
charges.clear();
for (int i = 0; i<numCharges; i++) {
charges.add(new Electron(400, 400, 20));
charges.add(new Electron(200, 300, 20));
charges.add(new Electron(500, 200, 20));
}
}
class Electron {
PVector pos;
float charge;
Electron(float x, float y, float charge) {
pos = new PVector (x, y);
this.charge=charge;
}
void display() {
pushMatrix();
translate(pos.x, pos.y);
noStroke();
fill(0);
ellipse(0, 0, charge, charge);
popMatrix();
}
}
class Vector {
PVector pos, dir;
Vector(float x, float y) {
pos = new PVector(x, y);
dir = new PVector();
}
void update() {
dir.set(0, 0, 0);
for (Electron e : charges) {
PVector currentDir = e.pos.get();
float magnitude = map(currentDir.dist(pos), 0, width, maxStrengthPerCharge, 0);
currentDir.sub(pos);
currentDir.normalize();
currentDir.mult(magnitude);
dir.add(currentDir);
}
}
}
2