Simple computational geometry

Source: Internet
Author: User

#include <ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespacestd;//=========================================================================structv{Doublepx,py; Voperator- (ConstV c)        {V A; A.PX= px-c.px,a.py = PY-c.py; returnA; }    BOOL operator<= (ConstV c) {        returnPX <= c.px && py <=c.py; }    BOOLmin (V a,v b) {if(b <=a) swap (A, b); returnA <= * This; }    BOOLMax (V a,v b) {if(A <=b) Swap (A, a); return* This<=A; }    DoubleCP (V c) {returnpx*c.py-py*c.px; }    DoubleDP (V c) {returnPX*C.PX + py*c.py; }};//Vectorstructl{V p1,p2; BOOLP0;//p0? beeline:segment    intPA;/*Switch (PA) {case 1:parallel-x;    Case 2:parallel-y;    Default:k < oo && k! = 0; }*/    DoubleK; voidgetk () {if(p1.px = = p2.px) PA =1; Else if(p1.py = = p2.py) PA =2; ElsePA =0, k = (p1.py-p2.py)/(p1.px-p2.px); }    BOOLIP (V q) {returnQ.min (P1,P2) &&Q.max (P1,P2); }//In the Ploygon of the line    BOOLCR (L c) {V V1= P2-p1,v2 = C.p1-p1,v3 = C.p2-P1; if(v1. CP (v2) * v1. CP (V3) <0)return true; Else{            if(v2. DP (v3) <=0)return true; Else return false; }    }// Line Cross};// Linestructbl{};//Broken Linestructpl{};//Ploygon//=========================================================================DoubleCP (V a,v b) {returna.px*b.py-a.py*b.px;}//Cross productDoubleDP (V a,v b) {returnA.PX*B.PX + a.py*b.py;}//dot ProductDoubleDIS (V a,v b) {returnsqrt ((a.px-b.px) * (A.PX-B.PX) + (a.py-b.py) * (a.py-b.py));}//disdance//=========================================================================BOOLPOL (V q,l p) {/*double x1 = p.p1.px,x2 = P.p2.px,y1 = P.p1.py,y2 = p.p2.py, x0 = Q.px,y0 = q.py; if ((!P.P0) && (!) (      X0 <= max (x1,x2) && x0 >= min (x1,x2) && y0 <= Max (y1,y2) && y0 >= min (y1,y2)))) return false;*/    //if ((!p.p0) && (!) ( Q.min (P.P1,P.P2) && Q.max (P.P1,P.P2)))) return false;    if((!p.p0) && (!p.ip (q)))return false; if(! CP (Q-P.P1,P.P2-P.P1))return true; Else return false;}//Wheather A point was on a lineBOOLSCR (L a,l b) {returnA.CR (b) &&B.cr (a);}//=========================================================================intmain () {}

Resolved issue:

Determine if the point is on a line segment

Determine if two segments intersect

(This is a draft, not to be continued ~)

Simple computational geometry

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.