PlanimetryCalculus.cc 25.3 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1
#include "PlanimetryCalculus.h"
2 3 4 5 6 7
#include "Circle.h"


namespace PlanimetryCalculus {
    namespace  {
        /*!
Valentin Platzgummer's avatar
Valentin Platzgummer committed
8
            \fn IntersectType intersects(const Circle &circle, const QLineF &line, PointList &intersectionPoints, bool calcInstersect)
9 10 11 12 13 14
            Returns the Intersection type of \a circle and \a line.
            Stores the intersection points in \a intersectionPoints if \a calcIntersect is \c true.
            Returns \c Error if either line or circe \c {isNull() == true}.

            \sa QPointF, Circle
        */
15
        bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints, IntersectType &type, bool calcInstersect)
16 17 18
        {
            if (!circle.isNull() && ! line.isNull()) {
                QPointF translationVector = line.p1();
19
                double alpha = angle(line); // angle between wold and line coordinate system
20 21

                QPointF originCircleL = circle.origin() - translationVector;
22
                rotateReference(originCircleL, -alpha); // circle origin in line corrdinate system
23 24 25

                double y = originCircleL.y();
                double r = circle.radius();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
26 27 28 29
                if (qAbs(y) > r) {
                    type = NoIntersection;
                    return false;
                }
30 31 32 33 34 35
                else if ( qFuzzyCompare(qFabs(y), r) ) { // tangent
                    double x_ori = originCircleL.x();

                    if (x_ori >= 0 && x_ori <= line.length()) {
                        if (calcInstersect) {
                            QPointF intersectionPt = QPointF(x_ori, 0);
36
                            rotateReference(intersectionPt, alpha);
37 38 39
                            intersectionPoints.append(intersectionPt + translationVector);
                        }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
40 41
                        type = Tangent;
                        return true;
42 43
                    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
44 45 46
                    type = NoIntersection;
                    return false;
                } else { // secant
47 48 49 50 51 52 53 54 55 56
                    double x_ori = originCircleL.x();
                    double y_ori = originCircleL.y();
                    double delta = qSqrt(qPow(r, 2)-qPow(y_ori, 2));
                    double x1    = x_ori + delta; // x coordinate (line system) of fist intersection point
                    double x2    = x_ori - delta;// x coordinate (line system) of second intersection point
                    bool doesIntersect = false; // remember if actual intersection was on the line

                    if (x1 >= 0 && x1 <= line.length()) { // check if intersection point is on the line
                        if (calcInstersect) {
                            QPointF intersectionPt = QPointF(x1, 0); // first intersection point (line system)
57
                            rotateReference(intersectionPt, alpha);
58 59 60 61 62 63 64
                            intersectionPoints.append(intersectionPt + translationVector); // transform (to world system) and append first intersection point
                        }
                        doesIntersect = true;
                    }
                    if (x2 >= 0 && x2 <= line.length()) { // check if intersection point is on the line
                        if (calcInstersect) {
                            QPointF intersectionPt = QPointF(x2, 0); // second intersection point (line system)
65
                            rotateReference(intersectionPt, alpha);
66 67 68 69
                            intersectionPoints.append(intersectionPt + translationVector); // transform (to world system) and append second intersection point
                        }
                        doesIntersect = true;
                    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
70 71
                    type = doesIntersect ? Secant : NoIntersection;
                    return doesIntersect ? true : false;
72 73
                }
            }
74

Valentin Platzgummer's avatar
Valentin Platzgummer committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            type = Error;
            return false;
        }

        /*!
            \fn  bool intersects(const Circle &circle1, const Circle &circle2, PointList &intersectionPoints, IntersectType type)
            Calculates the intersection points of two circles if present and stores the result in \a intersectionPoints.
            Returns the intersection type of the two cirles \a circle1 and \a circle2.

            The function assumes that the list \a intersectionPoints is empty.

            \note Returns Error if circle.isNull() returns true;

            \sa Circle
        */
        bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints, IntersectType type, bool calcIntersection)
        {
            if (!circle1.isNull() && !circle2.isNull()) {
                double r1 = circle1.radius();
                double r2 = circle2.radius();
                double d  = distance(circle1.origin(), circle2.origin());
                double r = 0;
                double R = 0;
                if (r1 > r2) {
                    R = r1; // large
100
                    r = r2; // smallline1
Valentin Platzgummer's avatar
Valentin Platzgummer committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                } else {
                    // this branch is also choosen if r1 == r2
                    R = r2;
                    r = r1;
                }

                // determine intersection type
                if        (r + d < R) {
                    // this branch is also reached if d < rLarge && rSmall == 0
                    type = InsideNoIntersection;
                    return false;
                } else if (qFuzzyCompare(r + d, R)) {
                    if    (qFuzzyIsNull(d))
                    {
                        type = CirclesEqual;
                        return true;
                    }
                    else {
                        type = InsideTouching;
                    }
                } else if (d < R) {
                    type = InsideIntersection;
                } else if (d - r < R) {
                    type = OutsideIntersection;
                } else if (qFuzzyCompare(d - r, R)) {
                    type = OutsideTouching;
                } else {
                    type = OutsideNoIntersection;
                    return false;
                }


                if (calcIntersection) {
                    // calculate intersection

                    // Coordinate system circle1: origin = circle1.origin(), x-axis towars circle2.origin() y-axis such that the
                    // coordinate system is dextrorse with z-axis outward faceing with respect to the drawing plane.
                    double alpha = angle(circle1.origin(), circle2.origin()); // angle between world and circle1 system
                    QPointF translationVector = circle1.origin(); // translation vector between world and circle1 system

                    if        (   type == InsideTouching
                               || type == OutsideTouching) {
                        // Intersection point in coordinate system of circle 1.
                        // Coordinate system circle1: origin = circle1.origin(), x-axis towars circle2.origin() y-axis such that the
                        // coordinate system is dextrorse with z-axis outward faceing with respect to the drawing plane.
                        intersectionPoints.append(rotateReturn(QPointF(0, r1), alpha)+translationVector);
                    } else { //triggered if (   type == InsideIntersection
                             //              || type == OutsideIntersection)
                        // See fist branch for explanation

                        // this equations are obtained by solving x^2+y^2=R^2 and (x - d)^2+y^2=r^2
                        double x  = (qPow(d, 2) - qPow(r, 2) + qPow(R, 2))/2/d;
                        double y = 1/2/d*qSqrt(4*qPow(d*R, 2) - qPow(qPow(d, 2) - qPow(r, 2) +  qPow(R, 2), 2));

                        intersectionPoints.append(rotateReturn(QPointF(x, y), alpha)+translationVector);
                        intersectionPoints.append(rotateReturn(QPointF(x, -y), alpha)+translationVector);
                    }
                    // Transform the coordinate to the world coordinate system. Alpha is the angle between world and circle1 coordinate system.

                        return true;
                }
            }
            type = Error;
            return false;
165 166 167 168 169 170 171
        }
    } // end anonymous namespace

