File indexing completed on 2024-12-01 04:28:05

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 #pragma once
0007 
0008 #include <QPointF>
0009 
0010 /** @class BPoint
0011  *  @brief Represents a point in a cubic Bézier spline.
0012  */
0013 class BPoint
0014 {
0015 public:
0016     enum class PointType { H1 = 0, P = 1, H2 = 2 };
0017     /** @brief Sets the point to -1, -1 to mark it as unusable (until point + handles have proper values) */
0018     BPoint();
0019     /** @brief Sets up according to the params. Linking detecting is done using autoSetLinked(). */
0020     BPoint(const QPointF &handle1, const QPointF &point, const QPointF &handle2);
0021 
0022     bool operator==(const BPoint &point) const;
0023     /** @brief Returns h1 if i = 0, p if i = 1, h2 if i = 2. */
0024     QPointF &operator[](int i);
0025     /** @brief Returns h1 if i = 0, p if i = 1, h2 if i = 2. */
0026     const QPointF &operator[](int i) const;
0027 
0028     /** @brief Sets p to @param point.
0029      * @param updateHandles (default = true) Whether to make sure the handles keep their position relative to p. */
0030     void setP(const QPointF &point, bool updateHandles = true);
0031 
0032     /** @brief Sets h1 to @param handle1.
0033      *
0034      * If handlesLinked is true h2 is updated. */
0035     void setH1(const QPointF &handle1);
0036 
0037     /** @brief Sets h2 to @param handle2.
0038      * If handlesLinked is true h1 is updated. */
0039     void setH2(const QPointF &handle2);
0040 
0041     /** @brief Sets handlesLinked to true if the handles are in a linked state (line through h1, p, h2) otherwise to false. */
0042     void autoSetLinked();
0043 
0044     /** @brief Toggles the link of the handles to @param linked*/
0045     void setHandlesLinked(bool linked);
0046 
0047     /** handle 1 */
0048     QPointF h1;
0049     /** point */
0050     QPointF p;
0051     /** handle 2 */
0052     QPointF h2;
0053     /** handles are linked to achieve a natural locking spline => PH1 = -r*PH2 ; a line can be drawn through h1, p, h2 */
0054     bool handlesLinked{true};
0055 };