计算几何学简单的模板

一些定义

#include <iostream>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define maxn 1200
#define eps 1e-8
struct point
{
    double x;
    double y;
}po[maxn];
struct line
{
    point a;
    point b;
};
typedef point vector;
vector operator + (vector a,vector b)
{
    return vector(a.x+b.x,a.y+b.y);
}
vector operator - (point a,point b)
{
    return vector(a.x-b.x,a.y-b.y);
}
vector operator * (vector a,double b)
{
    return vector(a.x*p,a.y*p);
}
vector operator / (vector a,double b)
{
    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 xmulit(point &a,point &b,point &c)
{
    return (a.x-b.x)*(a.y-c.y)-(a.y-b.y)*(a.x-c.x);
}

直线ab和线段cd是否相交(用叉乘来判断两点是否在线段的两侧)

bool across(point &a,point &b,point &c,point &d)
{
    double p=xmulit(a,b,c),p1=xmulit(a,b,d);
    if( fabs(p1) <= eps || fabs(p) <= eps ) return true;
    if( p*p1 < -eps )
        return true;
    return false;
}

判断是否是同一条直线

bool one_line(point &a,point &b,point &c,point &d)
{
    double p=xmulit(a,b,c),p1=xmulit(a,b,d);
    if( fabs(p1) < eps && fabs(p) < eps ) return true;
    return false;
}

判断点a和点b是否相等

bool is_equal(point &a,point &b)
{
    return (fabs(a.x-b.x) <= eps) && (fabs(a.y-b.y) <=eps);
}

求两直线的交点

point intersection(line &u,line &v)
{
    point ret=u.a;
    double t=((u.a.x-v.a.x)*(v.a.y-v.b.y) - (u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
    ret.x+=(u.b.x-u.a.x)*t;
    ret.y+=(u.b.y-u.a.y)*t;
    return ret;
}

两点之间的距离

double dis(point &a,point &b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

判断点pk时候在线段pi, pj上

bool on_line(point pi,point pj,point pk)
{
    if(xmulit(pi, pj, pk)==0)
    {
        if(pk.x>=min(pi.x,pj.x)&&pk.x<=max(pi.x,pj.x)&&pk.y>=min(pi.y,pj.y)&&pk.y<=max(pi.y,pj.y))
            return true;
    }
    return false;
}

判断线段是否相交

bool segments_intersect(point p1,point p2,point p3,point p4)
{
    double d1=xmulit(p3,p4,p1);
    double d2=xmulit(p3,p4,p2);
    double d3=xmulit(p1,p2,p3);
    double d4=xmulit(p1,p2,p4);
    if(d1*d2<0&&d3*d4<0)
        return true;
    else if(d1==0&&on_segment(p3,p4,p1))
        return true;
    else if(d2==0&&on_segment(p3,p4,p2))
        return true;
    else if(d3==0&&on_segment(p1,p2,p3))
        return true;
    else if(d4==0&&on_segment(p1,p2,p4))
        return true;
    return false;
}

判断点是否在多边形的内部

int inpoto(point a)
{
    int i;
    point b,c,d;
    b.y=a.y;
    b.x=1e15;//定义射线
    int flag=0;
    int count=0;
    for(i=0;i<n;i++)
    {
        c = po[i];
        d = po[i + 1];
        if(on_segment(c,d,a))//该点在多边形的一条边上
            return 1;
        if(abs(c.y-d.y)<eps)
            continue;
        if(on_segment(a,b,c))//和顶点相交的情况,如果y值较大则取
        {
            if(c.y>d.y)
                count++;
        }
        else if(on_segment(a,b,d))//和顶点相交的情况,如果y值较大则取
        {
            if(d.y>c.y)
                count++;
        }
        else if(segments_intersect(a,b,c,d))//和边相交
            count++;
    }
    return count%2;//当L和多边形的交点数目C是奇数的时候,P在多边形内,是偶数的话P在多边形外。
}

求两点的中点

point mid(point &a,point &b)
{
    point c;
    c.x=(a.x+b.x)/2;
    c.y=(a.y+b.y)/2;
    return c;
}

向量旋转

vector rotate (vector a,double rad)
{
    return vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页