Calculate Geometry Template

Source: Internet
Author: User
Tags cos

Since I'm in charge of calculating geometry, I'll brush some more questions. WW

Template adapted from Rujia

Point, line Base section:

structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {}};typedef point Vector; Point Read () {DoubleX, Y;SCANF ("%LF%LF", &x, &y);returnPoint (x, y);} Vectoroperator+ (vector A, vector B) {returnVector (a.x + b.x, A.Y +b.y); }vectoroperator-(vector A, vector B) {returnVector (a.x-b.x, A.Y-b.y); }vectoroperator* (Vector A,DoubleP) {returnVector (A.x*p, a.y*p); }vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p, a.y/p); }Double    operator* (vector A, vector B) {returna.x*b.x + a.y*b.y;}//dot ProductDouble    operator^ (vector A, vector B) {returna.x*b.y-a.y*b.x;}//Cross ProductBOOL    operator< (Constpoint& A,Constpoint& b) {returna.x < b.x | | (a.x = = b.x && A.y <b.y); }intDCMP (Doublex) {if(Fabs (x) < EPS)return 0;Else returnX <0? -1:1; }BOOL    operator==(Constpoint& A,Constpoint& b) {returnDCMP (a.x-b.x) = =0&& dcmp (a.y-b.y) = =0; }DoubleLength (Vector A) {returnsqrt (a*a); }//the modulus of a vectorDoubleAngle (vector A, vector B) {returnACOs (A*b/length (A)/Length (B)); }//the angle of the vector, the return value is radiansDoubleAREA2 (Point A, point B, point C) {return(b-a) ^ (c-a); }//The direction area of the vector ab Fork by ACVector vrotate (vector A,Doublerad) {returnVector (A.x*cos (RAD)-A.y*sin (RAD), A.x*sin (RAD) + A.y*cos (RAD));}//vector A rotates rad radiansPoint Protate (Point A, point B,Doublerad) {returnA + vrotate (B-a, RAD);}//Rotate B Point around a point to rad radiansVector Normal (vector A) {DoubleL = Length (A);returnVector (-a.y/l, a.x/l);}//ask vector A to rotate the unit normal vector of 90° to the left, make sure a is not a 0 vector before callingPoint getlineintersection/*to make sure that the two lines have a unique intersection before calling the line intersection.*/(Point P, Vector v, point Q, Vector W) {Doublet = (w^ (p-q))/(V^W);returnP + v*t;}//You can customize the fractional class when the accuracy requirement is extremely highDoubleDistancetoline/*distance from P-point to straight-line AB*/(Point P, point A, point B) {Vector V1 = b-a, v2 = p-a;returnFabs (V1^V2)/Length (v1);}//no absolute value is a forward distanceDoubleDistancetosegment/*Point-to-line distance*/(Point P, point A, point B) {if(a==b)returnLength (P-A); Vector v1=b-a,v2=p-a,v3=p-C; if(DCMP (V1*V2) <0)returnLength (v2);Else    if(DCMP (V1*V3) >0)returnLength (v3);Else    returnFabs (V1*V2)/Length (v1);} Point Getlineprojection/*a projection of points on a straight line*/(Point P, point A, point B) {Vector v=b-A; returnA+v* ((v* (P-A))/(v*v));}BOOLSs_is/*Line segment "Specification" intersection decision*/(Point A1, Point A2, point B1, point B2) {Doublec1= (A2-A1) ^ (B1-A1), c2= (A2-A1) ^ (b2-A1); Doublec3= (B2-B1) ^ (A1-B1), c4= (B2-B1) ^ (a2-B1); returnDCMP (C1) *dcmp (C2) <0&& dcmp (C3) *dcmp (C4) <0;}BOOLOnsegment/*determines whether a point is on a line segment (with endpoints)*/(Point p,point a1,point A2) {Vector V1=a1-p,v2=a2-Q; if(dcmp (v1^v2) = =0&& min (a1.x,a2.x) <=p.x && P.x<=max (a1.x,a2.x) && min (a1.y,a2.y) <=p.y && p.y& Lt;=max (A1.Y,A2.Y))return true; return false;}BOOLIntri/*determine if a point is inside a triangle*/(Point P, point A,point b,point c) {if(DCMP (Fabs (c-a) ^ (c-b))-fabs ((p-a) ^ (p-b))-fabs ((p-b) ^ (p-c))-fabs ((p-a) ^ (p-c)) = =0)return true; return false;}DoublePolygonarea/*to find the polygon area, note that the convex hull p number starts at 0.*/(Point *p,intN) {    DoubleAns =0.0;  for(intI=1; i<n-1; i++) ans+ = (p[i]-p[0]) ^ (p[i+1]-p[0]); returnans/2;}BOOLCrossofsegandline/*to determine if a segment intersects a line*/(Point a1,point a2,point b1,vector B2) {if(Onsegment (B1,A1,A2) | | Onsegment (B1+B2,A1,A2))return true; returnDCMP (b2^ (A1-B1)) *dcmp (b2^ (A2-B1)) <0;}

Calculate Geometry Template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.