当前位置: 首页 > news >正文

计算几何专项:UVa 12307

应用旋转卡壳求最小面积外接矩阵和最小周长外接矩阵,在网上找到了实现策略,可是不会自己实现。参考了别人的代码,搞了一上午把细节搞懂了,奉上AC代码。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const double eps=1e-6;
const double inf=1e10;
int dcmp(double x)
{if(fabs(x)<eps) return 0;else return x<0?-1:1;
}
struct point
{double x,y;point(double x=0,double y=0):x(x),y(y){}
};
point operator-(point a,point b){return point(a.x-b.x,a.y-b.y);}
point operator+(point a,point b){return point(a.x+b.x,a.y+b.y);}
point operator*(point a,double p){return point(a.x*p,a.y*p);}
bool operator<(const point& a,const point& b)
{return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
bool operator==(const point& a,const point& b)
{return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double cross(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;}
double length(point a){return sqrt(dot(a,a));}
point normal(point a)
{double l=length(a);return point(a.x/l,a.y/l);
}
double distoline(point p,point a,point b)
{point v1=b-a,v2=p-a;return fabs(cross(v1,v2))/length(v1);
}
vector<point> p;
double mina,minp;
vector<point> convex(vector<point>& p)
{sort(p.begin(),p.end());int n=p.size();vector<point> ch(n+1);int m=0;for(int i=0;i<n;i++){while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;ch[m++]=p[i];}int k=m;for(int i=n-2;i>=0;i--){while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;ch[m++]=p[i];}if(n>1) m--;ch.resize(m);return ch;
}
void rotating_calipers(vector<point>& points)
{vector<point> p=convex(points);int n=p.size();p.push_back(p[0]);mina=inf;minp=inf;int l=1,r=1,u=1;for(int i=0;i<n;i++){point edge=normal(p[(i+1)%n]-p[i]);while(dot(edge,p[r%n]-p[i])<dot(edge,p[(r+1)%n]-p[i])) r++;while(u<r||cross(edge,p[u%n]-p[i])<cross(edge,p[(u+1)%n]-p[i])) u++;while(l<u||dot(edge,p[l%n]-p[i])>dot(edge,p[(l+1)%n]-p[i])) l++;double w=dot(edge,p[r%n]-p[i])-dot(edge,p[l%n]-p[i]);double h=distoline(p[u%n],p[i],p[(i+1)%n]);mina=min(mina,w*h);minp=min(minp,2*(w+h));}
}
int main()
{int n;while(~scanf("%d",&n)){if(!n) break;p.clear();for(int i=0;i<n;i++){double x,y;scanf("%lf%lf",&x,&y);p.push_back(point(x,y));}rotating_calipers(p);printf("%.2f %.2f\n",mina,minp);}return 0;
}



http://www.taodudu.cc/news/show-2639130.html

相关文章:

  • 【physx/wasm】在physx中添加自定义接口并重新编译wasm
  • excel---常用操作
  • Lora训练Windows[笔记]
  • linux基础指令讲解(ls、pwd、cd、touch、mkdir)
  • InnoDB 事务处理机制
  • 启明云端ESP32 C3 模组WT32C3通过 MQTT 连接 AWS
  • asp dotnet core 从零开始创建一个 WebApi 服务
  • UVa 12307 Smallest Enclosing Rectangle(旋转卡壳+最小覆盖矩形)
  • uva 12307(点集的外接矩形)
  • UVA 12307 Smallest Enclosing Rectangle(旋转卡壳)
  • uva 12307 - Smallest Enclosing Rectangle(旋转卡壳)
  • UVA 12307 Smallest Enclosing Rectangle
  • UVA 12307 旋转卡壳
  • uva12307(旋转卡壳)
  • UVA12307 Smallest Enclosing Rectangle 题解
  • numpy.ptp
  • gPTP与PTP理解资料参考
  • linux下ptp性能测试
  • 时统ptp_IEEE1588 PTP对时系统原理及特点
  • 时统ptp_IEEE1588对时系统,PTP校时模块,PTP时钟服务器
  • linuxptp源码研究
  • ptp输出内容包含什么_PTP技术及其应用分析
  • IEEE1588 Precision Time Protocol(PTP)
  • Linuxptp安装部署
  • Ubuntu 设置PTP时间同步
  • PTP时间同步
  • ptp4l linux,如何使用PTP4l测试PTPV2协议精度?
  • android ptp 源码分析,ptp增加豆瓣评分
  • PTP移植
  • linuxptp分析
  • ptp输出内容包含什么_PTP 无线传输
  • ptp精准时间协议_精确时间协议PTP研究
  • ptp精准时间协议_PTP时钟协议原理
  • ptp输出内容包含什么_04-PTP命令
  • 详解PTP协议
  • ptp输出内容包含什么_解剖PTP协议