#include <cstdio> #include <algorithm> #include <cmath> #include <vector> using namespace std; //lrj计算几何模板 struct Point { double x, y; Point(double x=0, double y=0) :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-10; //向量+向量=向量 点+向量=点 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 0; else return x < 0 ? -1 : 1; } bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } /**********************基本运算**********************/ //点积 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)) < 0) return Length(v2); else if(dcmp(Dot(v1, v3)) > 0) 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)<0 && dcmp(c3)*dcmp(c4)<0; } //判断点是否在线段上 bool OnSegment(Point P, Point a1, Point a2) { Vector v1 = a1 - P, v2 = a2 - P; return dcmp(Cross(v1, v2)) == 0 && dcmp(Dot(v1, v2)) < 0; } //求多边形面积 double PolygonArea(Point* P, int n) { double ans = 0.0; for(int i = 1; i < n - 1; ++i) ans += Cross(P[i]-P[0], P[i+1]-P[0]); return ans/2; } int main(void) { Vector a[2]; sort(a, a + 2); return 0; }
/**********************圆的相关计算**********************/ 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 = 2*(a*b + c*d), g = b*b + d*d - C.r*C.r; double delta = f*f - 4*e*g; //判别式 if(dcmp(delta) < 0) return 0; //相离 if(dcmp(delta) == 0) //相切 { t1 = t2 = -f / (2 * e); sol.push_back(L.point(t1)); return 1; } //相交 t1 = (-f - sqrt(delta)) / (2 * e); sol.push_back(L.point(t1)); t2 = (-f + sqrt(delta)) / (2 * e); sol.push_back(L.point(t2)); return 2; } //计算向量极角 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) == 0) { if(dcmp(C1.r - C2.r) == 0) return -1; //两圆重合 return 0; //没有交点 } if(dcmp(C1.r + C2.r - d) > 0) return 0; if(dcmp(fabs(C1.r - C2.r) - d) > 0) return 0; double a = angle(C2.c - C1.c); double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d)); Point p1 = C1.point(a+da), p2 = C1.point(a-da); sol.push_back(p1); if(p1 == p2) return 1; sol.push_back(p2); return 2; } //过定点作圆的切线并返回切线条数 int getTangents(Point p, Circle C, Vector* v) { Vector u = C.c - p; double dist = Length(u); if(dist < C.r) return 0; else if(dcmp(dist - C.r) == 0) { v[0] = VRotate(u, PI/2); return 1; } else { double ang = asin(C.r / dist); v[0] = VRotate(u, +ang); v[1] = VRotate(u, -ang); return 2; } } //求两个圆的公切线,并返回切线条数 //注意,这里的Circle和上面的定义的Circle不一样 int getTangents(Circle A, Circle B, Point* a, Point* b) { int cnt = 0; 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 0; //内含 double base = atan2(B.y-A.y, B.x-A.x); if(dcmp(d2) == 0 && dcmp(A.r - B.r) == 0) return -1; //重合 if(dcmp(d2 - rdiff*rdiff) == 0) //内切 { a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++; return 1; } //有外公切线 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) == 0) {//外切 a[cnt] = b[cnt] = A.point(base); cnt++; } else if(dcmp(d2 - rsum*rsum) > 0) { 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 = 0; i < n; ++i) { if(OnSegment(P, Poly[i], Poly[(i+1)%n])) return -1; //在边界上 int k = dcmp(Cross(Poly[(i+1)%n] - Poly[i], P - Poly[i])); int d1 = dcmp(Poly[i].y - P.y); int d2 = dcmp(Poly[(i+1)%n].y - P.y); if(k > 0 && d1 <= 0 && d2 > 0) wn++; if(k < 0 && d2 <= 0 && d1 > 0) wn--; } if(wn != 0) return 1; //内部 return 0; //外部 } //计算凸包,输入点数组P,个数为n,输出点数组ch。函数返回凸包顶点数。 //输入不能有重复点,函数执行后点的顺序会发生变化 //如果不希望凸包的边上有输入点,把两个 <= 改成 < //在精度要求高时,可用dcmp比较 int ConvexHull(Point* p, int n, Point* ch) { sort(p, p +n); 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--; return m; }