Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find intersect 1

Status
Not open for further replies.

gameon

Programmer
Feb 23, 2001
325
GB
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@
 
Here's one way - check for the minimum and maximum values of the initial points and if the intersection lies outside these bounds don't return a value.

Code:
getMin = function (val1, val2, val3, val4) {
	vals = [val1, val2, val3, val4];
	temp = vals[0];
	for (var j = 1; j<vals.length; j++) {
		temp = Math.min(temp, vals[j]);
	}
	return temp;
};
getMax = function (val1, val2, val3, val4) {
	vals = [val1, val2, val3, val4];
	temp = vals[0];
	for (var j = 1; j<vals.length; j++) {
		temp = Math.max(temp, vals[j]);
	}
	return temp;
};
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 xMin = getMin(x1, x2, x3, x4);
	var xMax = getMax(x1, x2, x3, x4);
	var yMin = getMin(y1, y2, y3, y4);
	var yMax = getMax(y1, y2, y3, y4);
	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;
	if ((xInt<=xMax && xInt>=xMin) && (yInt<=yMax && yInt>=yMin)) {
		return {x:xInt, y:yInt};
	}
	else {
		return {x:NaN, y:NaN};
	}
};
p1 = {x:5, y:100};
p2 = {x:50, y:120};
p3 = {x:30, y:10};
p4 = {x:100, y:10};
obj = Math.intersect4Pts(p1, p2, p3, p4);
trace(obj.x+&quot; - &quot;+obj.y);
//
stop();
 
cheers, loads mate - will work my way through it...
M@)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top