I'm just getting started. Computational geometry, I want to write a template for getting started, so that those who are just as good as I can understand.
First of all to have some theoretical knowledge, this can Baidu, I will not say more, through Baidu, you have to know:
① Cross product can judge 3 points collinear, you can also judge 2 points to form a straight line, the 3rd point on the left side of the line or the right.
② judge two segments intersect to have 2 conditions, one is what theorem of the rectangle (the name is too long, forget) another is 4 points of the cross product multiplication is less than 0 (that is, the difference)
Then you can look at the simple templates I've collected.
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int inf=0x3f3f3f3f; typedef long long ll; #define PrN printf ("\ n") #define SI (n) scanf ("%d",& (n)) #define SII (n,m) scanf ("%d%d",& (n), & amp; (M)) #define SIII (n,m,k) scanf ("%d%d%d",& (N),& (M),& (K)) #define CLE (A,val) memset (A, (Val), sizeof (a)) #define REP (i,b) for (int i=0;i< (b), i++) #define REP (i,a,b) for (int i= (a), i<= (b); i++) #define REREP (I,A,B) for (int i = (a); i>= (b); i--) Const double EPS = 1e-8;//to determine the positive or negative of doubule type or 0int sgn (double x) {if (Fabs (x) < EPS) return 0; if (x < 0) return-1; else return 1;} Build point, and overloaded operator struct point{double x, y; Point () {}, point (double _x,double _y) {x = _x;y = _y; }//Reload minus because P is used when two points are subtracted to form a vectorOint operator-(const point &b) const {return point (X-B.X,Y-B.Y); }//This is a cross product operation, it is important not to say double operator ^ (const point &b) const {return x*b.y-y*b.x; } Double operator * (const point &b) Const {return x*b.x + y*b.y; }};
Build line, struct line{point s,e; Line () {_s,point _e) {s = _s;e = _e; }};//Judgment Segment intersection bool Inter (line L1,line L2) {return//This is 2 rectangles intersect max (l1.s.x,l1.e.x) >= min (l2.s.x,l2.e.x) & amp;& Max (l2.s.x,l2.e.x) >= min (l1.s.x,l1.e.x) && max (l1.s.y,l1.e.y) >= min (l2.s.y,l2.e.y) && Max (l2.s.y,l2.e.y) >= min (l1.s.y,l1.e.y) &&//This is the determination of the cross product of the different sgn (L2.S-L1.S) ^ (l1.e-l 1.S) *SGN ((l2.e-l1.s) ^ (l1.e-l1.s)) <= 0 && sgn ((l1.s-l2.s) ^ (L2.E-L2.S)) *SGN ((L1.E-L2.S) ^ (L2.E-L2.S)) < ; = 0;} The distance double dist (point A,point b) {return sqrt ((b-a) * (b-a));} int main () {//main function call OK return 0;}
Now I started to use these, made more than 10 questions, and so on, I am more.
Computational Geometry starter Template (continuous update)