-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClip.cpp
More file actions
executable file
·105 lines (87 loc) · 2.66 KB
/
Copy pathClip.cpp
File metadata and controls
executable file
·105 lines (87 loc) · 2.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
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
#include "Clip.h"
#include <iostream>
Edge::Edge(GPoint p0, GPoint p1, int wind){
if(p0.y() > p1.y()){
std::swap(p0, p1);
wind = -wind;
}
topY = GRoundToInt(p0.y());
bottomY = GRoundToInt(p1.y());
slope = (p1.x() - p0.x()) / (p1.y() - p0.y());
curX = p0.x() + slope * ((float)topY - p0.y() + 0.5f);
this -> wind = wind;
}
bool Edge::operator<(const Edge& other) const {
if (this->topY == other.topY) {
if (this->curX == other.curX) {
return this->slope < other.slope;
} else {
return this->curX < other.curX;
}
}
return this->topY < other.topY;
}
bool checkHorizontal(const GPoint& pa, const GPoint& pb) {
return GRoundToInt(pa.fY) == GRoundToInt(pb.fY);
}
void clipLine(GPoint p0, GPoint p1, GRect bounds, std::vector<Edge> &edges) {
std::vector<Edge> edge = edges;
int wind = 1;
if (p0.y() > p1.y()) {
std::swap(p0, p1);
wind = -wind;
}
// if (p0.fY < p1.fY) {
// wind = 1;
// } else {
// std::swap(p0, p1);
// wind = -1;
// }
if (p1.y() <= bounds.top() || p0.y() >= bounds.bottom()) {
return;
}
if (p0.y() < bounds.top()) {
float x_clipped = p0.x() + (p1.x() - p0.x()) * (bounds.top() - p0.y()) / (p1.y() - p0.y());
p0.set(x_clipped, bounds.top());
}
if (p1.y() > bounds.bottom()) {
float x_clipped = p1.x() - (p1.x() - p0.x()) * (p1.y() - bounds.bottom()) / (p1.y() - p0.y());
p1.set(x_clipped, bounds.bottom());
}
if (p0.x() > p1.x()) {
std::swap(p0, p1);
wind = -wind;
};
if (p1.x() <= bounds.left()) {
p0.fX = p1.fX = bounds.left();
if (!checkHorizontal(p0, p1)) {
edges.push_back({p0, p1, wind});
}
return;
}
if(p0.x() >= bounds.right()){
p0.fX = p1.fX = bounds.right();
if (!checkHorizontal(p0, p1)) {
edges.push_back(Edge(p0, p1, wind));
}
return;
}
if(p0.x() < bounds.left()){
GVector v = p1 - p0;
v = v * ((bounds.left() - p0.x()) / (p1.x()- p0.x()));
if (!checkHorizontal(p0, p0 + v)) {
edges.push_back(Edge(GPoint::Make(bounds.left(), p0.y()), p0 + v, wind));
}
p0 = p0 + v;
}
if(p1.x() >= bounds.right()){
GVector v = p0 - p1;
v = v * ((p1.x() - bounds.right()) / (p1.x()- p0.x()));
if (!checkHorizontal(p1 + v, p1)) {
edges.push_back(Edge(p1 + v, GPoint::Make(bounds.right(), p1.y()), wind));
}
p1 = p1 + v;
}
if(!checkHorizontal(p0, p1))
edges.push_back(Edge(p0, p1, wind));
}