-
Notifications
You must be signed in to change notification settings - Fork 3
/
mtriarea.cpp
184 lines (165 loc) · 3.36 KB
/
mtriarea.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#define MAX 50001
#define INF 10001
#define NEG_INF -1
using namespace std;
struct Point
{
int x;
int y;
};
bool onSegment(Point p1, Point p2, Point p3);
Point p0;
Point nextToTop(stack<Point> &S)
{
Point p = S.top();
S.pop();
Point res = S.top();
S.push(p);
return res;
}
void swap(Point &p1, Point &p2)
{
Point temp = p1;
p1 = p2;
p2 = temp;
}
int dist(Point p1, Point p2)
{
return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
int compare(const void *vp1, const void *vp2)
{
Point *p1 = (Point *)vp1;
Point *p2 = (Point *)vp2;
int o = orientation(p0, *p1, *p2);
if (o == 0)
return (dist(p0, *p2) >= dist(p0, *p1))? -1 : 1;
return (o == 2)? -1: 1;
}
void convexHull(Point points[], int n,stack<Point> &S)
{
int ymin = points[0].y, min = 0;
for (int i = 1; i < n; i++)
{
int y = points[i].y;
if ((y < ymin) || (ymin == y && points[i].x < points[min].x))
ymin = points[i].y, min = i;
}
swap(points[0], points[min]);
p0 = points[0];
if(n>1)
qsort(&points[1], n-1, sizeof(Point), compare);
S.push(points[0]);
if(n>2)
{
int i=0;
while((!orientation(points[i],points[i+1],points[i+2]))&&(i<n-2))
{
i++;
}
if(i<(n-2))
{
S.push(points[++i]);
S.push(points[++i]);
for (; i < n; i++)
{
while (orientation(nextToTop(S), S.top(), points[i]) != 2)
S.pop();
S.push(points[i]);
}
}
else
{
S.push(points[i+1]);
}
}
else if(n==2)
{
S.push(points[1]);
}
}
int area(Point p1, Point p2, Point p3)
{
int area;
area=(p1.x*p2.y-p1.x*p3.y-p2.x*p1.y+p2.x*p3.y+p3.x*p1.y-p3.x*p2.y);
return area;
}
int main()
{
int n,convexHullPointCount;
int A,B,C,bA,bB,bC;
Point points[MAX],hull[MAX];
stack<Point> s;
while(1)
{
scanf("%d",&n);
if(n==-1)
break;
for(int i=0;i<n;i++)
{
scanf("%d",&points[i].x);
scanf("%d",&points[i].y);
}
convexHullPointCount=0;
convexHull(points,n,s);
while(!s.empty())
{
Point p=s.top();
s.pop();
hull[convexHullPointCount++]=p;
}
A=0;B=1;C=2;
bA=A;bB=B;bC=C;
while(1)
{
while(1)
{
while(area(hull[A],hull[B],hull[C])<=area(hull[A],hull[B],hull[(C+1)%convexHullPointCount]))
{
C=(C+1)%convexHullPointCount;
}
if(area(hull[A],hull[B],hull[C])<=area(hull[A],hull[(B+1)%convexHullPointCount],hull[C]))
{
B=(B+1)%convexHullPointCount;
continue;
}
else
break;
}
if(area(hull[A],hull[B],hull[C])>area(hull[bA],hull[bB],hull[bC]))
{
bA=A;bB=B;bC=C;
}
A=(A+1)%convexHullPointCount;
if(A==B)
B=(B+1)%convexHullPointCount;
if(B==C)
C=(C+1)%convexHullPointCount;
if(A==0)
break;
}
int minArea=area(hull[bA],hull[bB],hull[bC]);
printf("%d",minArea/2);
if(minArea%2==0)
printf(".00\n");
else
printf(".50\n");
}
return 0;
}