Pagini recente » Cod sursa (job #1832444) | Cod sursa (job #1502871) | Cod sursa (job #1143432) | Cod sursa (job #1850466) | Cod sursa (job #2087769)
#include <bits/stdc++.h>
#define maxN 100005
#define ll long long
#define ld double
#define e 1e-14
using namespace std;
//https://gis.stackexchange.com/questions/22895/finding-minimum-area-rectangle-for-given-points
FILE *fin = freopen("rubarba.in", "r", stdin);
FILE *fout = freopen("rubarba.out", "w", stdout);
/* -------------------------------------------------------- */
int n;
struct Point
{
int x, y;
bool operator < (const Point &ot) const
{
if (x == ot.x)
return y < ot.y;
return x < ot.x;
}
} v[maxN];
/* -------------------------------------------------------- */
int head, St[maxN], Next[maxN], st[maxN];
bool vis[maxN];
/* -------------------------------------------------------- */
ld ans;
/* -------------------------------------------------------- */
ll cross_product(Point O, Point A, Point B)
{
return 1LL * (A.x - O.x) * (B.y - O.y) - 1LL * (B.x - O.x) * (A.y - O.y);
}
Point newc(int x, int y)
{
Point ret;
ret.x = v[y].x - v[x].x;
ret.y = v[y].y - v[x].y;
return ret;
}
ll DotProd(Point A, Point B)
{
return 1LL * A.x * B.x + 1LL * A.y * B.y;
}
ll CrossProd(Point A, Point B)
{
return 1LL * A.x * B.y - 1LL * A.y * B.x;
}
ld Dist(int x, int y, ld m)
{
return fabs((1.0 * v[x].y - v[x].x * m) - (1.0 * v[y].y - v[y].x * m)) / sqrt(m * m + 1);
}
/* -------------------------------------------------------- */
void convex_hull()
{
int i, p, Head;
St[1] = 1;
St[2] = 2;
Head = 2;
for (i = 3; i <= n; ++ i)
{
while (Head >= 2 && cross_product(v[St[Head - 1]], v[St[Head]], v[i]) <= 0LL)
-- Head;
St[++ Head] = i;
//vis[i] = 1;
}
-- Head;
for (i = 1; i <= Head; ++ i)
st[++ head] = St[i];
}
void Convex_hull()
{
int i, p, Head;
St[1] = n;
St[2] = n - 1;
Head = 2;
for (i = 3; i <= n; ++ i)
{
while (Head >= 2 && cross_product(v[n - St[Head - 1] + 1], v[n - St[Head] + 1], v[i]) <= 0LL)
-- Head;
St[++ Head] = n - i + 1;
//vis[i] = 1;
}
-- Head;
for (i = 1; i <= Head; ++ i)
st[++ head] = St[i];
}
void readPolygon()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++ i)
scanf("%d %d", &v[i].x, &v[i].y);
}
void findCHull()
{
sort(v + 1, v + n + 1);
convex_hull();
reverse(v + 1, v + n + 1);
Convex_hull();
reverse(v + 1, v + n + 1);
if (head < 3)
return;
for (int i = 1; i < head; ++ i)
Next[i] = i + 1;
Next[head] = 1;
}
double minAreaRect()
{
int p1 = 1, p2, p3;
for (int i = 1; i <= head; ++ i)
{
while (DotProd(newc(st[i], st[Next[i]]), newc(st[p1], st[Next[p1]])) > 0LL)
p1 = Next[p1];
if (i == 1)
p2 = p1;
while (CrossProd(newc(st[i], st[Next[i]]), newc(st[p2], st[Next[p2]])) > 0LL)
p2 = Next[p2];
if (i == 1)
p3 = p2;
while (DotProd(newc(st[i], st[Next[i]]), newc(st[p3], st[Next[p3]])) < 0LL)
p3 = Next[p3];
ld Dx, Dy;
if (v[st[i]].x == v[st[Next[i]]].x)
{
Dx = fabs((double)(v[st[i]].x * 1.0 - v[st[p2]].x * 1.0));
Dy = fabs((double)(v[st[p1]].y * 1.0 - v[st[p3]].y * 1.0));
}
else if (v[st[i]].y == v[st[Next[i]]].y)
{
Dx = fabs((double)(v[st[p1]].x * 1.0 - v[st[p3]].x * 1.0));
Dy = fabs((double)(v[st[i]].y * 1.0 - v[st[p2]].y * 1.0));
}
else
{
double m = (double)(1.0 * (v[st[Next[i]]].y - v[st[i]].y) / (v[st[Next[i]]].x - v[st[i]].x));
Dx = Dist(st[i], st[p2], m);
Dy = Dist(st[p1], st[p3], -1.0 / m);
}
if (i == 1 || Dx * Dy < ans)
ans = Dx * Dy;
}
return ans;
}
int main()
{
readPolygon();
findCHull();
printf("%.2lf\n", minAreaRect());
return 0;
}