-
Notifications
You must be signed in to change notification settings - Fork 2
/
CGbase.cpp
214 lines (191 loc) · 4.18 KB
/
CGbase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
ID: mfs6174
PROG: 计算几何基本函数
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define sf scanf
using namespace std;
ifstream inf("ti.in");
//ofstream ouf("ti.out");
//freopen("ti.i","r",stdin);
const int maxlongint=2147483647;
const double INF=1e200;
const double Ling=1e-8;
bool fail;
inline int cwz(double x)
{
if (abs(x)<Ling)
return 0;
else
return (x>0)?1:-1;
}
struct P
{
double x;
double y;
//constructor
P(double a=0, double b=0)
{ x=a; y=b;}
P operator+(const P &b) const
{
return P(x + b.x, y + b.y);
}
P operator - (const P &b) const
{
return P(x - b.x, y - b.y);
}
bool operator<(const P &b) const//a在b逆时针
{
return x * b.y < y * b.x;
}
bool operator==(const P &b) const
{
return ((cwz(x-b.x)==0)&&(cwz(y-b.y)==0));
}
double operator ^ (const P &b) const //aXb
{
return x*b.y-b.x*y;
}
double operator *(const P &b) const
{
return x*b.x+y*b.y;
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
struct SEG
{
P s,e;
SEG(P a=P(0,0), P b=P(0,0)) { s=a; e=b;}
void input()
{
s.input();e.input();
}
};
// 直线的解析方程 a*x+b*y+c=0 为统一表示,约定 a >= 0
struct L
{
double a;
double b;
double c;
L(double d1=1, double d2=-1, double d3=0) {a=d1; b=d2; c=d3;}
};
struct RECT
{
P zs,yx;
double w,h;
RECT(){}
RECT(P a,P b)
{
zs=a;
yx=b;
w=yx.x-zs.x;
h=zs.y-yx.y;
}
P mid()
{
return P(zs.x+w/2,yx.y+h/2);
}
RECT sub(int x,int y,int fks)
{
return RECT(P(zs.x+(x-1)*w/fks,yx.y+y*h/fks),P(zs.x+x*w/fks,yx.y+(y-1)*h/fks));
}
};
inline double dst(P p1,P p2)
{
return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
}
inline double cha(P a,P b,P c)
{
return (b-a)^(c-a);
}
P lcp(P aa, P ad, P ba, P bd)//返回fail如果true说明平行或重合再交叉相减叉积即可
{ // 求直线交点
ad = ad - aa;
bd = bd - ba;
double tmp = bd ^ ad;
fail=false;
if (cwz(tmp)==0)
{
fail=true;
return P(0,0);
}
else
return P((ad.x * bd.x * (ba.y - aa.y) + aa.x * bd.x * ad.y - ba.x * ad.x * bd.y) / tmp,
(ad.y * bd.y * (aa.x - ba.x) + ba.y * ad.y * bd.x - aa.y * bd.y * ad.x) / tmp);
}
bool scwa(P &a,P &b)
{ //与射线相交判断 a,b是线段两端点
P tmp(-1.0, 0.0);//其实是坐标轴
return (a^b) * (a^tmp) > 0.0
&& (a ^tmp) * (tmp^b) > 0.0;
}
inline bool os(SEG &l,P &p) //点在线段上
{
return( (cwz(cha(l.s,l.e,p))==0) &&( ( cwz((p.x-l.s.x)*(p.x-l.e.x))<=0 )&&( cwz((p.y-l.s.y)*(p.y-l.e.y))<=0 ) ) );
}
inline P scp(SEG l1,SEG l2) //线段交点 不考虑(部分)重合的数据 fail表示不相交
{
P rr;
rr=lcp(l1.s,l1.e,l2.s,l2.e);
if (!fail)
if (os(l1,rr)&&os(l2,rr))
return rr;
fail=true;
return P(0,0);
}
inline bool scwl(SEG s,const SEG l)//线段与直线相交判度
{
double d1,d2;
d1=cha(l.s,s.s,l.e);
d2=cha(l.s,s.e,l.e);
if( (d1>Ling&&d2<-Ling || d1<-Ling&&d2>Ling) ) //跨立试验
return true ;
if( abs(d1)<Ling || abs(d2)<Ling )//这里特判了共线的情况,共线认为是相交
return true ;
return false ;
}
inline bool cmp(const P &a, const P &b)
{ //中心极角排序 从-PI到-PI内
return atan2(a.y, a.x) > atan2(b.y, b.x);
}
inline double polys(P shu[],int n)//多边形面积
{
int i;
double ss;
for (i=1;i<n;i++)
ss+=shu[i]^shu[i+1];
ss+=shu[n]^shu[1];
if (ss<0) ss=-ss;
ss/=2;
return ss;
}
P gravity(P *p, int n)//多边形重心
{
double area = 0;
P center;
center.x = 0;
center.y = 0;
for (int i = 0; i < n-1; i++)
{
area += (p[i].x*p[i+1].y - p[i+1].x*p[i].y)/2;
center.x += (p[i].x*p[i+1].y - p[i+1].x*p[i].y) * (p[i].x + p[i+1].x);
center.y += (p[i].x*p[i+1].y - p[i+1].x*p[i].y) * (p[i].y + p[i+1].y);
}
area += (p[n-1].x*p[0].y - p[0].x*p[n-1].y)/2;
center.x += (p[n-1].x*p[0].y - p[0].x*p[n-1].y) * (p[n-1].x + p[0].x);
center.y += (p[n-1].x*p[0].y - p[0].x*p[n-1].y) * (p[n-1].y + p[0].y);
center.x /= 6*area;
center.y /= 6*area;
return center;
}