这一部分主要总结一下在《挑战程序设计竞赛( 二)》中的一些代码
/*常用预处理*/ #define EPS (1e-10) ///设置精度 #define PI acos(-1) ///精确圆周率 #define INF 1e20 ///设置无限大 #define equals(a,b) (fabs((a)-(b))<EPS) ///处理小数相等的精度问题
利用C++和STL,对一些基本图形进行了封装
/*点*/ struct Point{double x,y;}; /*向量*/ rypedef Point Vector; /*线段*/ struct Segment{Point p1,p2; }; /*直线*/ typedef Segment Line; /*圆*/ class Circle{ public: Point c; double r; Circle(Point c = Point (),double r=0.0):c(c),r(r){} }; /*多边形*/ typedef vector<Point> Polygon; /*点*//*包括对点的处理*/ class Point{ public: double x,y; Point (double x=0,double y=0):x(x),y(y){} Point operator + (Point &p){ return Point(x+p.x,y+p.y); } Point operator - (Point &p){ return Point(x-p.x,y-p.y); } Point operator *(double k){ return Point(x*k,y*k); } Point operator /(double k){ return Point(x/k,y/k); } double norm(Vector a){ return a.x*a.y+a.y*a.y; } double abs(Vector a){ return sqrt(norm(a)); } bool operator <(const Point &p)const{ return x!=p.x ?x<p.x:y<p.y; } bool operator ==(const Point &p)const{ return fabs(x-p.x)<EPS&&fabs(y-p.y)<EPS; } };计算几何的根本是向量,利用向量的点积和叉积,可以处理很多问题
基础几何里学过,证明就省略了
/*点积(内积)*/ 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; }上面的是之后处理问题的基本