Skip to content

Commit 6a503af

Browse files
committed
Reusable code for complex geometry problems
1 parent c6b42bf commit 6a503af

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<snippet>
2+
<content><![CDATA[
3+
typedef complex<double> point;
4+
#define x real()
5+
#define y imag()
6+
7+
double dot(point a,point b){ return (conj(a)*b).x ; }
8+
double cross(point a,point b){ return (conj(a)*b).y ; }
9+
10+
double dist(point a, point b){ return abs(a-b); } //distance btw points a and b
11+
12+
double dist(point a, point b, point c, bool issegment = false){ //distance btw line ab and point c
13+
double d = cross(b-a,c-a)/dist(a,b) ;
14+
if( issegment == true ) // if line is a segment, issegment is true
15+
{
16+
double dt1 = dot(b-a,c-b) ;
17+
if( dt1 > 0 ) return dist(b,c);
18+
double dt2 = dot(a-b,c-a) ;
19+
if( dt2 > 0 ) return dist(a,c);
20+
}
21+
22+
return abs(d) ;
23+
}
24+
25+
bool cmp(point& a, point& b)
26+
{
27+
if ( a.x == b.x )
28+
{
29+
return a.y < b.y ;
30+
}
31+
else
32+
{
33+
return a.x < b.x ;
34+
}
35+
}
36+
37+
template<class T>
38+
istream& operator>> (istream& is, complex<T>& p){
39+
T value;
40+
is >> value;
41+
p.real(value);
42+
is >> value;
43+
p.imag(value);
44+
return is;
45+
}
46+
]]></content>
47+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
48+
<tabTrigger>geometry-basic</tabTrigger>
49+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
50+
<!-- <scope>source.python</scope> -->
51+
</snippet>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<snippet>
2+
<content><![CDATA[
3+
// Convex Hull - Returns vector of points belonging to hull in anti-clockwise order.
4+
typedef complex<double> point;
5+
#define x real()
6+
#define y imag()
7+
8+
double dot(point a,point b){ return (conj(a)*b).x ; }
9+
double cross(point a,point b){ return (conj(a)*b).y ; }
10+
11+
double dist(point a, point b){ return abs(a-b); } //distance btw points a and b
12+
13+
double dist(point a, point b, point c, bool issegment = false){ //distance btw line ab and point c
14+
double d = cross(b-a,c-a)/dist(a,b) ;
15+
if( issegment == true ) // if line is a segment, issegment is true
16+
{
17+
double dt1 = dot(b-a,c-b) ;
18+
if( dt1 > 0 ) return dist(b,c);
19+
double dt2 = dot(a-b,c-a) ;
20+
if( dt2 > 0 ) return dist(a,c);
21+
}
22+
23+
return abs(d) ;
24+
}
25+
26+
template<class T>
27+
istream& operator>> (istream& is, complex<T>& p){
28+
T value;
29+
is >> value;
30+
p.real(value);
31+
is >> value;
32+
p.imag(value);
33+
return is;
34+
}
35+
36+
bool cmp(point& a, point& b)
37+
{
38+
if ( a.x == b.x )
39+
{
40+
return a.y < b.y ;
41+
}
42+
else
43+
{
44+
return a.x < b.x ;
45+
}
46+
}
47+
48+
49+
vector<point> hull(vector<point> P)
50+
{
51+
int n = P.size() ;
52+
int k = 0;
53+
vector<point> H(2*n) ;
54+
55+
sort( all(P), cmp );
56+
57+
for (int i = 0; i < n; i++) { // lower monotone chain
58+
while( k>=2 && cross( H[k-1]-H[k-2], P[i]-H[k-2] ) <= 0)
59+
k--;
60+
H[k++] = P[i] ;
61+
}
62+
63+
for (int i = n-2, t=k+1; i >= 0; i--) { // upper monotone chain
64+
while( k>=t && cross( H[k-1]-H[k-2], P[i]-H[k-2] ) <= 0)
65+
k--;
66+
H[k++] = P[i];
67+
}
68+
69+
H.resize(k-1);
70+
return H;
71+
}
72+
73+
]]></content>
74+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
75+
<tabTrigger>convex-hull</tabTrigger>
76+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
77+
<!-- <scope>source.python</scope> -->
78+
</snippet>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<snippet>
2+
<content><![CDATA[
3+
// Area of polygon whose perimeter points are given in order in array
4+
vector< point > p;
5+
6+
double polyarea(){
7+
double area = 0;
8+
int n = p.size();
9+
for(int i=1; i+1<n ; i++ )
10+
{
11+
point v1 = p[i] - p[0] ; //taking 0th point as origin
12+
point v2 = p[i+1] - p[0] ;
13+
area += cross(v1,v2) ;
14+
}
15+
return abs(area/2.0);
16+
}
17+
]]></content>
18+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
19+
<tabTrigger>polygon-area</tabTrigger>
20+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
21+
<!-- <scope>source.python</scope> -->
22+
</snippet>

0 commit comments

Comments
 (0)