lrj计算几何模板

整理了一下大白书上的计算几何模板。

 #include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
//lrj计算几何模板
struct Point
{
double x, y;
Point(double x=, double y=) :x(x),y(y) {}
};
typedef Point Vector; Point read_point(void)
{
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
} const double EPS = 1e-; //向量+向量=向量 点+向量=点
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); } bool operator < (const Point& a, const Point& b)
{ return a.x < b.x || (a.x == b.x && a.y < b.y); } int dcmp(double x)
{ if(fabs(x) < EPS) return ; else return x < ? - : ; } 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 Length(Vector A) { return sqrt(Dot(A, A)); } //向量的夹角,返回值为弧度
double Angle(Vector A, Vector B)
{ return acos(Dot(A, B) / Length(A) / Length(B)); } //叉积
double Cross(Vector A, Vector B)
{ return A.x*B.y - A.y*B.x; } //向量AB叉乘AC的有向面积
double Area2(Point A, Point B, Point C)
{ return Cross(B-A, C-A); } //向量A旋转rad弧度
Vector VRotate(Vector A, double rad)
{
return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
} //将B点绕A点旋转rad弧度
Point PRotate(Point A, Point B, double rad)
{
return A + VRotate(B-A, rad);
} //求向量A向左旋转90°的单位法向量,调用前确保A不是零向量
Vector Normal(Vector A)
{
double l = Length(A);
return Vector(-A.y/l, A.x/l);
} /**********************点和直线**********************/ //求直线P + tv 和 Q + tw的交点,调用前要确保两条直线有唯一交点
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)
{
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v*t;
}//在精度要求极高的情况下,可以自定义分数类 //P点到直线AB的距离
double DistanceToLine(Point P, Point A, Point B)
{
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1); //不加绝对值是有向距离
} //点到线段的距离
double DistanceToSegment(Point P, Point A, Point B)
{
if(A == B) return Length(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < ) return Length(v2);
else if(dcmp(Dot(v1, v3)) > ) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
} //点在直线上的射影
Point GetLineProjection(Point P, Point A, Point B)
{
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
} //线段“规范”相交判定
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
} //判断点是否在线段上
bool OnSegment(Point P, Point a1, Point a2)
{
Vector v1 = a1 - P, v2 = a2 - P;
return dcmp(Cross(v1, v2)) == && dcmp(Dot(v1, v2)) < ;
} //求多边形面积
double PolygonArea(Point* P, int n)
{
double ans = 0.0;
for(int i = ; i < n - ; ++i)
ans += Cross(P[i]-P[], P[i+]-P[]);
return ans/;
} int main(void)
{
Vector a[];
sort(a, a + );
return ;
} /**********************圆的相关计算**********************/ const double PI = acos(-1.0);
struct Line
{//有向直线
Point p;
Vector v;
double ang;
Line() { }
Line(Point p, Vector v): p(p), v(v) { ang = atan2(v.y, v.x); }
Point point(double t)
{
return p + v*t;
}
bool operator < (const Line& L) const
{
return ang < L.ang;
}
}; struct Circle
{
Point c; //圆心
double r; //半径
Circle(Point c, double r):c(c), r(r) {}
Point point(double a)
{//求对应圆心角的点
return Point(c.x + r*cos(a), c.y + r*sin(a));
}
}; //两圆相交并返回交点个数
int getLineCircleIntersection(Line L, Circle C, double& t1, double& t2, vector<Point>& sol)
{
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
double e = a*a + c*c, f = *(a*b + c*d), g = b*b + d*d - C.r*C.r;
double delta = f*f - *e*g; //判别式
if(dcmp(delta) < ) return ; //相离
if(dcmp(delta) == ) //相切
{
t1 = t2 = -f / ( * e);
sol.push_back(L.point(t1));
return ;
}
//相交
t1 = (-f - sqrt(delta)) / ( * e); sol.push_back(L.point(t1));
t2 = (-f + sqrt(delta)) / ( * e); sol.push_back(L.point(t2));
return ;
} //计算向量极角
double angle(Vector v) { return atan2(v.y, v.x); } int getCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol)
{//圆与圆相交,并返回交点个数
double d = Length(C1.c - C2.c);
if(dcmp(d) == )
{
if(dcmp(C1.r - C2.r) == ) return -; //两圆重合
return ; //没有交点
}
if(dcmp(C1.r + C2.r - d) > ) return ;
if(dcmp(fabs(C1.r - C2.r) - d) > ) return ; double a = angle(C2.c - C1.c);
double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (*C1.r*d));
Point p1 = C1.point(a+da), p2 = C1.point(a-da);
sol.push_back(p1);
if(p1 == p2) return ;
sol.push_back(p2);
return ;
} //过定点作圆的切线并返回切线条数
int getTangents(Point p, Circle C, Vector* v)
{
Vector u = C.c - p;
double dist = Length(u);
if(dist < C.r) return ;
else if(dcmp(dist - C.r) == )
{
v[] = VRotate(u, PI/);
return ;
}
else
{
double ang = asin(C.r / dist);
v[] = VRotate(u, +ang);
v[] = VRotate(u, -ang);
return ;
}
} //求两个圆的公切线,并返回切线条数
//注意,这里的Circle和上面的定义的Circle不一样
int getTangents(Circle A, Circle B, Point* a, Point* b)
{
int cnt = ;
if(A.r < B.r) { swap(A, B); swap(a, b); }
double d2 = (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
double rdiff = A.r - B.r;
double rsum = A.r + B.r;
if(d2 < rdiff*rdiff) return ; //内含 double base = atan2(B.y-A.y, B.x-A.x);
if(dcmp(d2) == && dcmp(A.r - B.r) == ) return -; //重合
if(dcmp(d2 - rdiff*rdiff) == ) //内切
{
a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
return ;
} //有外公切线
double ang = acos((A.r - B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
if(dcmp(rsum*rsum - d2) == )
{//外切
a[cnt] = b[cnt] = A.point(base); cnt++;
}
else if(dcmp(d2 - rsum*rsum) > )
{
ang = acos((A.r + B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt] = B.point(PI + base + ang); cnt++;
a[cnt] = A.point(base - ang); b[cnt] = B.point(PI + base - ang); cnt++;
}
return cnt;
} //转角发判定点P是否在多边形内部
int isPointInPolygon(Point P, Point* Poly, int n)
{
int wn;
for(int i = ; i < n; ++i)
{
if(OnSegment(P, Poly[i], Poly[(i+)%n])) return -; //在边界上
int k = dcmp(Cross(Poly[(i+)%n] - Poly[i], P - Poly[i]));
int d1 = dcmp(Poly[i].y - P.y);
int d2 = dcmp(Poly[(i+)%n].y - P.y);
if(k > && d1 <= && d2 > ) wn++;
if(k < && d2 <= && d1 > ) wn--;
}
if(wn != ) return ; //内部
return ; //外部
} //计算凸包,输入点数组P,个数为n,输出点数组ch。函数返回凸包顶点数。
//输入不能有重复点,函数执行后点的顺序会发生变化
//如果不希望凸包的边上有输入点,把两个 <= 改成 <
//在精度要求高时,可用dcmp比较
int ConvexHull(Point* p, int n, Point* ch)
{
sort(p, p +n);
int m = ;
for(int i = ; i < n; ++i)
{
while(m > && Cross(ch[m-] - ch[m-], p[i] - ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-; i >= ; --i)
{
while(m > k && Cross(ch[m-] - ch[m-], p[i] - ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
return m;
}

旋转卡壳的模板:

int diameter2(vector<Point>& points)
{
vector<Point> p = ConvexHull(points);
int n = p.size();
//for(int i = 0; i < n; ++i) printf("%d %d\n", p[i].x, p[i].y);
if(n == ) return ;
if(n == ) return Dist2(p[], p[]);
p.push_back(p[]);
int ans = ;
for(int u = , v = ; u < n; ++u)
{// 一条直线贴住边p[u]-p[u+1]
while(true)
{
// 当Area(p[u], p[u+1], p[v+1]) <= Area(p[u], p[u+1], p[v])时停止旋转
//因为两个三角形有一公共边,所以面积大的那个点到直线距离大
// 即Cross(p[u+1]-p[u], p[v+1]-p[u]) - Cross(p[u+1]-p[u], p[v]-p[u]) <= 0
// 根据Cross(A,B) - Cross(A,C) = Cross(A,B-C)
// 化简得Cross(p[u+1]-p[u], p[v+1]-p[v]) <= 0
int diff = Cross(p[u+]-p[u], p[v+]-p[v]);
if(diff <= )
{
ans = max(ans, Dist2(p[u], p[v]));
if(diff == ) ans = max(ans, Dist2(p[u], p[v+]));
break;
}
v = (v+)%n;
}
}
return ans;
}
上一篇:PID25 / 合并果子 ☆


下一篇:Android UI学习 - Tab的学习和使用(转)