【转】计算几何模板

转载注明出处 @AOQNRMGYXLMV 】

#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;
}
时间: 2024-08-25 13:26:01

【转】计算几何模板的相关文章

【模板整合】【及时更新】【天坑】计算几何模板

计算几何模板要写的内容真多- 我写烦了-先写这些放上来吧- #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #define MAXDBL 1e20 #define eps 1e-9 #define pi acos(-1) using namespace std; stru

lrj计算几何模板

整理了一下大白书上的计算几何模板. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 //lrj计算几何模板 6 struct Point 7 { 8 double x, y; 9 Point(double x=0, double y=0) :x(x),y(y) {} 10 }; 11 typedef Point Vector; 12 const

计算几何模板(刘汝佳本)(转载)

转载自: 计算几何模板(仿照刘汝佳大白书风格) 想想自己一个学期连紫皮都没看完就想自杀 // Geometry.cpp #include <bits/stdc++.h> #define LL long long #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI 3.1415926535897932384626 #define EXIT exit(0); #define DEBUG puts(

计算几何模板

是时候贴一些几何模板了.. #include<iostream> #include<cstring> #include<cstdio> #include<string> #include<algorithm> #include<queue> #include<cmath> #include<vector> using namespace std; #define mnx 50050 #define LL lon

ZBT的计算几何模板

Basic template 一个基础型模板包括一个向量的实现 DATE: 2015-06-01 #define op operator #define __ while #define _0 return typedef long long ll; inline ll _(ll a,ll b){ll t;__(a){t=a;a=b%a;b=t;}_0 b;} struct frac{ ll u,d; frac(ll u=0,ll d=1):u(u),d(d){} frac op()(){ll

hdu 3060 Area2 (计算几何模板)

Problem Description 小白最近又被空军特招为飞行员,参与一项实战演习.演习的内容还是轰炸某个岛屿(这次的岛屿很大,很大很大很大,大到炸弹怎么扔都能完全在岛屿上引爆),看来小白确实是飞行员的命...这一次,小白扔的炸弹比较奇怪,爆炸的覆盖区域不是圆形,而是一个不规则的简单多边形,请你再次帮助小白,计算出炸到了多少面积.需要注意的是,这次小白一共扔了两枚炸弹,但是两枚炸弹炸到的公共部分的面积只能计算一次. Input 首先输入两个数n,m,分别代表两枚炸弹爆炸覆盖到的图形的顶点数:

LA 3263 好看的一笔画 欧拉几何+计算几何模板

题意:训练指南260 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x) , y(y) { } }; typedef Point V

计算几何模板 (bzoj 1336,poj 2451 ,poj3968)

poj 3968 (bzoj 2642) 二分+半平面交,每次不用排序,这是几个算几版综合. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<deque> using namespace std; #define MAXN 100000 na

3维计算几何模板

#include <iostream> #include <cmath> #include <vector> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; #define MAX_N 110 /*------------------常量区-------------------*/ const double INF =