-
Notifications
You must be signed in to change notification settings - Fork 1
/
sutherland_hodgman.cpp
142 lines (112 loc) · 4.11 KB
/
sutherland_hodgman.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
140
141
142
/* Which of the favors of your Lord will you deny? */
/**
*
* Sutherland Hodgman Polygon Clipping Algorithm
* ---------------------------------------------
*
* Works for Convex polygon clip region.
*
*/
#include <bits/stdc++.h>
using namespace std;
struct Point {
double x, y;
Point() {
x = 0;
y = 0;
}
Point(double x, double y) {
this->x = x;
this->y = y;
}
};
struct Polygon {
vector<Point> points;
};
struct Line {
Point p1, p2;
};
// Sutherland-Hodgman algorithm
Polygon sutherlandHodgman(Polygon subjectPolygon, Polygon clipPolygon) {
Polygon outputPolygon = subjectPolygon;
for (int i = 0; i < clipPolygon.points.size(); i++) {
int k = (i + 1) % clipPolygon.points.size();
Polygon inputPolygon = outputPolygon;
outputPolygon.points.clear();
Point A = clipPolygon.points[i];
Point B = clipPolygon.points[k];
// do clipping for the infinite line joining A and B
for (int j = 0; j < inputPolygon.points.size(); j++) {
int l = (j + 1) % inputPolygon.points.size();
Point P = inputPolygon.points[j];
Point Q = inputPolygon.points[l];
// check which side P is on with respect to line AB
double sideP = (B.x - A.x) * (P.y - A.y) - (B.y - A.y) * (P.x - A.x);
// check which side Q is on with respect to line AB
double sideQ = (B.x - A.x) * (Q.y - A.y) - (B.y - A.y) * (Q.x - A.x);
if (sideP < 0 && sideQ < 0) {
// both P and Q are inside, so only Q is added to the output polygon
outputPolygon.points.push_back(Q);
} else if (sideP < 0 && sideQ >= 0) {
// P is inside and Q is outside, so intersection point is added to the output polygon
double t = ((P.y - A.y) * (B.x - A.x) - (P.x - A.x) * (B.y - A.y)) / ((Q.x - P.x) * (B.y - A.y) - (Q.y - P.y) * (B.x - A.x));
Point intersectionPoint;
intersectionPoint.x = P.x + t * (Q.x - P.x);
intersectionPoint.y = P.y + t * (Q.y - P.y);
outputPolygon.points.push_back(intersectionPoint);
} else if (sideP >= 0 && sideQ <= 0) {
// P is outside and Q is inside, so intersection point and Q are added to the output polygon
double t = ((P.y - A.y) * (B.x - A.x) - (P.x - A.x) * (B.y - A.y)) / ((Q.x - P.x) * (B.y - A.y) - (Q.y - P.y) * (B.x - A.x));
Point intersectionPoint;
intersectionPoint.x = P.x + t * (Q.x - P.x);
intersectionPoint.y = P.y + t * (Q.y - P.y);
outputPolygon.points.push_back(intersectionPoint);
outputPolygon.points.push_back(Q);
}
}
}
return outputPolygon;
}
int main()
{
// Case 1
// Polygon : (100,150), (200,250), (300,200)
// Clipping Area : (150,150), (150,200), (200,200), (200,150)
Polygon subjectPolygon;
subjectPolygon.points = {
Point(100,150),
Point(200,250),
Point(300,200)
};
Polygon clipPolygon;
clipPolygon.points = {
Point(150,150),
Point(150,200),
Point(200,200),
Point(200,150)
};
Polygon outputPolygon = sutherlandHodgman(subjectPolygon, clipPolygon);
cout << "Output Polygon: " << endl;
for (int i = 0; i < outputPolygon.points.size(); i++) {
cout << "(" << outputPolygon.points[i].x << ", " << outputPolygon.points[i].y << ")" << endl;
}
// Case 2
// Polygon : (100,150), (200,250), (300,200)
// Clipping Area : (100,300), (300,300), (200,100)
subjectPolygon.points = {
Point(100,150),
Point(200,250),
Point(300,200)
};
clipPolygon.points = {
Point(100,300),
Point(300,300),
Point(200,100)
};
outputPolygon = sutherlandHodgman(subjectPolygon, clipPolygon);
cout << "Output Polygon: " << endl;
for (int i = 0; i < outputPolygon.points.size(); i++) {
cout << "(" << outputPolygon.points[i].x << ", " << outputPolygon.points[i].y << ")" << endl;
}
return 0;
}