forked from XingZhiang/point
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbspline.cpp
More file actions
77 lines (66 loc) · 1.85 KB
/
Copy pathbspline.cpp
File metadata and controls
77 lines (66 loc) · 1.85 KB
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
#include "BSpline.h"
BSpline::BSpline()
{
}
struct Point{
double x;
double y;
};
BSpline::BSpline(std::vector<QPoint> p): point(p){
table.resize(p.size() + 3 + 1); //阶数为3
for(size_t i = 0; i < table.size(); i++){
table[i] = i * 1.0 / (table.size() - 1);
}
}
void BSpline::translate(int x, int y){
QPoint qpoint(0, 0);
for(auto x : point){
qpoint += x;
}
qpoint /= point.size();
int trans_x = x - qpoint.x();
int trans_y = y - qpoint.y();
for(auto beg = point.begin();beg != point.end();++beg){
beg->setX(beg->x() + x);
beg->setY(beg->y() + y);
}
}
void BSpline::draw(QPainter &painter){
double t = table[2];
int n = point.size();
func.clear();
func.resize(n + 3);
for(auto& x : func){
x.resize(3);
}
QPen pentmp = QPen(Qt::black,2);
for(auto x : point){
Circle(x, 5, pentmp).draw(painter);
}
if(n <= 1)
return;
while(t <= table[table.size() - 4]){
//func.clear();
for(int i = 0; i < n + 3; i++){
func[i][0] = (table[i] <= t && t <= table[i + 1]) ? 1 : 0;
}
for(int j = 1; j < 3; j++){
for(int i = 0; i < n + 2 - j; i++){
func[i][j] = (t - table[i]) / (table[i + j] - table[i]) * func[i][j - 1]
+ (table[i + j + 1] - t) / (table[i + j + 1] - table[i + 1]) * func[i + 1][j - 1];
}
}
double x_sum = 0;
double y_sum = 0;
for(size_t i = 0; i < point.size(); i++){
x_sum += func[i][2] * point[i].x();
y_sum += func[i][2] * point[i].y();
}
painter.drawPoint(static_cast<int>(x_sum + 0.5), static_cast<int>(y_sum + 0.5));
t += 0.0001;
}
}
void BSpline::translate(int x, int y, int p){
point[p].setX(x);
point[p].setY(y);
}