Hi - Rob Penner wrote this function to get the intercept ot two lines from pasing it four points.
its very neat:
Math.intersect4Pts = function (p1, p2, p3, p4) {
var x1 = p1.x; var y1 = p1.y;
var x2 = p2.x; var y2 = p2.y;
var x3 = p3.x; var y3 = p3.y;
var x4 = p4.x; var y4 = p4.y;
var dx1 = x2 - x1;
var dx2 = x3 - x4;
var m1 = (y2 - y1) / dx1;
var m2 = (y3 - y4) / dx2;
if (!(dx1 || dx2)) return NaN;
if (!dx1) {
// infinity
return { x:x1,
y:m2 * (x1 - x4) + y4 };
} else if (!dx2) {
// infinity
return { x:x4,
y:m1 * (x4 - x1) + y1 };
}
var xInt = (-m2 * x4 + y4 + m1 * x1 - y1) / (m1 - m2);
var yInt = m1 * (xInt - x1) + y1;
return { x:xInt, y:yInt };
};
I want to add to it, so that it only alerts that there is an intercpet if the interspet of the two lines is actually on the two lines - rather than 1000 picels away...
Everytime I try an extend it - it starts getting very messy and ends up not working in some circomstances. Can someone suggest something?
M@
its very neat:
Math.intersect4Pts = function (p1, p2, p3, p4) {
var x1 = p1.x; var y1 = p1.y;
var x2 = p2.x; var y2 = p2.y;
var x3 = p3.x; var y3 = p3.y;
var x4 = p4.x; var y4 = p4.y;
var dx1 = x2 - x1;
var dx2 = x3 - x4;
var m1 = (y2 - y1) / dx1;
var m2 = (y3 - y4) / dx2;
if (!(dx1 || dx2)) return NaN;
if (!dx1) {
// infinity
return { x:x1,
y:m2 * (x1 - x4) + y4 };
} else if (!dx2) {
// infinity
return { x:x4,
y:m1 * (x4 - x1) + y1 };
}
var xInt = (-m2 * x4 + y4 + m1 * x1 - y1) / (m1 - m2);
var yInt = m1 * (xInt - x1) + y1;
return { x:xInt, y:yInt };
};
I want to add to it, so that it only alerts that there is an intercpet if the interspet of the two lines is actually on the two lines - rather than 1000 picels away...
Everytime I try an extend it - it starts getting very messy and ends up not working in some circomstances. Can someone suggest something?
M@