题意
https://www.acwing.com/problem/content/description/2787/
给你一个椭圆的 倾斜角 和 短半轴与长半轴的比值 问你将n个点全部覆盖的最小短半轴椭圆是多少
思路
根据椭圆的标准方程
考虑将坐标压缩 已知 P = a / b , x' = x / P,y' = y 满足 x'² + y'² = 1;
所以坐标先顺时针旋转后再压缩就变成了一个最小圆覆盖问题!
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps = 1e-12;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int MaxN = 5e5 + 5;
int n;
struct Point{
double x,y;
Point(double x = 0,double y = 0) : x(x),y(y) {}
}p[MaxN];
typedef Point Vector;
struct Circle{
Point C;
double r;
};
int dcmp(double x){//equal ?= 0
if(fabs(x) < eps) return 0;
else return x < 0 ? -1:1;
}
//运算符重载
Vector operator + (Vector A, Vector B){ return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Vector A, Vector B){ return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, double p){ return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p){ return Vector(A.x/p, A.y/p); }
bool operator == (const Point &a, const Point &b){
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
double Cross(Vector A, Vector B){ return A.x*B.y - A.y*B.x; }
double dis(Point A,Point B){
return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
//两参数方程求交点,(点,向量,点,向量)
Point Getlinenode(Point P,Point v,Point Q,Point w){
//线1(P1,P2) 线2(P3,P4) (P1,P1-P2,P3,P3-P4)
Point u = P - Q;
double t = Cross(w,u) / Cross(v,w);
return P + v * t;
}
Vector get_Fa(Vector Y){
Vector Fa;
if(!dcmp(Y.x - 0)){//原向量垂直
Fa.x = 1,Fa.y = 0;
}
else if(!dcmp(Y.y - 0)){//原向量水平
Fa.x = 0,Fa.y = 1;
}
else{
Fa.x = 1;
Fa.y = -1.0 * Y.x / Y.y;
}
return Fa;
}
//点a顺时针旋转b度(弧度)
Point rotate(Point a,double b){
Point Ro;
Ro.x = a.x * cos(b) + a.y * sin(b);
Ro.y = -a.x * sin(b) + a.y * cos(b);
return Ro;
}
Circle get_circle(Point a,Point b,Point c){
Vector ab = get_Fa(a - b);
Vector ac = get_Fa(a - c);
Point ab_mid = (a + b) / 2;
Point ac_mid = (a + c) / 2;
Point o = Getlinenode(ab_mid,ab,ac_mid,ac);
Circle C;
C.C = o;
C.r = dis(o,a);
return C;
}
//最小圆覆盖
Circle Mcc(){
random_shuffle(p,p + n);
Circle c({p[0],0});
for(int i = 1;i < n; i++){
if(dcmp(c.r - dis(p[i],c.C)) < 0){//i不在圆内
c = {p[i],0};
for(int j = 0;j < i; j++){
if(dcmp(c.r - dis(p[j],c.C)) < 0){
c = {(p[i] + p[j]) / 2,dis(p[i],p[j]) / 2};
for(int k = 0;k < j ; k++){
if(dcmp(c.r - dis(p[k],c.C)) < 0){
c = get_circle(p[i],p[j],p[k]);
}
}
}
}
}
}
return c;
}
int main()
{
scanf("%d",&n);
for(int i = 0;i < n; i++){
scanf("%lf %lf",&p[i].x,&p[i].y);
}
double a,P;
scanf("%lf %lf",&a,&P);
a = a / 180.0 * pi;
for(int i = 0;i < n; i++){
p[i] = rotate(p[i],a);//把椭圆摆正
p[i].x /= P;//坐标压缩
}
Circle ans = Mcc();
printf("%.3f\n",ans.r);
return 0;
}