Testing segment intersection with extent
If needed, this function can be modified to take two additional coorinates that will be set to the intersection points.
This commit is contained in:
@@ -643,6 +643,59 @@ ol.extent.scaleFromCenter = function(extent, value) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the segment between two coordinates intersects (crosses,
|
||||
* touches, or is contained by) the provided extent.
|
||||
* @param {ol.Extent} extent The extent.
|
||||
* @param {ol.Coordinate} start Segment start coordinate.
|
||||
* @param {ol.Coordinate} end Segment end coordinate.
|
||||
* @return {boolean} The segment intersects the extent.
|
||||
*/
|
||||
ol.extent.segmentIntersects = function(extent, start, end) {
|
||||
var intersects = false;
|
||||
var startRel = ol.extent.coordinateRelationship(extent, start);
|
||||
var endRel = ol.extent.coordinateRelationship(extent, end);
|
||||
if (startRel === ol.extent.Relationship.INTERSECTING ||
|
||||
endRel === ol.extent.Relationship.INTERSECTING) {
|
||||
intersects = true;
|
||||
} else {
|
||||
var minX = extent[0];
|
||||
var minY = extent[1];
|
||||
var maxX = extent[2];
|
||||
var maxY = extent[3];
|
||||
var startX = start[0];
|
||||
var startY = start[1];
|
||||
var endX = end[0];
|
||||
var endY = end[1];
|
||||
var slope = (endY - startY) / (endX - startX);
|
||||
var x, y;
|
||||
if (!!(endRel & ol.extent.Relationship.ABOVE) &&
|
||||
!(startRel & ol.extent.Relationship.ABOVE)) {
|
||||
// potentially intersects top
|
||||
x = endX - ((endY - maxY) / slope);
|
||||
intersects = x >= minX && x <= maxX;
|
||||
} else if (!!(endRel & ol.extent.Relationship.RIGHT) &&
|
||||
!(startRel & ol.extent.Relationship.RIGHT)) {
|
||||
// potentially intersects right
|
||||
y = endY - ((endX - maxX) * slope);
|
||||
intersects = y >= minY && y <= maxY;
|
||||
} else if (!!(endRel & ol.extent.Relationship.BELOW) &&
|
||||
!(startRel & ol.extent.Relationship.BELOW)) {
|
||||
// potentially intersects bottom
|
||||
x = endX - ((endY - minY) / slope);
|
||||
intersects = x >= minX && x <= maxX;
|
||||
} else if (!!(endRel & ol.extent.Relationship.LEFT) &&
|
||||
!(startRel & ol.extent.Relationship.LEFT)) {
|
||||
// potentially intersects left
|
||||
y = endY - ((endX - minX) * slope);
|
||||
intersects = y >= minY && y <= maxY;
|
||||
}
|
||||
|
||||
}
|
||||
return intersects;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Extent} extent1 Extent 1.
|
||||
* @param {ol.Extent} extent2 Extent 2.
|
||||
|
||||
Reference in New Issue
Block a user