This repository has been archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubicSpline.cpp
139 lines (124 loc) · 3.55 KB
/
CubicSpline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
100
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
#include "CubicSpline.h"
CubicSpline::CubicSpline(QWidget *parent)
: QMainWindow(parent)
{
this->points.append(qMakePair(0.1, 0.5));
this->points.append(qMakePair(0.2, 0.1));
this->points.append(qMakePair(0.3, 0.6));
this->points.append(qMakePair(0.35, 0.2));
this->points.append(qMakePair(0.4, 0.7));
this->points.append(qMakePair(0.6, 0.5));
this->points.append(qMakePair(0.7, 0.8));
this->points.append(qMakePair(0.8, 0.2));
this->points.append(qMakePair(0.9, 0.4));
this->spline();
}
CubicSpline::~CubicSpline()
{
if (this->cubic != nullptr) {
delete this->cubic;
this->cubic = nullptr;
}
if (this->x != nullptr) {
delete[] this->x;
this->x = nullptr;
}
if (this->y != nullptr) {
delete[] this->y;
this->y = nullptr;
}
if (this->a != nullptr) {
delete[] this->a;
this->a = nullptr;
}
if (this->b != nullptr) {
delete[] this->b;
this->b = nullptr;
}
if (this->c != nullptr) {
delete[] this->c;
this->c = nullptr;
}
if (this->d != nullptr) {
delete[] this->d;
this->d = nullptr;
}
}
void CubicSpline::spline()
{
if (this->cubic != nullptr) {
delete this->cubic;
this->cubic = nullptr;
}
if (this->x != nullptr) {
delete[] this->x;
this->x = nullptr;
}
if (this->y != nullptr) {
delete[] this->y;
this->y = nullptr;
}
if (this->a != nullptr) {
delete[] this->a;
this->a = nullptr;
}
if (this->b != nullptr) {
delete[] this->b;
this->b = nullptr;
}
if (this->c != nullptr) {
delete[] this->c;
this->c = nullptr;
}
if (this->d != nullptr) {
delete[] this->d;
this->d = nullptr;
}
if (this->points.size() < 2) {
return;
}
this->x = new double[this->points.size()];
this->y = new double[this->points.size()];
for (int i = 0; i < this->points.size(); i++) {
memcpy(&(this->x[i]), &(this->points.at(i).first), sizeof(double));
memcpy(&(this->y[i]), &(this->points.at(i).second), sizeof(double));
}
this->cubic = new CubicS(this->x, this->y, this->points.size());
this->a = new double[this->points.size() - 1];
this->b = new double[this->points.size() - 1];
this->c = new double[this->points.size() - 1];
this->d = new double[this->points.size() - 1];
this->cubic->caculate(this->a, this->b, this->c, this->d);
}
void CubicSpline::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
if (this->cubic == nullptr) {
return;
}
QPainter painter(this);
QPen pen;
pen.setColor(Qt::GlobalColor::blue);
pen.setStyle(Qt::PenStyle::SolidLine);
pen.setJoinStyle(Qt::PenJoinStyle::RoundJoin);
pen.setCapStyle(Qt::PenCapStyle::RoundCap);
pen.setWidth(2);
painter.setPen(pen);
QPolygon poly;
for (int i = 0; i < this->width(); i++) {
QPoint point(i, this->height() * this->cubic->result(i / (double)this->width()));
poly.append(point);
}
painter.drawPoints(poly);
pen.setColor(Qt::GlobalColor::black);
pen.setWidth(1);
painter.setPen(pen);
QBrush brush;
brush.setStyle(Qt::BrushStyle::SolidPattern);
brush.setColor(Qt::GlobalColor::red);
painter.setBrush(brush);
for (int i = 0; i < this->points.size(); i++) {
QPoint point(this->width() * this->points.at(i).first, this->height() * this->points.at(i).second);
painter.drawEllipse(point, 4, 4);
}
}