    /*!
        \fn void rotatePoint(QPointF &point, double alpha)
        Rotates the \a point counter clockwisely by the angle \a alpha (in radiants).
    */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
172
    void rotateReference(QPointF &point, double alpha)
173 174 175 176 177 178 179 180
    {
        if (!point.isNull()) {
            double x = point.x();
            double y = point.y();

            point.setX(x*qCos(alpha) - y*qSin(alpha));
            point.setY(x*qSin(alpha) + y*qCos(alpha));
        }
181 182
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
183
    void rotateReference(QPointFList &points, double alpha)
184 185
    {
        for (int i = 0; i < points.size(); i++) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
186
            rotateReference(points[i], alpha);
187
        }
188
    }
189

190 191 192 193
    /*!
        \fn void rotatePointDegree(QPointF &point, double alpha)
        Rotates the \a point counter clockwisely by the angle \a alpha (in degrees).
    */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
194
    void rotateReferenceDegree(QPointF &point, double alpha)
195
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
196
        rotateReference(point, alpha/180*M_PI);
197
    }
198

Valentin Platzgummer's avatar
Valentin Platzgummer committed
199
    void rotateReferenceDegree(QPointFList &points, double alpha)
200 201
    {
        for (int i = 0; i < points.size(); i++) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
202
            rotateReferenceDegree(points[i], alpha);
203
        }
204
    }
205

206 207 208 209
    /*!
        \fn IntersectType intersects(const Circle &circle, const QLineF &line)
        Returns the Intersection type of \a circle and \a line.
        Returns \c Error if either line or circe \c {isNull() == true}.
210

211 212
        \sa QPointF, Circle
    */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
