Krita Source Code Documentation
Loading...
Searching...
No Matches
trajectory.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2008-2010 Lukáš Tvrdý <lukast.dev@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "trajectory.h"
8#include <cmath>
9
10#include <kis_debug.h>
11
13{
14 m_i = 0;
15 m_size = 0;
16}
17
21
22
23void Trajectory::addPoint(QPointF pos)
24{
25 if (m_i >= m_path.size()) {
26 m_path.append(pos);
27 m_i++;
28 } else {
29 m_path[m_i] = pos;
30 m_i++;
31 }
32
33 m_size++;
34}
35
36
38{
39 m_size = 0;
40 m_i = 0;
41}
42
43
44const QVector<QPointF> &Trajectory::getLinearTrajectory(const QPointF &start, const QPointF &end, double space)
45{
46 Q_UNUSED(space);
47 reset();
48
49 // Width and height of the line
50 qreal xd = (end.x() - start.x());
51 qreal yd = (end.y() - start.y());
52
53 int x = (int)start.x();
54 int y = (int)start.y();
55 qreal fx = start.x();
56 qreal fy = start.y();
57 qreal m = yd / xd;
58
59 int y2 = (int)end.y();
60 int x2 = (int)end.x();
61
62 addPoint(start);
63
64 if (fabs(m) > 1) {
65 // y - directional axis
66 int incr;
67 if (yd > 0) {
68 m = 1.0f / m;
69 incr = 1;
70 } else {
71 m = -1.0f / m;
72 incr = -1;
73 }
74 while (y != y2) {
75 fx = fx + m;
76 fy = fy + incr;
77 y += incr;
78// x = (int)(fx + 0.5f);
79 addPoint(QPointF(fx, fy));
80 }
81 } else {
82 // x - directional axis
83 int incr;
84 if (xd > 0) {
85 incr = 1;
86 } else {
87 incr = -1;
88 m = -m;
89 }
90 while (x != x2) {
91 fy = fy + m;
92 fx = fx + incr;
93 x += incr;
94// y = (int)(fy + 0.5f);
95 addPoint(QPointF(fx, fy));
96 }
97 }
98
99 addPoint(end);
100 return m_path;
101}
102
103QVector<QPointF> Trajectory::getDDATrajectory(QPointF start, QPointF end, double space)
104{
105 Q_UNUSED(space);
106 reset();
107 // Width and height of the line
108 int xd = (int)(end.x() - start.x());
109 int yd = (int)(end.y() - start.y());
110
111 int x = (int)start.x();
112 int y = (int)start.y();
113 float fx = start.x();
114 float fy = start.y();
115 float m = (float)yd / (float)xd;
116 int y2 = (int)end.y();
117 int x2 = (int)end.x();
118
119 if (fabs(m) > 1) {
120 int incr;
121 if (yd > 0) {
122 m = 1.0f / m;
123 incr = 1;
124 }
125 else {
126 m = -1.0f / m;
127 incr = -1;
128 }
129 while (y != y2) {
130 fx = fx + m;
131 y = y + incr;
132 x = (int)(fx + 0.5f);
133 addPoint(QPointF(x, y));
134 }
135 } else {
136 int incr;
137 if (xd > 0) {
138 incr = 1;
139 }
140 else {
141 incr = -1;
142 m = -m;
143 }
144 while (x != x2) {
145 fy = fy + m;
146 x = x + incr;
147 y = (int)(fy + 0.5f);
148 addPoint(QPointF(x, y));
149 }
150 }
151
152 return m_path;
153}
154
155
const QVector< QPointF > & getLinearTrajectory(const QPointF &start, const QPointF &end, double space)
QVector< QPointF > m_path
Definition trajectory.h:28
void reset()
void addPoint(QPointF pos)
QVector< QPointF > getDDATrajectory(QPointF start, QPointF end, double space)