// Computational geometry 1 points // By kevin_samuel (Fenice) soochow University 2011 // kevin.samuel.sun@gmail.com // optional <iostream> # include <cmath> # include <algorithm> # include <cstdio> using namespace STD; // defineconst double EPS = 1e-8; const double Pi = ACOs (-1.0); // pointclass point {public: Double X; Double Y; point () {}; point (Double X, Double Y ): x (x), y (y) {}; // operat Or // operator = point & operator = (const point & _ p) {x = _ p. x; y = _ p. y; return * This;} // operator * Double operator * (const point & _ p) const {return x * _ p. y-y * _ p. x;} // operator-point operator-(const point & _ p) const {return point (X-_ p. x, Y-_ p. y);} // operator = bool operator = (const point & _ p) const {If (FABS (_ p. x-x) <EPS & FABS (_ p. y-y) <EPS) return true; else return false;} bool Opera Tor! = (Const point & _ p) const {return (* This) = _ p) = false;} // dot product static double dotproduct (point S1, point E1, point S2, point E2) {double result = 0; double X1 = e1.x-s1.x; double Y1 = e1.y-s1.y; double X2 = e2.x-s2.x; double y2 = e2.y-s2.y; result = x1 * X2 + Y1 * Y2; return result;} // Cross Product 1 (4 point-type Params) Static double crossproduct (point S1, point E1, point S2, point E2) {Double result = 0; double X1 = e1.x-s1.x; double Y1 = e1.y-s1.y; double X2 = e2.x-s2.x; double y2 = e2.y-s2.y; result = x1 * Y2-X2 * Y1; return result;} // cross product 2 (3 point-type Params) Static double crossproduct (point P1, point P2, point P0) {return crossproduct (P0, P1, P0, P2);} // is on segment or line static bool onsegment (point pi, point PJ, point Q) {If (Q. x> = min (Pi. x, PJ. x )&& Q. x <= max (Pi. x, PJ. x) & Q. y> = min (Pi. y, PJ. y) & Q. Y <= max (Pi. y, PJ. y) & crossproduct (Q, PJ, Pi) = 0) return true; else return false;} // is on segment bool isonsegment (point pi, point PJ) {If (this-> x> = min (Pi. x, PJ. x) & this-> x <= max (Pi. x, PJ. x) & this-> Y> = min (Pi. y, PJ. y) & this-> Y <= max (Pi. y, PJ. y) & point: crossproduct (* This, PJ, Pi) = 0) return true; else return false;} // is inside Tri Angle bool intriangle (point a, point B, point C) {double SABC = FABS (point: crossproduct (B, C, A); double spab = FABS (point :: crossproduct (A, B, (* This); double SPAC = FABS (point: crossproduct (A, C, (* This ))); double spbc = FABS (point: crossproduct (B, c, (* This); If (spbc + spab + SPAC = SABC) return true; else return false ;} // is inside polygon // polys [], 0-n bool insidepolygon (point * polys, int N ){ Int counter = 0; double xinters; Point P1, P2; P1 = polys [0]; for (INT I = 1; I <n; I ++) {P2 = polys [I % N]; If (point: onsegment (P1, P2, (* This) = true) return true; If (* This ). y> min (p1.y, p2.y) & (* This ). Y <= max (p1.y, p2.y) {If (* This ). x <= max (p1.x, p2.x) & p1.y! = P2.y) {xinters = (* This ). y-p1.y) * (p2.x-p1.x)/(p2.y-p1.y) + p1.x; If (p1.x = p2.x | (* This ). x <= xinters) Counter ++;} p1 = P2;} If (counter % 2 = 0) return false; return true ;} // distance ^ 2 double dis2 (const point & _ p) const {return (X-_ p. x) * (X-_ p. x) + (Y-_ p. y) * (Y-_ p. y) ;}// distance double DIS (const point & _ p) const {return SQRT (dis2 (_ p) ;}}; // test zone int main () {// return 0 ;}