pta-神坛
[神坛]pta
*相邻两条边围成的三角形面积会是最小的
极角排序+叉积计算三角形面积
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, m;
double ans = -1;
struct node{
int x, y;
int pos;
}a[N], b[N];
int jd(node s){
if(s.x > 0 && s.y > 0) return 1;
if(s.x < 0 && s.y > 0) return 2;
if(s.x < 0 && s.y < 0) return 3;
if(s.x > 0 && s.y < 0) return 4;
return 0;//不加的话,vscode会报错
}
//计算几何:极角排序
bool cmp(node s1, node s2){
if(s1.pos != s2.pos) return s1.pos < s2.pos;
return s1.x * s2.y - s1.y * s2.x > 0;
}
signed main(){
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
for(int i = 1; i <= n; i++){
int cnt = 0;
for(int j = 1; j <= n; j++){//确定第二个点
if(i == j) continue;
b[++cnt].x = a[i].x - a[j].x;
b[cnt].y = a[i].y - a[j].y;
b[cnt].pos = jd(b[cnt]);
}
sort(b + 1, b + n, cmp);
for(int j = 1; j < n - 1; j ++){
if(ans == -1 || abs(b[j].x * b[j + 1].y - b[j].y * b[j + 1].x ) * 0.5 < ans){
//叉乘计算三角形面积
ans = abs(b[j].x * b[j + 1].y - b[j].y * b[j + 1].x ) * 0.5;
}
}
}
printf("%.3lf", ans);
return 0;
}