非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241
题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线,
即露出来的表面边长,有些山是重叠的不计。空白地带不计,每座山都是等腰三角形。
分析:大白书P414页。
求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点。这样就能保
证相邻两个扫描点之间再无交点。然后一最上面的点就是分割点,维护上一个点lastp即可。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
const double eps=1e-;
using namespace std; struct Point{ //定义点
double x;
double y;
Point(double x=,double y=):x(x),y(y){} //构造函数
//void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}
}; int dcmp(double x) {return (x>eps)-(x<-eps); } //判断精度 typedef Point Vector; //自定义别名 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);} //向量/数=向量 ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;} //输出点的 符号重载 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)==&&dcmp(A.y-B.y)==;} //等于号 重载 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-B.x*A.y; } //叉积 double Length(Vector A) { return sqrt(Dot(A, A));} //向量长度 double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));} //向量夹角 double Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);} //三角形面积 Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));} //向量旋转,rad为逆时针旋转的弧度
Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);} //计算向量的单位法线,需确保A不是零向量。 Point GetLineIntersection(Point P,Vector v,Point Q,Vector w) //两直线的交点。需确保直线 P+tv 和 Q+tw有唯一交点,cross(v,w)需非0。
{ //t是参数,v,w分别为两直线的向量。
Vector u=P-Q;
double t=Cross(w, u)/Cross(v,w);
return P+v*t;
} double DistanceToLine(Point P,Point A,Point B) //点到直线的距离,p到ab的距离
{
Vector v1=P-A; Vector v2=B-A;
return fabs(Cross(v1,v2))/Length(v2);
} double DistanceToSegment(Point P,Point A,Point B) //点到线段的距离,p到ab
{
if(A==B) return Length(P-A);
Vector v1=B-A;
Vector v2=P-A;
Vector v3=P-B; if(dcmp(Dot(v1,v2))==-) return Length(v2);
else if(Dot(v1,v3)>) return Length(v3);
else return DistanceToLine(P, A, B); } Point GetLineProjection(Point P,Point A,Point B) //点在直线的投影,p到ab
{
Vector v=B-A;
Vector v1=P-A;
double t=Dot(v,v1)/Dot(v,v);
return A+v*t;
} bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2) //判断线段相交,交点不在端点上(如果交点在端点上可以借助下面的OnSegment来判断)
{
double c1=Cross(b1-a1, a2-a1);
double c2=Cross(b2-a1, a2-a1);
double c3=Cross(a1-b1, b2-b1);
double c4=Cross(a2-b1, b2-b1);
return dcmp(c1)*dcmp(c2)<&&dcmp(c3)*dcmp(c4)< ;
} bool OnSegment(Point P,Point A,Point B) //判断一个点是否在一条线段上
{
return dcmp(Cross(P-A, P-B))==&&dcmp(Dot(P-A,P-B))<;
} double PolygonArea(Point *p,int n) //多边形的有向面积
{
double area=; for(int i=;i<n-;i++)
{
area+=Cross(p[i]-p[], p[i+]-p[]);
}
return area/; } Point read_point() //输入点
{
Point P;
scanf("%lf%lf",&P.x,&P.y);
return P;
} int n;
Point L[][][];
double x[]; // 存放离散化的x坐标 int main()
{
double X,H,B;
int cas=;
while(cin>>n && n)
{
int c=;
for(int i=;i<n;i++)
{
scanf("%lf%lf%lf",&X,&H,&B);
L[i][][]=Point(X-B*0.5,);
L[i][][]=L[i][][]=Point(X,H);
L[i][][]=Point(X+B*0.5,); x[c++]=X-B*0.5;
x[c++]=X;
x[c++]=X+B*0.5;
}
for(int i=;i<n;i++)
for(int a=;a<;a++)
for(int j=i+;j<n;j++)
for(int b=;b<;b++)
{
Point A=L[i][a][];
Point B=L[i][a][];
Point C=L[j][b][];
Point D=L[j][b][]; if(SegmentProperIntersection(A, B, C, D))
{
x[c++]=GetLineIntersection(A, B-A, C, D-C).x;
}
} sort(x,x+c);
c=unique(x, x+c)-x; //unique()函数去重函数,在头文件algorithm中
double ans=;
Point lastp=Point(x[],); for(int i=;i<c;i++)
{
Point P=Point(x[i],);
Vector v=Vector(,);
double maxy=-;
Point inter; for(int j=;j<n;j++)
for(int a=;a<;a++)
{
Point A=L[j][a][];
Point B=L[j][a][];
if(dcmp(A.x-x[i])<=&&dcmp(B.x-x[i])>=)
{
inter=GetLineIntersection(A, B-A, P, v);
maxy=max(maxy,inter.y);
}
}
if(i>&&(dcmp(maxy)>||dcmp(lastp.y)>)) ans+=Length(Point(x[i],maxy)-lastp);
lastp=Point(x[i],maxy);
}
printf("Case %d: %.0f\n\n",++cas,ans);
}
}