forked from XingZhiang/point
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurve.cpp
More file actions
62 lines (54 loc) · 1.66 KB
/
Copy pathCurve.cpp
File metadata and controls
62 lines (54 loc) · 1.66 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
#include "Curve.h"
#include <iostream>
struct Point{
double x;
double y;
};
Curve::Curve(std::vector<QPoint> p):control_point(p) {};
void Curve::draw(QPainter &painter){
double t = 0;
QPen pentmp = QPen(Qt::black,2);
int n = control_point.size();
for(auto x : control_point){
Circle(x, 5, pentmp).draw(painter);
}
if(n <= 1)
return;
std::vector<Point> DeCasteljau;
DeCasteljau.reserve(n);
while(t <= 1){
for(int i = 0; i < n; i++){
DeCasteljau[i].x = control_point[i].x();
DeCasteljau[i].y = control_point[i].y();
}
for(int k = 0; k < n - 1; k++){
for(int i = 0; i < n - 1 - k; i++){
DeCasteljau[i].x = (1 - t) * DeCasteljau[i].x + t * DeCasteljau[i + 1].x;
DeCasteljau[i].y = (1 - t) * DeCasteljau[i].y + t * DeCasteljau[i + 1].y;
}
}
painter.drawPoint(static_cast<int>(DeCasteljau[0].x + 0.5), static_cast<int>(DeCasteljau[0].y + 0.5));
t += 0.0001;
}
/*for(int i = 0; i < control_point.size() - 1; i++){
Line line(control_point[i], control_point[i + 1]);
line.drawByBresenham(painter);
}*/
}
void Curve::translate(int x, int y, int p){
control_point[p].setX(x);
control_point[p].setY(y);
}
void Curve::translate(int x, int y){
QPoint qpoint(0, 0);
for(auto x : control_point){
qpoint += x;
}
qpoint /= control_point.size();
int trans_x = x - qpoint.x();
int trans_y = y - qpoint.y();
for(auto beg = control_point.begin();beg != control_point.end();++beg){
beg->setX(beg->x() + x);
beg->setY(beg->y() + y);
}
}