Created
June 12, 2013 14:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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