joj 2536
计算几何求解最小外接矩形的问题,这道题是暑假训练时做的一道题,当时不会做,选拔赛在即,突然想看看自己进步了多少,便做了一下,想法很容易就有了(说明进步了。。),但总是wa,逼我买了前辈的代码(178point啊),但还是找不到哪错了,最后终于发现最后四个数的初始化有问题,天,我已经初始化为整形最大值了(数据为double类型),还是不够大,天知道他给的测试数据有多变态。。。。话说马上选拔赛了,总感觉心里很不安,哎,尽人事,听天命吧。
#include<math.h>
#include<iostream>
#include<stdio.h>
using namespace std;
const double PI = 3.14159265;
struct POINT
{
double x;
double y;
};
POINT bian[100];
POINT rotate(POINT o,double alpha,POINT p)
{
POINT tp;
p.x -=o.x;
p.y -=o.y;
tp.x=p.x*cos(alpha) - p.y*sin(alpha)+o.x;
tp.y=p.y*cos(alpha) + p.x*sin(alpha)+o.y;
return tp;
}
POINT circle_center(POINT pt[3])
{
double x1,x2,x3,y1,y2,y3;
double x = 0;
double y = 0;
x1 = pt[0].x;
x2 = pt[1].x;
x3 = pt[2].x;
y1 = pt[0].y;
y2 = pt[1].y;
y3 = pt[2].y;
x=((y2-y1)*(y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1))/(2*(x3-x1)*(y2-y1)-2*((x2-x1)*(y3-y1)));
y=((x2-x1)*(x3*x3-x1*x1+y3*y3-y1*y1)-(x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1))/(2*(y3-y1)*(x2-x1)-2*((y2-y1)*(x3-x1)));
POINT center;
center.x = x ;
center.y = y ;
return center;
}
int main()
{
int n,i,iii=0;
while(scanf("%d",&n),n)
{
iii++;
POINT p[3];
for(i=0;i<3;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
POINT center=circle_center(p);
bian[0]=p[0];
double m=(2*PI)/n;
for(i=1;i<n;i++)
bian[i]=rotate(center,m,bian[i-1]);
double xmax=bian[0].x,ymax=bian[0].y,xmin=bian[0].x,ymin=bian[0].y;
for(i=0;i<n;i++)
{
if(xmax<bian[i].x)
xmax=bian[i].x;
if(ymax<bian[i].y)
ymax=bian[i].y;
if(xmin>bian[i].x)
xmin=bian[i].x;
if(ymin>bian[i].y)
ymin=bian[i].y;
}
double area=(ymax-ymin)*(xmax-xmin);
printf("Polygon %d: %.2lf\n",iii,area);
}
return 0;
}