File indexing completed on 2024-12-22 04:16:10
0001 /* 0002 * SPDX-FileCopyrightText: 2008-2010 Lukáš Tvrdý <lukast.dev@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "trajectory.h" 0008 #include <cmath> 0009 0010 #include <kis_debug.h> 0011 0012 Trajectory::Trajectory() 0013 { 0014 m_i = 0; 0015 m_size = 0; 0016 } 0017 0018 Trajectory::~Trajectory() 0019 { 0020 } 0021 0022 0023 void Trajectory::addPoint(QPointF pos) 0024 { 0025 if (m_i >= m_path.size()) { 0026 m_path.append(pos); 0027 m_i++; 0028 } else { 0029 m_path[m_i] = pos; 0030 m_i++; 0031 } 0032 0033 m_size++; 0034 } 0035 0036 0037 void Trajectory::reset() 0038 { 0039 m_size = 0; 0040 m_i = 0; 0041 } 0042 0043 0044 const QVector<QPointF> &Trajectory::getLinearTrajectory(const QPointF &start, const QPointF &end, double space) 0045 { 0046 Q_UNUSED(space); 0047 reset(); 0048 0049 // Width and height of the line 0050 qreal xd = (end.x() - start.x()); 0051 qreal yd = (end.y() - start.y()); 0052 0053 int x = (int)start.x(); 0054 int y = (int)start.y(); 0055 qreal fx = start.x(); 0056 qreal fy = start.y(); 0057 qreal m = yd / xd; 0058 0059 int y2 = (int)end.y(); 0060 int x2 = (int)end.x(); 0061 0062 addPoint(start); 0063 0064 if (fabs(m) > 1) { 0065 // y - directional axis 0066 int incr; 0067 if (yd > 0) { 0068 m = 1.0f / m; 0069 incr = 1; 0070 } else { 0071 m = -1.0f / m; 0072 incr = -1; 0073 } 0074 while (y != y2) { 0075 fx = fx + m; 0076 fy = fy + incr; 0077 y += incr; 0078 // x = (int)(fx + 0.5f); 0079 addPoint(QPointF(fx, fy)); 0080 } 0081 } else { 0082 // x - directional axis 0083 int incr; 0084 if (xd > 0) { 0085 incr = 1; 0086 } else { 0087 incr = -1; 0088 m = -m; 0089 } 0090 while (x != x2) { 0091 fy = fy + m; 0092 fx = fx + incr; 0093 x += incr; 0094 // y = (int)(fy + 0.5f); 0095 addPoint(QPointF(fx, fy)); 0096 } 0097 } 0098 0099 addPoint(end); 0100 return m_path; 0101 } 0102 0103 QVector<QPointF> Trajectory::getDDATrajectory(QPointF start, QPointF end, double space) 0104 { 0105 Q_UNUSED(space); 0106 reset(); 0107 // Width and height of the line 0108 int xd = (int)(end.x() - start.x()); 0109 int yd = (int)(end.y() - start.y()); 0110 0111 int x = (int)start.x(); 0112 int y = (int)start.y(); 0113 float fx = start.x(); 0114 float fy = start.y(); 0115 float m = (float)yd / (float)xd; 0116 int y2 = (int)end.y(); 0117 int x2 = (int)end.x(); 0118 0119 if (fabs(m) > 1) { 0120 int incr; 0121 if (yd > 0) { 0122 m = 1.0f / m; 0123 incr = 1; 0124 } 0125 else { 0126 m = -1.0f / m; 0127 incr = -1; 0128 } 0129 while (y != y2) { 0130 fx = fx + m; 0131 y = y + incr; 0132 x = (int)(fx + 0.5f); 0133 addPoint(QPointF(x, y)); 0134 } 0135 } else { 0136 int incr; 0137 if (xd > 0) { 0138 incr = 1; 0139 } 0140 else { 0141 incr = -1; 0142 m = -m; 0143 } 0144 while (x != x2) { 0145 fy = fy + m; 0146 x = x + incr; 0147 y = (int)(fy + 0.5f); 0148 addPoint(QPointF(x, y)); 0149 } 0150 } 0151 0152 return m_path; 0153 } 0154 0155