Line intersection, failing to grasp a key concept?
in
Programming Questions
•
3 years ago
Hey guys, I'm trying to write what I thought would be a fairly simple program.
I'd like to spawn a number of nodes and give them a random direction to travel in (incrementing x and y speed each draw cycle). If the node touches the line traveled by any of the other nodes, or it touches the boundary, it should stop.
This code is very slow. I feel like I'm doing it inefficient/wrong by loading the pixels of the Processing window and checking against that to see if a line is present (notice the shoddy rounded float to integer conversion). My knowledge of programming isn't deep enough to think of a better solution though.
Should I do something like load all the lines into an array and check each of the nodes against it using math on each cycle instead?
I'd like to spawn a number of nodes and give them a random direction to travel in (incrementing x and y speed each draw cycle). If the node touches the line traveled by any of the other nodes, or it touches the boundary, it should stop.
This code is very slow. I feel like I'm doing it inefficient/wrong by loading the pixels of the Processing window and checking against that to see if a line is present (notice the shoddy rounded float to integer conversion). My knowledge of programming isn't deep enough to think of a better solution though.
Should I do something like load all the lines into an array and check each of the nodes against it using math on each cycle instead?
- int w = 960;
int h = 540;
int boundary = 50;
int number_of_walkers = 50;
Walker[] walk;
void setup() {
size(w, h);
smooth();
background(255);
drawBoundary();
walk = new Walker[number_of_walkers];
for(int i = 0; i < number_of_walkers; i++) {
walk[i] = new Walker(i);
}
}
void draw() {
for(int i = 0; i < number_of_walkers; i++) {
walk[i].update();
walk[i].plot();
}
}
void drawBoundary() {
line(boundary, boundary, width - boundary, boundary);
line(width - boundary, boundary, width - boundary, height - boundary);
line(width - boundary, height - boundary, boundary, height - boundary);
line(boundary, height - boundary, boundary, boundary);
}
class Walker {
int id;
float x, y;
boolean alive = true;
float x_birth = int(random(boundary, width - boundary));
float y_birth = int(random(boundary, height - boundary));
float x_speed = random(-2.0, 2.0);
float y_speed = random(-2.0, 2.0);
float x_archive, y_archive;
float x_death, y_death;
float x_midpoint, y_midpoint;
float x_slope, y_slope;
Walker(int id_) {
id = id_;
x = x_birth;
y = y_birth;
birth();
}
void birth() {
fill(255);
// ellipse(x, y, 3, 3);
}
void update() {
if(alive == true) {
x_archive = x;
y_archive = y;
x += x_speed;
y += y_speed;
if(x < boundary || y < boundary || x > width - boundary || y > height - boundary) {
alive = false;
fill(255, 0, 0);
// ellipse(x, y, 3, 3);
}
loadPixels();
if(pixels[int(ceil(x)) + int(ceil(y)) * width] != color(255)) {
alive = false;
fill(0, 0, 255);
ellipse(x, y, 5, 5);
}
updatePixels();
}
}
void plot() {
if(alive == true) {
line(x_archive, y_archive, x, y);
}
}
}
1
