Two-dimensional geometric template--learn from Rujia Liu
Const double EPS = 1e-10;struct Point {//points defined by double x, y; Point (Double x=0, double y=0): X (x), Y (y) {}};typedef point Vector; The definition of a vector double polar_angle (vector A) {//Vector polar return atan2 (A.Y, a.x);} Double dot (vector A, vector B) {//vector dot product return a.x * b.x + a.y * B.Y;} Double Cross (vector A, vector B) {//vector fork product return a.x * B.Y-A.Y * b.x;} int dcmp (double x) {///three state function, reduced accuracy problem if (fabs (x) < EPS) return 0; else return x < 0? -1:1;} Vector operator + (vector A, vector B) {//vector addition return vector (a.x + b.x, A.Y + b.y);} Vector operator-(vector A, vector B) {//vector subtraction return vector (a.x-b.x, a.y-b.y);} Vector operator * (vector A, double p) {//vector multiplied by the scalar return vector (a.x * p, A.Y * p);} Vector operator/(vector A, double p) {//vector divided by scalar return vector (a.x/p, a.y/p);} BOOL operator < (const dot &a, const point &b) {//points ' coordinate sort return a.x < b.x | | (a.x = = b.x && a.y < B.Y);} BOOL operator = = (const dot &a, const point &b) {//judgment same spot return dcmp (a.x-b.x) = = 0 && DC MP (a.y-b.y) = = 0;} Double length (vector a) {//vector length, dot product return sqrt (dot (A, a));} Double angle (vector a, vector b) {//Vector corner, counterclockwise, dot product return ACOs (dot (A, b)/Length (a)/length (b));} Double Area_triangle (Point A, point B, point C) {//triangular area, fork product return Cross (b-a, c-a)/2.0;} Vector Rotate (vector A, double rad) {//vector rotation, counter-clockwise return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad ) + a.y * cos (RAD));} Vector Nomal (vector a) {//vector double len = length (a) of the unit method vectors; Return Vector (-a.y/len, A.x/len);} Point Point_inter (point P, Vector V, point q, Vector W) {//two line intersection, parametric equation Vector U = p-q; Double T = Cross (w, U)/Cross (V, W); return p + V * t;} Double Dis_to_line the distance from {//points to a straight line, two-point Vector V1 = b-a, V2 = p-a; RetuRN Fabs (Cross (V1, V2))/Length (V1);} Double dis_to_seg the distance from {//points to a line segment, two-point 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 Point_proj (point P, points a, dot b) {//dots are projected on a line, two points Vector V = b-a; Return a + v * (dot (v, p-a)/dot (V, v));} BOOL Inter (Points A1, point A2, Spot b1, dot b2) {//Judgment segment intersection, two-bit double C1 = Cross (A2-A1, b1-a1), C2 = Cro SS (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 On_seg (point P, bit A1, points A2) {//Judgment dot on segment, two-point return dcmp (Cross (a1-p, a2-p)) = = 0 && DCMP (dot (a1-p, a2-p)) < 0;} Double Area_poly (point *p, int n) { Polygon area DOUBLE ret = 0; for (int i=1; i<n-1; ++i) {ret + = Fabs (Cross (P[i]-p[0], p[i+1]-p[0])); } return RET/2;}
Calculate Geometry Template