MCPcopy Create free account
hub / github.com/OpenOrienteering/mapper / lineIntersectsRect

Function lineIntersectsRect

src/util/util.cpp:115–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

113}
114
115bool lineIntersectsRect(const QRectF& rect, const QPointF& p1, const QPointF& p2)
116{
117 if (rect.contains(p1) || rect.contains(p2))
118 return true;
119
120 // Left side
121 if ((p1.x() > rect.left()) != (p2.x() > rect.left()))
122 {
123 if ((p2.x() == p1.x()) && !((p1.y() < rect.top() && p2.y() < rect.top()) || (p1.y() > rect.bottom() && p2.y() > rect.bottom())))
124 return true;
125 qreal intersection_y = p1.y() + (p2.y() - p1.y()) * (rect.left() - p1.x()) / (p2.x() - p1.x());
126 if (intersection_y >= rect.top() && intersection_y <= rect.bottom())
127 return true;
128 }
129 // Right side
130 if ((p1.x() > rect.right()) != (p2.x() > rect.right()))
131 {
132 if ((p2.x() == p1.x()) && !((p1.y() < rect.top() && p2.y() < rect.top()) || (p1.y() > rect.bottom() && p2.y() > rect.bottom())))
133 return true;
134 qreal intersection_y = p1.y() + (p2.y() - p1.y()) * (rect.right() - p1.x()) / (p2.x() - p1.x());
135 if (intersection_y >= rect.top() && intersection_y <= rect.bottom())
136 return true;
137 }
138
139 // Top side
140 if ((p1.y() > rect.top()) != (p2.y() > rect.top()))
141 {
142 if ((p2.y() == p1.y()) && !((p1.x() < rect.left() && p2.x() < rect.left()) || (p1.x() > rect.right() && p2.x() > rect.right())))
143 return true;
144 qreal intersection_x = p1.x() + (p2.x() - p1.x()) * (rect.top() - p1.y()) / (p2.y() - p1.y());
145 if (intersection_x >= rect.left() && intersection_x <= rect.right())
146 return true;
147 }
148 // Bottom side
149 if ((p1.y() > rect.bottom()) != (p2.y() > rect.bottom()))
150 {
151 if ((p2.y() == p1.y()) && !((p1.x() < rect.left() && p2.x() < rect.left()) || (p1.x() > rect.right() && p2.x() > rect.right())))
152 return true;
153 qreal intersection_x = p1.x() + (p2.x() - p1.x()) * (rect.bottom() - p1.y()) / (p2.y() - p1.y());
154 if (intersection_x >= rect.left() && intersection_x <= rect.right())
155 return true;
156 }
157
158 return false;
159}
160
161double parameterOfPointOnLine(double x0, double y0, double dx, double dy, double x, double y, bool& ok)
162{

Callers 1

intersectsBoxMethod · 0.85

Calls 3

xMethod · 0.80
yMethod · 0.80
containsMethod · 0.45

Tested by

no test coverage detected