213
    bool intersects(const Circle &circle, const QLineF &line)
214
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
215 216 217
        QPointFList dummyList;
        IntersectType type = NoIntersection;
        return intersects(circle, line, dummyList, type, false /* calculate intersection points*/);
218
    }
219

Valentin Platzgummer's avatar
Valentin Platzgummer committed
220
    bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints)
221
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
222 223
        IntersectType type = NoIntersection;
        return intersects(circle, line, intersectionPoints, type, true /* calculate intersection points*/);
224
    }
225

226 227 228 229 230 231 232 233 234 235
    /*!
        \fn double distance(const QPointF &p1, const QPointF p2)
        Calculates the distance (2-norm) between \a p1 and \a p2.
        \sa QPointF
    */
    double distance(const QPointF &p1, const QPointF p2)
    {
        double dx = p2.x()-p1.x();
        double dy = p2.y()-p1.y();

236
        return norm(dx, dy);
237
    }
238

239 240 241 242
    /*!
        \fn double distance(const QPointF &p1, const QPointF p2)
        Calculates the angle (in radiants) between the line defined by \a p1 and \a p2 and the x-axis according to the following rule.
        Angle = qAtan2(dy, dx), where dx = p2.x()-p1.x() and dy = p2.y()-p1.y().
243

244 245 246 247 248 249 250
        \note The order of \a p1 and \a p2 matters. Swapping \a p1 and \a p2 will result in a angle of oposite sign.
        \sa QPointF
    */
    double angle(const QPointF &p1, const QPointF p2)
    {
        double dx = p2.x()-p1.x();
        double dy = p2.y()-p1.y();
251

252 253
        return qAtan2(dy, dx);
    }
254

255 256 257 258
    /*!
        \fn double distance(const QPointF &p1, const QPointF p2)
        Calculates the angle (in degrees) between the line defined by \a p1 and \a p2 and the x-axis according to the following rule.
        Angle = qAtan2(dy, dx)*180/pi, where dx = p2.x()-p1.x() and dy = p2.y()-p1.y().
259
angle
260 261 262 263 264 265 266
        \note The order of \a p1 and \a p2 matters. Swapping \a p1 and \a p2 will result in a angle of oposite sign.
        \sa QPointF
    */
    double angleDegree(const QPointF &p1, const QPointF p2)
    {
        return angle(p1, p2)*180/M_PI;
    }
267

268 269 270 271
    double truncateAngle(double angle)
    {
        while (angle < 0     ) { angle += 2*M_PI;}
        while (angle > 2*M_PI) { angle -= 2*M_PI;}
272

273 274
        return angle;
    }
275

276 277 278 279
    double truncateAngleDegree(double angle)
    {
        return truncateAngle(angle/180*M_PI);
    }
280 281


282
    /*!
Valentin Platzgummer's avatar
Valentin Platzgummer committed
283
     * \fn IntersectType intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt)
284 285
     * Determines wheter \a line1 and \a line2 intersect and of which type the intersection is.
     * Stores the intersection point in \a intersectionPt
Valentin Platzgummer's avatar
Valentin Platzgummer committed
286 287 288 289 290 291 292 293 294 295 296
     * Returns the intersection type (\c IntersectType).
     *
     * Intersection Types:
     * \c NoIntersection
     * \c CornerCornerIntersection; A intersection is present such that two of the lines cornes touch.
     * \c EdgeCornerIntersection; A intersection is present such that a corner and a edge touch.
     * \c EdgeEdgeIntersection; A intersection is present such two edges intersect.
     * \c LinesParallel
     * \c LinesEqual
     * \c Error; Returned if at least on line delivers isNULL() == true.
     *
297
     *
Valentin Platzgummer's avatar
Valentin Platzgummer committed
298
     * \sa QPointF
299
     */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
300
    bool intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt, IntersectType &type)
301
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
302 303 304 305 306
        if (line1.isNull() || line2.isNull()){
            type = Error;
            return false;
        }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
307 308 309

        // line 1 coordinate system: origin line1.p1(), x-axis towards line1.p2()
        QPointF translationVector = line1.p1(); // translation vector between world and line1 system
310
        double alpha = angle(line1);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
311 312
        double l1 = line1.length();

313
        QLineF line2L1 = line2;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
314 315 316 317 318 319 320
        line2L1.translate(-translationVector);
        rotateReference(line2L1, -alpha);

        double x1 = line2L1.x1();
        double x2 = line2L1.x2();
        double y1 = line2L1.y1();
        double y2 = line2L1.y2();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
321 322 323 324 325 326 327
        if (x1 > x2) {
            x1 = x2;
            x2 = line2L1.x1();
            y1 = y2;
            y2 = line2L1.y1();
        }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
328 329 330 331 332 333 334 335
        double dx = (x2-x1);
        double dy = (y2-y1);
        double xNull = 0; // (xNull, 0) intersection point in line1 system

        if (!qFuzzyIsNull(dx)) {
            double k = dy/dx;

            if (qFuzzyIsNull(k)) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
336 337 338 339 340 341 342 343
                if (qFuzzyIsNull(x1) && qFuzzyIsNull(y1) && qFuzzyCompare(x2, l1)){
                    type = LinesEqual;
                    return true;
                }
                else {
                    type = LinesParallel;
                    return false;
                }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
344 345 346 347 348 349 350 351 352
            }

            double d = (y1*x2-y2*x1)/dx;
            xNull = -d/k;
        } else {
            // lines orthogonal
            if (signum(y1) != signum(y2)){
                xNull = x1;
            }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
353 354 355 356
            else {
                type = NoIntersection;
                return false;
            }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
357 358 359
        }

        if (xNull >= x1 && xNull <= x2){
360
            // determine intersection type#include "QVector3D"
361

Valentin Platzgummer's avatar
Valentin Platzgummer committed
362 363
            if(qFuzzyIsNull(xNull) || qFuzzyCompare(xNull, l1)) {
                if(qFuzzyIsNull(y1) || qFuzzyIsNull(y2))
Valentin Platzgummer's avatar
Valentin Platzgummer committed
364
                    type = CornerCornerIntersection;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
365
                else
Valentin Platzgummer's avatar
Valentin Platzgummer committed
366
                    type = EdgeCornerIntersection;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
367 368
            } else if (xNull > 0 && xNull < l1) {
                if(qFuzzyIsNull(y1) || qFuzzyIsNull(y2))
Valentin Platzgummer's avatar
Valentin Platzgummer committed
369
                    type = EdgeCornerIntersection;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
370
                else
Valentin Platzgummer's avatar
Valentin Platzgummer committed
371 372 373 374 375 376 377 378 379
                    type = EdgeEdgeIntersection;
            } else {
                type = NoIntersection;
                return false;
            }
        } else {
            type = NoIntersection;
            return false;
        }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
380 381 382 383 384

        intersectionPt = QPointF(xNull, 0); // intersection point in line1 system
        rotateReference(intersectionPt, alpha);
        intersectionPt += translationVector;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
385
        return true;
386 387
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
388 389
    /*!IntersectType type = NoIntersection;
     * \overload bool intersects(const QPolygonF &polygon, const QLineF &line, PointList &intersectionList,  NeighbourList &neighbourList, IntersectList &typeList)
390 391 392 393 394 395 396 397 398 399 400 401 402
     * Checks if \a polygon intersect with \a line.
     * Stores the intersection points in \a intersectionList.
     *
     * Stores the indices of the closest two \a area vetices for each of coorespoinding intersection points in \a neighbourList. *
     * For example if an intersection point is found between the first and the second vertex of the \a area the intersection point will
     * be stored in \a intersectionList and the indices 1 and 2 will be stored in \a neighbourList.
     * \a neighbourList has entries of type \c {QPair<int, int>}, where \c{pair.first} would contain 1 and \c{pair.second} would contain 2, when
     * relating to the above example.
     *
     * Returns the \c IntersectionType of each intersection point within a QList.
     *
     * \sa QPair, QList
     */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
403
    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList,  NeighbourList &neighbourList, IntersectList &typeList)
