码迷,mamicode.com
首页 > 其他好文 > 详细

计算几何模板

时间:2014-09-07 23:42:46      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   cti   sp   

是时候贴一些几何模板了。。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<vector>

using namespace std;

#define mnx 50050
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define eps 1e-8
#define Pi acos(-1.0);
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1

// 角度与弧度转换 
double todeg( double rad ){
    return rad * 180 / Pi;
}
double torad( double deg ){
    return deg / 180 * Pi;
} 

int dcmp( double x ){
    if( fabs( x ) < eps ) return 0;
    return x < 0 ? -1 : 1;
}
//
struct point{
    double x, y;
    point( double x = 0, double y = 0 ) : x(x), y(y) {}
    point operator + ( const point &b ) const{
        return point( x + b.x, y + b.y );
    }
    point operator - ( const point &b ) const{
        return point( x - b.x, y - b.y );
    }
    point operator * ( const double &k ) const{
        return point( x * k, y * k );
    }
    point operator / ( const double &k ) const{
        return point( x / k, y / k );
    }
    bool operator < ( const point &b ) const{
        return dcmp( x - b.x ) < 0 || dcmp( x - b.x ) == 0 && dcmp( y - b.y ) < 0;
    }
    bool operator == ( const point &b ) const{
        return dcmp( x - b.x ) == 0 && dcmp( y - b.y ) == 0;
    }
    double len(){
        return sqrt( x * x + y * y );
    }
};
typedef point Vector;
// 线 
struct line{
    point p; 
    Vector v;
    double ang;
    line() {}
    line( point p, point v ) : p(p), v(v) {
        ang = atan2( v.y, v.x );
    }
    bool operator < ( const line &b ) const{
        return ang < b.ang;
    }
};
// 点积 
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;
}
// 点在有向线段左边  
bool onleft( line l, point p ){
    return cross( l.v, p - l.p ) > 0;
}
// 二直线交点,假设交点唯一存在  
point getintersection( line a, line b ){
    Vector u = a.p - b.p;
    double t = cross( b.v, u ) / cross( a.v, b.v );
    return a.p + a.v * t;
}
// 半平面交 
int halfplaneintersection( line *L, int n, point *poly ){
    sort( L, L + n );
    int first, last;
    point *p = new point[n];
    line *q = new line[n];
    q[first = last = 0] = L[0];
    for( int i = 1; i < n; i++ ){
        while( first < last && !onleft( L[i], p[last-1] ) ) last--;
        while( first < last && !onleft( L[i], p[first] ) ) first++;
        q[++last] = L[i];
        if( fabs( cross( q[last].v, q[last-1].v ) ) < eps ){
            last--;
            if( onleft( q[last], L[i].p ) ) q[last] = L[i];
        }
        if( first < last ) p[last-1] = getintersection( q[last-1], q[last] );
    }
    while( first < last && !onleft( q[first], p[last-1] ) ) last--;
    if( last - first <= 1 ) return 0;
    p[last] = getintersection( q[last], q[first] );
    int m = 0;
    for( int i = first; i <= last; i++ ){
        poly[m++] = p[i];
    }
    return m;
}
// 凸包(两个<=表示在凸包边上可以有输入点) 
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;
} 

 

计算几何模板

标签:style   blog   color   os   io   for   div   cti   sp   

原文地址:http://www.cnblogs.com/LJ-blog/p/3960950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!