File indexing completed on 2024-04-21 04:51:03

0001 /*
0002     SPDX-FileCopyrightText: 2011 Till Theato <root@ttill.de>
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "bpoint.h"
0007 
0008 #include <QLineF>
0009 
0010 BPoint::BPoint()
0011     : h1(-1, -1)
0012     , p(-1, -1)
0013     , h2(-1, -1)
0014 
0015 {
0016 }
0017 
0018 BPoint::BPoint(const QPointF &handle1, const QPointF &point, const QPointF &handle2)
0019     : h1(handle1)
0020     , p(point)
0021     , h2(handle2)
0022 {
0023     autoSetLinked();
0024 }
0025 
0026 QPointF &BPoint::operator[](int i)
0027 {
0028     return i == 0 ? h1 : (i == 1 ? p : h2);
0029 }
0030 
0031 const QPointF &BPoint::operator[](int i) const
0032 {
0033     return i == 0 ? h1 : (i == 1 ? p : h2);
0034 }
0035 
0036 bool BPoint::operator==(const BPoint &point) const
0037 {
0038     return point.h1 == h1 && point.p == p && point.h2 == h2;
0039 }
0040 
0041 void BPoint::setP(const QPointF &point, bool updateHandles)
0042 {
0043     QPointF offset = point - p;
0044     p = point;
0045     if (updateHandles) {
0046         h1 += offset;
0047         h2 += offset;
0048     }
0049 }
0050 
0051 void BPoint::setH1(const QPointF &handle1)
0052 {
0053     h1 = handle1;
0054     if (handlesLinked) {
0055         qreal angle = QLineF(h1, p).angle();
0056         QLineF l = QLineF(p, h2);
0057         l.setAngle(angle);
0058         h2 = l.p2();
0059     }
0060 }
0061 
0062 void BPoint::setH2(const QPointF &handle2)
0063 {
0064     h2 = handle2;
0065     if (handlesLinked) {
0066         qreal angle = QLineF(h2, p).angle();
0067         QLineF l = QLineF(p, h1);
0068         l.setAngle(angle);
0069         h1 = l.p2();
0070     }
0071 }
0072 
0073 void BPoint::autoSetLinked()
0074 {
0075     // sometimes the angle is returned as 360°
0076     // due to rounding problems the angle is sometimes not quite 0
0077     qreal angle = QLineF(h1, p).angleTo(QLineF(p, h2));
0078     handlesLinked = angle < 1e-3 || qRound(angle) == 360;
0079 }
0080 
0081 void BPoint::setHandlesLinked(bool linked)
0082 {
0083     handlesLinked = linked;
0084     if (linked) {
0085         // we force recomputing one of the handles
0086         setH1(h1);
0087     }
0088 }