您的位置:首页 > 其它

二维几何模板 - 二维几何基础

2015-08-19 16:46 417 查看
二维几何模板

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#include <stack>
using namespace std;

/************************** 二维几何基础 **************************/

struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y){ }
};

typedef Point Vector;

const double eps = 1e-10;
int dcmp(double x) ///三态函数 处理与double零有关的精度问题
{
if(fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}

///向量运算
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); }

bool operator == (const Point& a, const Point& b)  { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; }

double angle(Vector v)///计算向量极角
{
return atan2(v.y, v.x);
}

double Dis(Point A, Point B)///两点距离
{
return sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
}

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;
}

///用叉积计算三角形有向面积
double Area2(Point A, Point B, Point C)
{
return Cross(B - A, C - A) / 2.0;
}

///向量绕起点旋转rad度(弧度)后的坐标
Vector Rotate(Vector A, double rad)
{
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}

///单位法线(左转90度后的单位向量)(调用需确定A为非零向量)
Vector Normal(Vector A)
{
double L = Length(A);
return Vector(-A.y / L , A.x / L);
}

///直线P + tv 和直线Q + tw的交点(调用应保证P,Q有交点 : 即 Cross(v,w)!=0)
Point GetLineInersection(Point P, Point v, Point Q, Point w)
{
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}

double DistanceToLine(Point P, Point A, Point B)///点P到直线AB距离
{
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1,v2) / Length(v1)); ///不取绝对值表示有向距离
}

double DistanceToSegment(Point P, Point A, Point B)///点P到线段AB距离
{
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)///点P在线段AB上的投影点Q
{
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
}

///线段相交判断(规范相交每条线段的两个端点都在另外一条线段两侧)
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{   ///c1, c2都为0,线段共线,c1,c2不都是0,一线段端点在另一线段上
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
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)///判断点是否在线段上
{
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Cross(a1 - p, a2 - p))< 0;
}

double ConvexPolygonArea(Point *p, int n)///多边形面积(有向面积 = area / 2)
{
double area = 0;
for(int i = 1; i < n - 1; i++)
area += Cross(p[i] - p[0], p[i+1] - p[0]);
return fabs(area) / 2;
}

Point Bcenter(Point *pnt, int n)///计算多边形的重心
{
Point p,s;
double tp, area=0, tpx=0, tpy=0;
p.x = pnt[0].x;
p.y = pnt[0].y;
for(int i = 1; i <= n; ++i)
{
s.x = pnt[i % n].x;
s.y = pnt[i % n].y;
tp = (p.x * s.y - s.x * p.y);
area += tp/2;
tpx += (p.x + s.x)*tp;
tpy += (p.y + s.y)*tp;
p.x = s.x;
p.y = s.y;
}
s.x = tpx / (6 * area);
s.y = tpy / (6 * area);
return s;
}

int main()
{
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: