0% found this document useful (0 votes)
42 views4 pages

Long Long Const Double Const Double Struct Double Double Double Void Void Double Double Double

The document defines a point struct with x and y coordinates and various operations like addition, subtraction, length, angle, etc. It also defines functions for computing distances, finding intersections, rotating points, finding convex hulls, checking if a point is inside a polygon, and converting between angle representations. The main purpose is to provide geometric algorithms and data structures for working with points and shapes in 2D space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Long Long Const Double Const Double Struct Double Double Double Void Void Double Double Double

The document defines a point struct with x and y coordinates and various operations like addition, subtraction, length, angle, etc. It also defines functions for computing distances, finding intersections, rotating points, finding convex hulls, checking if a point is inside a polygon, and converting between angle representations. The main purpose is to provide geometric algorithms and data structures for working with points and shapes in 2D space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 #include <bits/stdc++.

h>
2 using namespace std;
3 typedef long long ll;
4
5 const double eps = 1e-10;
6 const double pi = acos(-1);
7
8 struct point{
9 double x,y;
10 point(){}
11 point(double a,double b){ x=a;y=b; }
12 void in(){ scanf("%lf %lf",&x,&y); }
13 void out(){ printf("{%.9f,%.9f}\n",x,y); }
14 double len(){ return hypot(x,y); }
15 double len2(){ return x*x+y*y; }
16 double angle(){ return atan2(y,x); }
17 point operator+(point s){ return point(s.x+x,s.y+y); }
18 point operator-(point s){ return point(-s.x+x,-s.y+y); }
19 point operator*(double k){ return point(x*k,y*k); }
20 point operator/(double k){ return point(x/k,y/k); }
21 point norm(){ return point(x/len(),y/len()); }
22
23 };
24
25 point vec(point a,point b){ return b-a; }
26 double dot(point a,point b){ return a.x*b.x + a.y*b.y; }
27 double crs(point a,point b){ return a.x*b.y - a.y*b.x; }
28
29 bool same(point a,point b){
30 return fabs( vec(a,b).len() ) <= eps;
31 }
32
33 // compare 2 real numbers a & b
34 // return +1 if a > b
35 // return 0 if a = b
36 // return -1 if a < b
37 int cmp(double a,double b){
38 if( fabs(a-b) < eps )return 0;
39 if( a-b > eps )return 1;
40 return -1;
41 }
42
43 // compute the distance between c & line defined by (a,b)
44 double linePointDist(point a,point b,point c){
45 return fabs( crs( vec(a,b) , vec(a,c) )/ vec(a,b).len() );
46 }
47
48 double segmentPointDist(point a,point b,point c){
49 if( dot(vec(a,b),vec(b,c)) >= 0 )
50 return vec(b,c).len();
51
52 if( dot(vec(b,a),vec(a,c)) >= 0 )
53 return vec(a,c).len();
54
55 return linePointDist(a,b,c);
56 }
57
58 double polygonArea(vector<point>pol){
59 double area =0.0;
60 int N = pol.size();
61 for(int i=1;i+1<N;i++)
62 area += crs( vec(pol[0],pol[i]) , vec(pol[i],pol[i+1]) );
63 return fabs( area / 2.0 );
64 }
65
66 struct L{
67 point p,q;
68 double A,B,C;
69 L(){}
70 L(point aa,point bb){
71 p=aa; q=bb;
72 A = q.y - p.y;
73 B = p.x - q.x;
74 C = A * p.x + B * p.y;
75 }
76
77 L(double a,double b,double c){
78 A=a;
79 B=b;
80 C=c;
81 p=point(1.0,(c-a)/b);
82 q=point((c-b)/a,1.0);
83 if( same(p,q) )q=point((c-10.0*b)/a,1.0);
84 }
85 // latice points on segment [p,q]
86 ll get(){ return 1 + __gcd( (ll)abs( p.x-q.x ) , (ll)abs( p.y-q.y) ); }
87
88 // is x in range [l , r]
89 bool between(double l,double r,double x){
90 if( cmp(l,r)==1 )swap(l,r);
91 return cmp(l,x)<=0&&cmp(x,r)<=0;
92 }
93 // is point o on line [p,q]
94 bool between(point& o){ return between(p.x,q.x,o.x)&&between(p.y,q.y,o.y); }
95
96 bool intersect(L &o,point &ret){
97 double det = A*o.B - o.A*B;
98 if( cmp(det,0)==0 )return 0;// parallel or same 2 lines
99 double dx = o.B*C - B*o.C;
100 double dy = A*o.C - o.A*C;
101 // if( dx%det !=0 || dy%det !=0 )return 0;
102 // not integer coordinates (should be used with int variables det,dx and dy)
103 dx /= det;
104 dy /= det;
105 ret = point(dx,dy);
106 return between(ret)&&o.between(ret);
107 }
108 };
109
110 void circleFrom3Points(point a,point b,point c,point &cen,double &rad){
111 L AB = L(a,b);
112 double d1 = - AB.B * ((a.x+b.x)/2.0) + AB.A * ((a.y+b.y)/2.0);
113 L t1 = L(-AB.B,AB.A,d1);
114
115 L BC = L(b,c);
116 double d2 = - BC.B * ((b.x+c.x)/2.0) + BC.A * ((b.y+c.y)/2.0);
117 L t2 = L(-BC.B,BC.A,d2);
118
119 t1.intersect(t2,cen);
120 rad = vec(cen,a).len();
121 }
122
123 point rot(point p,double t){
124 return point( p.x*cos(t) - p.y*sin(t) , p.x*sin(t) + p.y*cos(t) );
125 }
126
127 point rotAboutPoint(point p,double t,point q){
128 return q+rot(p-q,t);
129 }
130
131 bool cmp1(point a,point b){
132 return cmp(a.x,b.x)==-1 ||( cmp(a.x,b.x)==0 && cmp(a.y,b.y)==-1 );
133 }
134
135 vector<point> convexHull(vector<point>pol){
136 sort(pol.begin(),pol.end(),cmp1);
137
138 // remove duplicate points
139 vector<point>tmp;
140 tmp.push_back(pol[0]);
141 int N = pol.size();
142 for(int i=1;i<N;i++)
143 if( !same(pol[i-1],pol[i]) )
144 tmp.push_back(pol[i]);
145 pol=tmp;
146
147 N=pol.size();// again
148 vector<point>up,dn;
149 for(int i=0;i<N;i++){
150 while( dn.size() >=2 &&
crs(vec(dn[dn.size()-2],dn[dn.size()-1]),vec(dn[dn.size()-1],pol[i])) <0 )
151 dn.pop_back();
152 dn.push_back(pol[i]);
153 }
154
155
156 for(int i=N-1;i>=0;i--){
157 while( up.size() >=2 &&
crs(vec(up[up.size()-2],up[up.size()-1]),vec(up[up.size()-1],pol[i])) <0 )
158 up.pop_back();
159 up.push_back(pol[i]);
160 }
161
162 dn.pop_back();
163 up.pop_back();
164
165 vector<point>cvx;
166 for(auto p:dn)cvx.push_back(p);
167 for(auto p:up)cvx.push_back(p);
168 return cvx;
169 }
170
171 bool pointInPolygon(vector<point>&pol,point &a){
172 int N = pol.size();
173 double tot =0.0;
174 for(int i=0;i<N;i++)
175 tot += vec(a,pol[(i+1)%N]).angle() - vec(a,pol[i]).angle();
176
177 return (cmp(tot,eps) != 0);
178 }
179
180 bool pointInTriangle(point a,point b,point c,point p){
181 return crs( vec(a,b) , vec(a,p) ) >=0
182 && crs( vec(b,c) , vec(b,p) ) >=0
183 && crs( vec(c,a) , vec(c,p) ) >=0 ;
184 }
185
186 bool pointInConvex(vector<point>&pol,point &a){
187 int N = pol.size();
188 if( crs( vec(pol[0],pol[1]) , vec(pol[0],a) ) < 0 )return 0;
189 if( crs( vec(pol[0],pol[N-1]) , vec(pol[0],a) ) > 0 )return 0;
190
191 int low =1,high = N-2;
192 while(low < high){
193 int mid = low+high+1>>1;
194 if( crs(vec(pol[0],pol[mid]),vec(pol[0],a)) >0 )low=mid;
195 else high=mid-1;
196 }
197 return pointInTriangle(pol[0],pol[low],pol[low+1],a);
198 }
199
200 ld an360(point p){
201 ld ct=p.x/hybot(p.x,p.y);
202 ld st=p.y/hybot(p.x,p.y);
203 ld th;
204 if(st==0.0||ct==0.0){
205 if(ct==0.0){
206 if(st>0.0)th=90.0;
207 else th=270.0;
208 }
209 else{
210 if(ct>0.0)th=0.0;
211 else th=180.0;
212 }
213
214 }
215 th=acos(ct);
216 else{
217 if(st>0.0&&ct>0.0)th=th*180.0/M_PI;
218 if(st>0.0&&ct<0.0)th=th*180.0/M_PI;
219 if(st<0.0&&ct<0.0)th=360.0-th*180.0/M_PI;
220 if(st<0.0&&ct>0.0)th=360.0-th*180.0/M_PI;
221 }
222 return th;
223 }
224
225 int main(){ }
226

You might also like