add geometry.intersects method for all geometry types (closes #1072)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5458 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -200,3 +200,67 @@ OpenLayers.Geometry = OpenLayers.Class({
|
||||
|
||||
CLASS_NAME: "OpenLayers.Geometry"
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Method: OpenLayers.Geometry.segmentsIntersect
|
||||
* Determine whether two line segments intersect. Optionally calculates
|
||||
* and returns the intersection point. This function is optimized for
|
||||
* cases where seg1.x2 >= seg2.x1 || seg2.x2 >= seg1.x1. In those
|
||||
* obvious cases where there is no intersection, the function should
|
||||
* not be called.
|
||||
*
|
||||
* Parameters:
|
||||
* seg1 - {Object} Object representing a segment with properties x1, y1, x2,
|
||||
* and y2. The start point is represented by x1 and y1. The end point
|
||||
* is represented by x2 and y2. Start and end are ordered so that x1 < x2.
|
||||
* seg2 - {Object} Object representing a segment with properties x1, y1, x2,
|
||||
* and y2. The start point is represented by x1 and y1. The end point
|
||||
* is represented by x2 and y2. Start and end are ordered so that x1 < x2.
|
||||
* point - {Boolean} Return the intersection point. If false, the actual
|
||||
* intersection point will not be calculated. If true and the segments
|
||||
* intersect, the intersection point will be returned. If true and
|
||||
* the segments do not intersect, false will be returned. If true and
|
||||
* the segments are coincident, true will be returned.
|
||||
*
|
||||
* Returns:
|
||||
* {Boolean | <OpenLayers.Geometry.Point>} The two segments intersect.
|
||||
* If the point argument is true, the return will be the intersection
|
||||
* point or false if none exists. If point is true and the segments
|
||||
* are coincident, return will be true (and the instersection is equal
|
||||
* to the shorter segment).
|
||||
*/
|
||||
OpenLayers.Geometry.segmentsIntersect = function(seg1, seg2, point) {
|
||||
var intersection = false;
|
||||
var x11_21 = seg1.x1 - seg2.x1;
|
||||
var y11_21 = seg1.y1 - seg2.y1;
|
||||
var x12_11 = seg1.x2 - seg1.x1;
|
||||
var y12_11 = seg1.y2 - seg1.y1;
|
||||
var y22_21 = seg2.y2 - seg2.y1;
|
||||
var x22_21 = seg2.x2 - seg2.x1;
|
||||
var d = (y22_21 * x12_11) - (x22_21 * y12_11);
|
||||
var n1 = (x22_21 * y11_21) - (y22_21 * x11_21);
|
||||
var n2 = (x12_11 * y11_21) - (y12_11 * x11_21);
|
||||
if(d == 0) {
|
||||
// parallel
|
||||
if(n1 == 0 && n2 == 0) {
|
||||
// coincident
|
||||
intersection = true;
|
||||
}
|
||||
} else {
|
||||
var along1 = n1 / d;
|
||||
var along2 = n2 / d;
|
||||
if(along1 >= 0 && along1 <= 1 && along2 >=0 && along2 <= 1) {
|
||||
// intersect
|
||||
if(!point) {
|
||||
intersection = true;
|
||||
} else {
|
||||
// calculate the intersection point
|
||||
var x = seg1.x1 + (along1 * x12_11);
|
||||
var y = seg1.y1 + (along1 * y12_11);
|
||||
intersection = new OpenLayers.Geometry.Point(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
return intersection;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user