Actually if anyone knows how to test line segment intersection and is patient enough to explain why it works that would probably be better for me.
EDIT: I found a nice explanation:
Finding m (slope) and b (y axis intercept) was fairly straightforward but getting the x intercept doesn't seem to be working correctly for some reason (this is finding line intercept so far, not segments). The explanation says that the x intercept can be found by x = (b2-b1)/(m1-m2) but when I test this by drawing a line such as line(x, 0, x, height) the line is not at the intercept. I'm not sure if this is because Processing's coordinate system is different from the typical eight grade algebra class coordinate system. Here is my code so far:
int numLines = 2;
float[] x1 = new float[numLines];
float[] x2 = new float[numLines];
float[] y1 = new float[numLines];
float[] y2 = new float[numLines];
void setup() {
size(800, 600);
smooth();
noFill();
placeLines();
segmentIntersect();
}
void draw() {
}
void mousePressed() {
placeLines();
segmentIntersect();
}
void placeLines() {
for (int i = 0; i < numLines; i++) {
x1[i] = random(width);
x2[i] = random(width);
y1[i] = random(height);
y2[i] = random(height);
}
}
void segmentIntersect() {
background(255);
// Lines are defined as y=mx+b
for (int i = 0; i < numLines; i++) {
float mi = (y2[i]-y1[i])/(x2[i]-x1[i]);
float bi = y1[i]-mi*x1[i];
for (int j = i+1; j < numLines; j++) {
float mj = (y2[j]-y1[j])/(x2[j]-x1[j]);
float bj = y1[j]-mi*x1[j];
if (mi != mj) {
float x = (bj-bi)/(mi-mj);
println(x);
line(x, 0, x, height);
//println("Not parallel");
}
else if (mi == mj && bi == bj) {
println("Colinear");
}
}
line(x1[i], y1[i], x2[i], y2[i]);
}
}