二維幾何模板
#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;
}