生日蛋糕上有蜡烛,问把蜡烛包括的两条平行线间的最短距离;
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
const int maxn=2e5+10;
struct point{
double x, y;
point(){}
point(double x, double y) : x(x), y(y) {}
friend point operator + (const point &a, const point &b){
return point(a.x + b.x, a.y + b.y);
}
friend point operator - (const point &a, const point &b){
return point(a.x - b.x, a.y - b.y);
}
friend point operator * (const point &a, const double &b){
return point(a.x * b, a.y * b);
}
friend point operator / (const point &a, const double &b){
return point(a.x / b, a.y / b);
}
friend bool operator == (const point &a, const point &b){
return a.x == b.x && a.y == b.y;
}
};
int r;
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
double cross(point a,point b,point c) //计算叉乘
{
return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);
}
double det(point a, point b)
{
return a.x * b.y - a.y * b.x;
}
double dot(point a, point b)
{
return a.x * b.x + a.y * b.y;
}
struct polygon_convex
{
vector<point> p;
polygon_convex(int size = 0){
p.resize(size);
}
};
bool comp_less(const point &a, const point &b)
{
return a.x - b.x < 0 || a.x - b.x == 0 && a.y - b.y < 0;
}
polygon_convex convex_hull(vector<point> a)
{
polygon_convex res(2 * a.size() + 5);
sort(a.begin(), a.end(), comp_less);
a.erase(unique(a.begin(), a.end()), a.end());
int m = 0;
for(int i = 0; i < a.size(); i ++){
while(m > 1 && det(res.p[m - 1] - res.p[m - 2], a[i] - res.p[m - 2]) <= 0) -- m;
res.p[m ++] = a[i];
}
int k = m;
for(int i = int(a.size()) - 2; i >= 0; i --){
while(m > k && det(res.p[m - 1] - res.p[m - 2], a[i] - res.p[m - 2]) <= 0) -- m;
res.p[m ++] = a[i];
}
res.p.resize(m);
if(a.size() > 1) res.p.resize(m - 1);
return res;
}
double rotating_caliper(point *p,int n)
{
int j=1;
double minn=2.0*r;
p[n]=p[0];
for (int i=0;i<n;i++)
{
while (cross(p[i],p[i+1],p[j+1])-cross(p[i],p[i+1],p[j])<=0)
j=(j+1)%n;
//cout<<dis(p[i],p[i+1])<<"++++"<<endl;
minn=min(minn,fabs(cross(p[i],p[i+1],p[j]))/dis(p[i],p[i+1]));
//cout<<minn<<"^^^^^"<<endl;
}
return minn;
}
polygon_convex a;
point t,p[maxn];
int main()
{
int nu,n;
double s=0.0;
scanf("%d%d",&nu,&r);
for (int i=0;i<nu;i++)
{
scanf("%lf%lf",&t.x,&t.y);
a.p.push_back(t);
}
a=convex_hull(a.p);
n=a.p.size();
for (int i=0;i<n;i++)
p[i]=a.p[i];
// for (int i=0;i<n;i++)
// cout<<p[i].x<<"$$$$"<<p[i].y<<endl;
if (n<=2)
printf("0.00000000\n");
else
printf("%.12lf\n",rotating_caliper(p,n));
return 0;
}