404
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
405 406 407

        if (polygon.size() >= 2) {
            IntersectList intersectionTypeList;
408 409 410
            // Assemble a line form each tow consecutive polygon vertices and check whether it intersects with line
            for (int i = 0; i < polygon.size(); i++) {

Valentin Platzgummer's avatar
Valentin Platzgummer committed
411 412 413 414 415
                QLineF interatorLine;
                QPointF currentVertex    = polygon[i];
                QPointF nextVertex       = polygon[PolygonCalculus::nextVertexIndex(polygon.size(), i)];
                interatorLine.setP1(currentVertex);
                interatorLine.setP2(nextVertex);
416

Valentin Platzgummer's avatar
Valentin Platzgummer committed
417 418 419
                QPointF intersectionPoint;
                IntersectType type;
                if ( intersects(line, interatorLine, intersectionPoint, type) ) {
420 421 422 423
                    intersectionList.append(intersectionPoint);

                    QPair<int, int>     neighbours;
                    neighbours.first    = i;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
424
                    neighbours.second   = PolygonCalculus::nextVertexIndex(polygon.size(), i);
425 426
                    neighbourList.append(neighbours);

Valentin Platzgummer's avatar
Valentin Platzgummer committed
427
                    typeList.append(type);
428 429 430
                }
            }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
431 432
            if (typeList.size() > 0) {
                return true;
433
            } else {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
434
                return false;
435 436
            }
        } else {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
437
            return false;
438 439 440 441
        }
    }

    /*!
Valentin Platzgummer's avatar
Valentin Platzgummer committed
442
     * \overload IntersectType intersects(const QPolygonF &polygon, const QLineF &line)
443 444 445 446
     * Returns \c true if any intersection between \a polygon and \a line exists, \c false else.
     *
     * \sa QPair, QList
     */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
447
    bool intersects(const QPolygonF &polygon, const QLineF &line)
448
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
449
        QPointFList dummyGeo;
450 451 452 453 454 455 456 457 458
        QList<QPair<int, int>> dummyNeighbour;
        intersects(polygon, line, dummyGeo, dummyNeighbour);

        if (dummyGeo.size() > 0)
            return true;

        return false;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
459 460 461 462 463 464 465 466 467 468 469 470
    void rotateReference(QLineF &line, double alpha)
    {
        line.setP1(rotateReturn(line.p1(), alpha));
        line.setP2(rotateReturn(line.p2(), alpha));
    }

    QPointF rotateReturn(QPointF point, double alpha)
    {
        rotateReference(point, alpha);
        return point;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
471
    QPointFList rotateReturn(QPointFList points, double alpha)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
472 473 474 475 476 477 478 479 480 481 482
    {
        rotateReference(points, alpha);
        return points;
    }

    QLineF rotateReturn(QLineF line, double alpha)
    {
        rotateReference(line, alpha);
        return line;
    }

483

Valentin Platzgummer's avatar
Valentin Platzgummer committed
484

Valentin Platzgummer's avatar
Valentin Platzgummer committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    bool intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt)
    {
        IntersectType dummyType;
        return intersects(line1, line2, intersectionPt, dummyType);
    }

    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList, NeighbourList &neighbourList)
    {
        IntersectList typeList;
        return intersects(polygon, line, intersectionList, neighbourList, typeList);
    }

    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList, IntersectList &typeList)
    {
        NeighbourList neighbourList;
        return intersects(polygon, line, intersectionList, neighbourList, typeList);
    }

    bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList)
    {
        NeighbourList neighbourList;
        IntersectList typeList;
        return intersects(polygon, line, intersectionList, neighbourList, typeList);
    }

    /*!
        \fn IntersectType intersects(const Circle &circle1, const Circle &circle2)
        Returns the intersection type of the two cirles \a circle1 and \a circle2.

        \note Returns Error if circle.isNull() returns true;

        \sa Circle
    */
    bool intersects(const Circle &circle1, const Circle &circle2)
    {
        IntersectType type = NoIntersection;
        QPointFList intersectionPoints;
        return intersects(circle1, circle2, intersectionPoints, type, false /*calculate intersection points*/);
    }

    bool intersects(const Circle &circle1, const Circle &circle2, IntersectType &type)
    {
        QPointFList intersectionPoints;
        return intersects(circle1, circle2, intersectionPoints, type, false /*calculate intersection points*/);
    }

    bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints)
    {
        IntersectType type;
        return intersects(circle1, circle2, intersectionPoints, type);
    }

    bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints, IntersectType &type)
    {
        return intersects(circle1, circle2, intersectionPoints, type, true /*calculate intersection points*/);
    }

