struct XPOINT { int x; int y; }; struct LINESEG { XPOINT start; XPOINT end; }; struct XRECT { int left; int top; int right; int bottom; }; #define PI (3.14159265358979323846264338327950288419716939937511) //返回两点之间的距离 #ifndef DISTANCE #define DISTANCE(a, b) (sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) )) #endif /* r=dotmultiply(p1,p2,op),得到矢量(p1-op)和(p2-op)的点积,如果两个矢量都非零矢量 r<0:两矢量夹角为锐角;r=0:两矢量夹角为直角;r>0:两矢量夹角为钝角 */ #ifndef DOTMULTIPLY #define DOTMULTIPLY(a, b, o) ((a.x - o.x) * (b.x-o.x) + (a.y-o.y) * (b.y-o.y)) #endif //点与线段的关系 #ifndef RELATION #define RELATION(p, l) (DOTMULTIPLY(p, l.end, l.start) / (DISTANCE(l.start, l.end) * DISTANCE(l.start, l.end))) #endif //点到线段的垂足 #ifndef PERPENDICULAR #define PERPENDICULAR(p, l, rp) / { / double r = RELATION(p, l); / rp.x = l.start.x + r * (l.end.x - l.start.x);/ rp.y = l.start.y + r * (l.end.y - l.start.y);/ } #endif //点到线段的最短距离 #ifndef PTOLINESEGDIS #define PTOLINESEGDIS(p, l, d) / { / double r = RELATION(p, l); / if( r<0 ) / { / d = DISTANCE(p, l.start); / } / if( r>1 ) / { / d = DISTANCE(p, l.end); / } / XPOINT tp; / PERPENDICULAR(p, l, tp); / d = DISTANCE(p, tp); / } #endif #ifndef VECTRANGLE #define VECTRANGLE(l, n, A) / { / XPOINT AB = {l.end.x - l.start.x, l.end.y - l.start.y}; / XPOINT MN = {n.end.x - n.start.x, n.end.y - n.start.y}; / int ABMN = AB.x * MN.x + AB.y * MN.y; / double dAB = sqrt(AB.x * AB.x + AB.y * AB.y); / double dMN = sqrt(MN.x * MN.x + MN.y * MN.y); / if ( (dAB * dMN >= -1e-6) && (dAB * dMN <= 1e-6) ) / { / A = -1; / } / double CosAngle = ((DOUBLE)ABMN) / (dAB * dMN); / A = acos(CosAngle) * (180.0 / PI); / } #endif #ifndef RECTAREA #define RECTAREA(rect) ((rect.right-rect.left)*(rect.bottom-rect.top)) #endif