src/basics.cpp

changeset 1181
ca6d0ef9aadb
parent 1177
8661b9237ed5
child 1305
31627acdd4b5
equal deleted inserted replaced
1180:2005e4147ad6 1181:ca6d0ef9aadb
278 lines.append(QLineF {QPointF {x0, z0}, QPointF {x1, z1}}); 278 lines.append(QLineF {QPointF {x0, z0}, QPointF {x1, z1}});
279 } 279 }
280 280
281 return lines; 281 return lines;
282 } 282 }
283
284 /*
285 * Computes the shortest distance from a point to a rectangle.
286 *
287 * The code originates from the Unity3D wiki, and was translated from C# to Qt by me (Teemu Piippo):
288 *
289 * Original code:
290 * http://wiki.unity3d.com/index.php/Distance_from_a_point_to_a_rectangle
291 *
292 * Copyright 2013 Philip Peterson.
293 *
294 * Permission is hereby granted, free of charge, to any person obtaining a copy
295 * of this software and associated documentation files (the "Software"), to
296 * deal in the Software without restriction, including without limitation the
297 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
298 * sell copies of the Software, and to permit persons to whom the Software is
299 * furnished to do so, subject to the following conditions:
300 *
301 * The above copyright notice and this permission notice shall be included in
302 * all copies or substantial portions of the Software.
303 *
304 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
305 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
306 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
307 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
308 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
309 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
310 * IN THE SOFTWARE.
311 */
312 qreal distanceFromPointToRectangle(const QPointF& point, const QRectF& rectangle)
313 {
314 // Calculate a distance between a point and a rectangle.
315 // The area around/in the rectangle is defined in terms of
316 // several regions:
317 //
318 // O--x
319 // |
320 // y
321 //
322 //
323 // I | II | III
324 // ======+==========+====== --yMin
325 // VIII | IX (in) | IV
326 // ======+==========+====== --yMax
327 // VII | VI | V
328 //
329 //
330 // Note that the +y direction is down because of Unity's GUI coordinates.
331
332 if (point.x() < rectangle.left())
333 {
334 // Region I, VIII, or VII
335 if (point.y() < rectangle.top()) // I
336 return QLineF {point, rectangle.topLeft()}.length();
337 else if (point.y() > rectangle.bottom()) // VII
338 return QLineF {point, rectangle.bottomLeft()}.length();
339 else // VIII
340 return rectangle.left() - point.x();
341 }
342 else if (point.x() > rectangle.right())
343 {
344 // Region III, IV, or V
345 if (point.y() < rectangle.top()) // III
346 return QLineF {point, rectangle.topRight()}.length();
347 else if (point.y() > rectangle.bottom()) // V
348 return QLineF {point, rectangle.bottomRight()}.length();
349 else // IV
350 return point.x() - rectangle.right();
351 }
352 else
353 {
354 // Region II, IX, or VI
355 if (point.y() < rectangle.top()) // II
356 return rectangle.top() - point.y();
357 else if (point.y() > rectangle.bottom()) // VI
358 return point.y() - rectangle.bottom();
359 else // IX
360 return 0;
361 }
362 }

mercurial