intersectsLine method
Checks if a line intersects with the rectangle.
@param lineStart Der Startpunkt der Linie.
@param lineEnd Der Endpunkt der Linie.
@param rectangle Das Rechteck, das auf Überschneidung geprüft werden soll.
@return true, wenn die Linie das Rechteck überschneidet oder berührt, andernfalls false.
Implementation
bool intersectsLine(ILatLong lineStart, ILatLong lineEnd) {
// 1. Überprüfe, ob einer der Linienendpunkte innerhalb des Rechtecks liegt.
if (containsLatLong(lineStart) || containsLatLong(lineEnd)) {
return true;
}
// 2. Definiere die vier Liniensegmente des Rechtecks.
ILatLong topLeft = LatLong(maxLatitude, minLongitude);
ILatLong topRight = LatLong(maxLatitude, maxLongitude);
ILatLong bottomRight = LatLong(minLatitude, maxLongitude);
ILatLong bottomLeft = LatLong(minLatitude, minLongitude);
// 3. Überprüfe, ob die Linie eines der Rechtecksegmente schneidet.
bool topIntersects = LatLongUtils.doLinesIntersect(lineStart, lineEnd, topLeft, topRight);
if (topIntersects) return true;
bool rightIntersects = LatLongUtils.doLinesIntersect(lineStart, lineEnd, topRight, bottomRight);
if (rightIntersects) return true;
bool bottomIntersects = LatLongUtils.doLinesIntersect(lineStart, lineEnd, bottomRight, bottomLeft);
if (bottomIntersects) return true;
bool leftIntersects = LatLongUtils.doLinesIntersect(lineStart, lineEnd, bottomLeft, topLeft);
if (leftIntersects) return true;
return false;
}