-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6-SutherlandHodgmanPolygonClippingAlgo.h
68 lines (62 loc) · 2.27 KB
/
6-SutherlandHodgmanPolygonClippingAlgo.h
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
#if !defined(SUTH_HODG)
#define SUTH_HODG
#include "../custom-headers/Line.h"
#include "../custom-headers/Point.h"
#include "../custom-headers/Polygone.h"
#include <vector>
template<typename T>
Polygone<T> clip(Polygone<T> polygone,Line<T> clipperLine)
{
std :: vector<Point<T>> clippedPolygon;
for (int i = 0; i < polygone.points.size(); i++)
{
int k = (i+1) % polygone.points.size();
Line<T> currentLine = polygone.getConnectedLine(i,k);
// Calculating position of point acc clipper line
int i_pos = clipperLine.getPositionOfPoint(currentLine.pStart);
int k_pos = clipperLine.getPositionOfPoint(currentLine.pEnd);
// Case 1 : When both points are inside. we are going clockwise so taking points that is negative
if (i_pos <= 0 && k_pos <= 0)
{
//Only second point is added
clippedPolygon.push_back(currentLine.pEnd);
}
// Case 2: When only first point is outside
else if (i_pos > 0 && k_pos <= 0)
{
// Point of intersection with edge and the second point is added
Point<T> p=clipperLine.getIntersectOfLines(currentLine);
clippedPolygon.push_back(p);
clippedPolygon.push_back(currentLine.pEnd);
}
// Case 3: When only second point is outside
else if (i_pos <= 0 && k_pos > 0)
{
//Only point of intersection with edge is added
Point<T> p=clipperLine.getIntersectOfLines(currentLine);
clippedPolygon.push_back(p);
}
// Case 4: When both points are outside
else
{
//No points are added
}
}
polygone.points=clippedPolygon;
return polygone;
}
template<typename T>
Polygone<T> suthHodgClip(Polygone<T> polygone, Polygone<T> window)
{
//i and k are two consecutive indexes
for (int i=0; i<window.points.size(); i++)
{
int k = (i+1) % window.points.size();
// We pass the current array of vertices, it's size
// and the end points of the selected clipper line
Line<T> clipperLine= window.getConnectedLine(i,k);
polygone = clip<double>(polygone, clipperLine);
}
return polygone;
}
#endif // SUTH_HODG