-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFT.cpp
More file actions
147 lines (110 loc) · 2.53 KB
/
FT.cpp
File metadata and controls
147 lines (110 loc) · 2.53 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
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
143
#include<bits/stdc++.h>
using namespace std;
struct Point
{
int x, y;
};
Point p0;
int distance(Point p1, Point p2)
{
return (p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y);
}
void swap(Point &a, Point &b)
{
Point temp = a;
a = b;
b = temp;
}
Point St_top(stack<Point> &St)
{
Point p = St.top();
St.pop();
Point result = St.top();
St.push(p);
return result;
}
int orders(Point p, Point q, Point r)
{
int value = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (value == 0) return 0;
return (value > 0)? 1: 2;
}
int compare(const void *ap1, const void *ap2)
{
Point *p1 = (Point *)ap1;
Point *p2 = (Point *)ap2;
int order = orders(p0, *p1, *p2);
if (order == 0)
return (distance(p0, *p2) >= distance(p0, *p1))? -1 : 1;
return (order == 2)? -1: 1;
}
stack<Point> pointbetween(Point points[], int n)
{
int y_min = points[0].y, min = 0;
for (int i = 1; i < n; i++)
{
int y = points[i].y;
if ((y < y_min) || (y_min == y &&
points[i].x < points[min].x))
y_min = points[i].y, min = i;
}
swap(points[0], points[min]);
p0 = points[0];
qsort(&points[1], n-1, sizeof(Point), compare);
int m = 1;
for (int i=1; i<n; i++)
{
while (i < n-1 && orders(p0, points[i],
points[i+1]) == 0)
i++;
points[m] = points[i];
m++;
}
stack<Point> St;
St.push(points[0]);
St.push(points[1]);
St.push(points[2]);
for (int i = 3; i < m; i++)
{
while (St.size()>1 && orders(St_top(St), St.top(), points[i]) != 2)
St.pop();
St.push(points[i]);
}
return St;
}
double polygonArea(vector<int> X, vector<int> Y, int n)
{
double area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i;
}
return abs(area / 2.0);
}
int main()
{
int n;
cin >> n;
Point
points[n];
if (n <= 2){
cout << 0;
}
for (int i=0; i<n; i++){
cin >> points[i].x >> points[i].y;
}
stack<Point> s = pointbetween(points, n);
vector<int> x, y;
while (!s.empty()){
Point cur = s.top();
s.pop();
x.push_back(cur.x);
y.push_back(cur.y);
}
cout << polygonArea(x, y, x.size());
return 0;
}