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

hxy的未完成的计算几何模板

时间:2015-07-26 17:09:55      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <cmath>
  5 using namespace std;
  6 
  7 const double PI = acos(-1);
  8 const double eps = 1e-8;
  9 
 10 int sgn( double x )
 11 {
 12     if ( fabs(x) < eps ) return 0;
 13     return x > 0 ? x : -x;
 14 }
 15 
 16 struct Point
 17 {
 18     double x, y;
 19 
 20     Point(){}
 21 
 22     Point( double _x, double _y )
 23     {
 24         x = _x, y = _y;
 25     }
 26 
 27     Point operator + ( const Point & o ) const
 28     {
 29         return Point( x + o.x, y + o.y );
 30     }
 31 
 32     Point operator - ( const Point & o ) const
 33     {
 34         return Point( x - o.x, y - o.y );
 35     }
 36 
 37     Point operator * ( double p )
 38     {
 39         return Point( x * p, y * p );
 40     }
 41 
 42     Point operator / ( double p )
 43     {
 44         return Point( x / p, y / p );
 45     }
 46 
 47     bool operator == ( const Point & o ) const
 48     {
 49         return sgn( x - o.x ) == 0 && sgn( y - o.y ) == 0;
 50     }
 51 
 52 };
 53 
 54 typedef Point Vector;
 55 
 56 double dot( Vector a, Vector b )
 57 {
 58     return a.x * b.x + a.y * b.y;
 59 }
 60 
 61 double length( Vector a )
 62 {
 63     return sqrt( dot( a, a ) );
 64 }
 65 
 66 double angle( Vector a, Vector b )
 67 {
 68     return acos( dot( a, b ) / length(a) / length(b) );
 69 }
 70 
 71 double cross( Vector a, Vector b )
 72 {
 73     return a.x * b.y - a.y * b.x;
 74 }
 75 
 76 double darea2( Point a, Point b, Point c )
 77 {
 78     return cross( b - a, c - a );
 79 }
 80 
 81 double area( Point a, Point b, Point c )
 82 {
 83     return fabs( cross( b - a, c - a ) * 0.5 );
 84 }
 85 
 86 Vector rotate( Vector a, double rad )
 87 {
 88     return Vector( a.x * cos(rad) - a.y * sin(rad),
 89                    a.x * sin(rad) + a.y * cos(rad) );
 90 }
 91 
 92 double polygonArea( Point * p, int n )
 93 {
 94     double area = 0;
 95     for ( int i = 1; i < n - 1; i++ )
 96     {
 97         area += cross( p[i] - p[0], p[i + 1] - p[0] );
 98     }
 99     return fabs( area * 0.5 );
100 }
101 
102 struct Circle
103 {
104     Point c;
105     double r;
106 };
107 
108 double angle( Vector v )
109 {
110     return atan2( v.y, v.x );
111 }
112 
113 int main ()
114 {
115 
116 
117     return 0;
118 }

 

hxy的未完成的计算几何模板

标签:

原文地址:http://www.cnblogs.com/huoxiayu/p/4677869.html

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