542 543 544 545 546 547
    ;

    double angle(const QLineF &line)
    {
        return angle(line.p1(), line.p2());
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
548

Valentin Platzgummer's avatar
Valentin Platzgummer committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    bool contains(const QLineF &line, const QPointF &point, IntersectType &type)
    {
        QPointF translationVector = line.p1();
        double alpha = angle(line);
        double l = line.length();

        QPointF pointL1 = rotateReturn(point - translationVector, -alpha);

        double x = pointL1.x();
        double y = pointL1.y();

        if ( x >= 0 && x <= l ) {
            if (qFuzzyIsNull(x) || qFuzzyCompare(x, l)) {
                if (qFuzzyIsNull(y))
                {
                    type = CornerCornerIntersection;
                    return true;
                }
            } else {
                if (qFuzzyIsNull(y))
                {
                    type = EdgeCornerIntersection;
                    return true;
                }
            }
        }
        type = NoIntersection;
        return false;

    }

    bool contains(const QLineF &line, const QPointF &point)
    {
        IntersectType dummyType;
        return contains(line, point, dummyType);
    }

    bool contains(const QPolygonF &polygon, const QPointF &point, IntersectType &type)
    {
        using namespace PolygonCalculus;
        if (polygon.containsPoint(point, Qt::FillRule::OddEvenFill))
        {
            type = Interior;
            return true;
        }
        int size = polygon.size();
        for (int i = 0; i < size; i++) {
            QLineF line(polygon[i], polygon[nextVertexIndex(size, i)]);
            if ( contains(line, point, type) ) {
                return true;
            }
        }

        return false;
    }

    bool contains(const QPolygonF &polygon, const QPointF &point)
    {
        IntersectType dummyType;
        return contains(polygon, point, dummyType);
    }

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    double norm(double x, double y)
    {
        return qSqrt(x*x+y*y);
    }

    double norm(const QPointF &p)
    {
        return norm(p.x(), p.y());
    }

    double angle(const QPointF &p1)
    {
        return qAtan2(p1.y(), p1.x());
    }

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints, NeighbourList &neighbourList, IntersectList &typeList)
    {
        using namespace PolygonCalculus;
        for (int i = 0; i < polygon.size(); i++) {
            QPointF p1 = polygon[i];
            int j = nextVertexIndex(polygon.size(), i);
            QPointF p2 = polygon[j];
            QLineF line(p1, p2);

            QPointFList lineIntersecPts;
            IntersectType type;
            if (intersects(circle, line, lineIntersecPts, type)) {
                QPair<int, int> neigbours;
                neigbours.first = i;
                neigbours.second = j;
                neighbourList.append(neigbours);
                typeList.append(type);
                intersectionPoints.append(lineIntersecPts);
            }

        }

        if (intersectionPoints.size() > 0)
            return true;
        else {
            return false;
        }
    }

    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints, NeighbourList &neighbourList)
    {
        QList<IntersectType> types;
        return intersects(circle, polygon, intersectionPoints, neighbourList, types);
    }

    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints, IntersectList &typeList)
    {
        NeighbourList neighbourList;
        return intersects(circle, polygon, intersectionPoints, neighbourList, typeList);
    }

    bool intersects(const Circle &circle, const QPolygonF &polygon, QList<QPointFList> &intersectionPoints)
    {
        NeighbourList neighbourList;
        return intersects(circle, polygon, intersectionPoints, neighbourList);
    }

    bool intersects(const Circle &circle, const QPolygonF &polygon)
    {
        QList<QPointFList> intersectionPoints;
        return intersects(circle, polygon, intersectionPoints);
    }

    bool intersects(const QLineF &line1, const QLineF &line2)
    {
        QPointF intersectionPoint;
        return intersects(line1, line2, intersectionPoint);
    }

    bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints, IntersectType &type)
    {
        return intersects(circle, line, intersectionPoints, type, true /* calculate intersection points*/);
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
690 691 692 693 694 695 696 697 698 699 700











701
} // end namespace PlanimetryCalculus
702 703 704



705 706 707 708 709 710
/*!
    \class PlanimetryCalculus
    \inmodule Wima

    \brief The \c PlanimetryCalculus class provides routines handy for planimetrical (2D) calculations.
*/