Skip to content

Instantly share code, notes, and snippets.

@mutoo
Created June 12, 2013 14:52
Show Gist options
  • Select an option

  • Save mutoo/5765988 to your computer and use it in GitHub Desktop.

Select an option

Save mutoo/5765988 to your computer and use it in GitHub Desktop.
a simple demo to testing the formula: http://en.wikipedia.org/wiki/Line-line_intersection
float[] x = {
88, 431, 431, 124
};
float[] y = {
115, 367, 188, 331
};
void setup() {
size(640, 480);
}
void draw() {
background(200);
line(x[0], y[0], x[1], y[1]);
line(x[2], y[2], x[3], y[3]);
float X,Y,delta;
delta = (x[0]-x[1])*(y[2]-y[3])-(y[0]-y[1])*(x[2]-x[3]);
float x1y2y1x2 = (x[0]*y[1]-y[0]*x[1]);
float x3y4y3x4 = (x[2]*y[3]-y[2]*x[3]);
X = (x1y2y1x2*(x[2]-x[3])-(x[0]-x[1])*x3y4y3x4)/delta;
Y = (x1y2y1x2*(y[2]-y[3])-(y[0]-y[1])*x3y4y3x4)/delta;
ellipse(X,Y,10,10); // intersection
x[3] = mouseX;
y[3] = mouseY;
}
void mousePressed() {
println(mouseX+" "+mouseY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment