浙江大学ACM模板(共93页).doc

上传人:飞****2 文档编号:5298830 上传时间:2022-01-01 格式:DOC 页数:93 大小:359KB
返回 下载 相关 举报
浙江大学ACM模板(共93页).doc_第1页
第1页 / 共93页
浙江大学ACM模板(共93页).doc_第2页
第2页 / 共93页
点击查看更多>>
资源描述

《浙江大学ACM模板(共93页).doc》由会员分享,可在线阅读,更多相关《浙江大学ACM模板(共93页).doc(93页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上Zhejiang UniversityICPC TeamRoutine Libraryby WishingBone (Dec. 2002)Last Update (Nov. 2004) by Riveria1、 几何1.1 注意1. 注意舍入方式(0.5的舍入方向);防止输出-0.2. 几何题注意多测试不对称数据.3. 整数几何注意xmult和dmult是否会出界; 符点几何注意eps的使用.4. 避免使用斜率;注意除数是否会为0.5. 公式一定要化简后再代入.6. 判断同一个2*PI域内两角度差应该是 abs(a1-a2)pi+pi-beta; 相等应该是 abs(

2、a1-a2)pi+pi-eps;7. 需要的话尽量使用atan2,注意:atan2(0,0)=0, atan2(1,0)=pi/2,atan2(-1,0)=-pi/2,atan2(0,1)=0,atan2(0,-1)=pi.8. cross product = |u|*|v|*sin(a) dot product = |u|*|v|*cos(a)9. (P1-P0)x(P2-P0)结果的意义: 正: 在顺时针(0,pi)内 负: 在逆时针(0,pi)内 0 : ,共线,夹角为0或pi10. 误差限缺省使用1e-8!1.2 几何公式三角形:1. 半周长 P=(a+b+c)/22. 面积 S=aH

3、a/2=absin(C)/2=sqrt(P(P-a)(P-b)(P-c)3. 中线 Ma=sqrt(2(b2+c2)-a2)/2=sqrt(b2+c2+2bccos(A)/24. 角平分线 Ta=sqrt(bc(b+c)2-a2)/(b+c)=2bccos(A/2)/(b+c)5. 高线 Ha=bsin(C)=csin(B)=sqrt(b2-(a2+b2-c2)/(2a)2)6. 内切圆半径 r=S/P=asin(B/2)sin(C/2)/sin(B+C)/2) =4Rsin(A/2)sin(B/2)sin(C/2)=sqrt(P-a)(P-b)(P-c)/P) =Ptan(A/2)tan(B

4、/2)tan(C/2)7. 外接圆半径 R=abc/(4S)=a/(2sin(A)=b/(2sin(B)=c/(2sin(C)四边形:D1,D2为对角线,M对角线中点连线,A为对角线夹角1. a2+b2+c2+d2=D12+D22+4M22. S=D1D2sin(A)/2(以下对圆的内接四边形)3. ac+bd=D1D24. S=sqrt(P-a)(P-b)(P-c)(P-d),P为半周长正n边形:R为外接圆半径,r为内切圆半径1. 中心角 A=2PI/n2. 内角 C=(n-2)PI/n3. 边长 a=2sqrt(R2-r2)=2Rsin(A/2)=2rtan(A/2)4. 面积 S=nar

5、/2=nr2tan(A/2)=nR2sin(A)/2=na2/(4tan(A/2)圆:1. 弧长 l=rA2. 弦长 a=2sqrt(2hr-h2)=2rsin(A/2)3. 弓形高 h=r-sqrt(r2-a2/4)=r(1-cos(A/2)=atan(A/4)/24. 扇形面积 S1=rl/2=r2A/25. 弓形面积 S2=(rl-a(r-h)/2=r2(A-sin(A)/2棱柱:1. 体积 V=Ah,A为底面积,h为高2. 侧面积 S=lp,l为棱长,p为直截面周长3. 全面积 T=S+2A棱锥:1. 体积 V=Ah/3,A为底面积,h为高(以下对正棱锥)2. 侧面积 S=lp/2,l

6、为斜高,p为底面周长3. 全面积 T=S+A棱台:1. 体积 V=(A1+A2+sqrt(A1A2)h/3,A1.A2为上下底面积,h为高(以下为正棱台)2. 侧面积 S=(p1+p2)l/2,p1.p2为上下底面周长,l为斜高3. 全面积 T=S+A1+A2圆柱:1. 侧面积 S=2PIrh2. 全面积 T=2PIr(h+r)3. 体积 V=PIr2h圆锥:1. 母线 l=sqrt(h2+r2)2. 侧面积 S=PIrl3. 全面积 T=PIr(l+r)4. 体积 V=PIr2h/3圆台:1. 母线 l=sqrt(h2+(r1-r2)2)2. 侧面积 S=PI(r1+r2)l3. 全面积 T

7、=PIr1(l+r1)+PIr2(l+r2)4. 体积 V=PI(r12+r22+r1r2)h/3球:1. 全面积 T=4PIr22. 体积 V=4PIr3/3球台:1. 侧面积 S=2PIrh2. 全面积 T=PI(2rh+r12+r22)3. 体积 V=PIh(3(r12+r22)+h2)/6球扇形:1. 全面积 T=PIr(2h+r0),h为球冠高,r0为球冠底面半径2. 体积 V=2PIr2h/31.3 多边形#include #include #define MAXN 1000#define offset 10000#define eps 1e-8#define zero(x) (x

8、)0?(x):-(x)eps?1:(x)-eps?2:0)struct pointdouble x,y;struct linepoint a,b;double xmult(point p1,point p2,point p0)return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);/判定凸多边形,顶点按顺时针或逆时针给出,允许相邻边共线int is_convex(int n,point* p)int i,s3=1,1,1;for (i=0;in&s1|s2;i+)s_sign(xmult(p(i+1)%n,p(i+2)%n,pi)=0;re

9、turn s1|s2;/判定凸多边形,顶点按顺时针或逆时针给出,不允许相邻边共线int is_convex_v2(int n,point* p)int i,s3=1,1,1;for (i=0;in&s0&s1|s2;i+)s_sign(xmult(p(i+1)%n,p(i+2)%n,pi)=0;return s0&s1|s2;/判点在凸多边形内或多边形边上,顶点按顺时针或逆时针给出int inside_convex(point q,int n,point* p)int i,s3=1,1,1;for (i=0;in&s1|s2;i+)s_sign(xmult(p(i+1)%n,q,pi)=0;r

10、eturn s1|s2;/判点在凸多边形内,顶点按顺时针或逆时针给出,在多边形边上返回0int inside_convex_v2(point q,int n,point* p)int i,s3=1,1,1;for (i=0;in&s0&s1|s2;i+)s_sign(xmult(p(i+1)%n,q,pi)=0;return s0&s1|s2;/判点在任意多边形内,顶点按顺时针或逆时针给出/on_edge表示点在多边形边上时的返回值,offset为多边形坐标上限int inside_polygon(point q,int n,point* p,int on_edge=1)point q2;in

11、t i=0,count;while (in)for (count=i=0,q2.x=rand()+offset,q2.y=rand()+offset;in;i+)if (zero(xmult(q,pi,p(i+1)%n)&(pi.x-q.x)*(p(i+1)%n.x-q.x)eps&(pi.y-q.y)*(p(i+1)%n.y-q.y)eps)return on_edge;else if (zero(xmult(q,q2,pi)break;else if (xmult(q,pi,q2)*xmult(q,p(i+1)%n,q2)-eps&xmult(pi,q,p(i+1)%n)*xmult(pi

12、,q2,p(i+1)%n)-eps)count+;return count&1;inline int opposite_side(point p1,point p2,point l1,point l2)return xmult(l1,p1,l2)*xmult(l1,p2,l2)-eps;inline int dot_online_in(point p,point l1,point l2)return zero(xmult(p,l1,l2)&(l1.x-p.x)*(l2.x-p.x)eps&(l1.y-p.y)*(l2.y-p.y)eps;/判线段在任意多边形内,顶点按顺时针或逆时针给出,与边界

13、相交返回1int inside_polygon(point l1,point l2,int n,point* p)point tMAXN,tt;int i,j,k=0;if (!inside_polygon(l1,n,p)|!inside_polygon(l2,n,p)return 0;for (i=0;in;i+)if (opposite_side(l1,l2,pi,p(i+1)%n)&opposite_side(pi,p(i+1)%n,l1,l2)return 0;else if (dot_online_in(l1,pi,p(i+1)%n)tk+=l1;else if (dot_onlin

14、e_in(l2,pi,p(i+1)%n)tk+=l2;else if (dot_online_in(pi,l1,l2)tk+=pi;for (i=0;ik;i+)for (j=i+1;jk;j+)tt.x=(ti.x+tj.x)/2;tt.y=(ti.y+tj.y)/2;if (!inside_polygon(tt,n,p)return 0;return 1;point intersection(line u,line v)point ret=u.a;double t=(u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x)/(u.a.x-

15、u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x);ret.x+=(u.b.x-u.a.x)*t;ret.y+=(u.b.y-u.a.y)*t;return ret;point barycenter(point a,point b,point c)line u,v;u.a.x=(a.x+b.x)/2;u.a.y=(a.y+b.y)/2;u.b=c;v.a.x=(a.x+c.x)/2;v.a.y=(a.y+c.y)/2;v.b=b;return intersection(u,v);/多边形重心point barycenter(int n,point*

16、 p)point ret,t;double t1=0,t2;int i;ret.x=ret.y=0;for (i=1;ieps)t=barycenter(p0,pi,pi+1);ret.x+=t.x*t2;ret.y+=t.y*t2;t1+=t2;if (fabs(t1)eps)ret.x/=t1,ret.y/=t1;return ret;1.4 多边形切割/多边形切割/可用于半平面交#define MAXN 100#define eps 1e-8#define zero(x) (x)0?(x):-(x)eps;point intersection(point u1,point u2,poin

17、t v1,point v2)point ret=u1;double t=(u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x)/(u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x);ret.x+=(u2.x-u1.x)*t;ret.y+=(u2.y-u1.y)*t;return ret;/将多边形沿l1,l2确定的直线切割在side侧切割,保证l1,l2,side不共线void polygon_cut(int& n,point* p,point l1,point l2,point side)point pp100

18、;int m=0,i;for (i=0;in;i+)if (same_side(pi,side,l1,l2)ppm+=pi;if (!same_side(pi,p(i+1)%n,l1,l2)&!(zero(xmult(pi,l1,l2)&zero(xmult(p(i+1)%n,l1,l2)ppm+=intersection(pi,p(i+1)%n,l1,l2);for (n=i=0;im;i+)if (!i|!zero(ppi.x-ppi-1.x)|!zero(ppi.y-ppi-1.y)pn+=ppi;if (zero(pn-1.x-p0.x)&zero(pn-1.y-p0.y)n-;if

19、(n3)n=0;1.5 浮点函数/浮点几何函数库#include #define eps 1e-8#define zero(x) (x)0?(x):-(x)eps)struct pointdouble x,y;struct linepoint a,b;/计算cross product (P1-P0)x(P2-P0)double xmult(point p1,point p2,point p0)return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);double xmult(double x1,double y1,double x2,doub

20、le y2,double x0,double y0)return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);/计算dot product (P1-P0).(P2-P0)double dmult(point p1,point p2,point p0)return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);double dmult(double x1,double y1,double x2,double y2,double x0,double y0)return (x1-x0)*(x2-x0)+(y1-y0)*(y2-y0

21、);/两点距离double distance(point p1,point p2)return sqrt(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);double distance(double x1,double y1,double x2,double y2)return sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);/判三点共线int dots_inline(point p1,point p2,point p3)return zero(xmult(p1,p2,p3);int dots_inline(double x

22、1,double y1,double x2,double y2,double x3,double y3)return zero(xmult(x1,y1,x2,y2,x3,y3);/判点是否在线段上,包括端点int dot_online_in(point p,line l)return zero(xmult(p,l.a,l.b)&(l.a.x-p.x)*(l.b.x-p.x)eps&(l.a.y-p.y)*(l.b.y-p.y)eps;int dot_online_in(point p,point l1,point l2)return zero(xmult(p,l1,l2)&(l1.x-p.x)

23、*(l2.x-p.x)eps&(l1.y-p.y)*(l2.y-p.y)eps;int dot_online_in(double x,double y,double x1,double y1,double x2,double y2)return zero(xmult(x,y,x1,y1,x2,y2)&(x1-x)*(x2-x)eps&(y1-y)*(y2-y)eps;int same_side(point p1,point p2,point l1,point l2)return xmult(l1,p1,l2)*xmult(l1,p2,l2)eps;/判两点在线段异侧,点在线段上返回0int o

24、pposite_side(point p1,point p2,line l)return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)-eps;int opposite_side(point p1,point p2,point l1,point l2)return xmult(l1,p1,l2)*xmult(l1,p2,l2)eps)return distance(p,l.a)eps)return distance(p,l1)eps)return distance(p,l.a)eps)return distance(p,l1)distance(p,l2)?distan

25、ce(p,l1):distance(p,l2);return fabs(xmult(p,l1,l2)/distance(l1,l2);/矢量V以P为顶点逆时针旋转angle并放大scale倍point rotate(point v,point p,double angle,double scale)point ret=p;v.x-=p.x,v.y-=p.y;p.x=scale*cos(angle);p.y=scale*sin(angle);ret.x+=v.x*p.x-v.y*p.y;ret.y+=v.x*p.y+v.y*p.x;return ret;1.6 面积#include struct

26、 pointdouble x,y;/计算cross product (P1-P0)x(P2-P0)double xmult(point p1,point p2,point p0)return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);double xmult(double x1,double y1,double x2,double y2,double x0,double y0)return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);/计算三角形面积,输入三顶点double area_triangle(point p1,

27、point p2,point p3)return fabs(xmult(p1,p2,p3)/2;double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)return fabs(xmult(x1,y1,x2,y2,x3,y3)/2;/计算三角形面积,输入三边长double area_triangle(double a,double b,double c)double s=(a+b+c)/2;return sqrt(s*(s-a)*(s-b)*(s-c);/计算多边形面积,顶点按顺时针或逆时针

28、给出double area_polygon(int n,point* p)double s1=0,s2=0;int i;for (i=0;in;i+)s1+=p(i+1)%n.y*pi.x,s2+=p(i+1)%n.y*p(i+2)%n.x;return fabs(s1-s2)/2;1.7 球面#include const double pi=acos(-1);/计算圆心角lat表示纬度,-90=w=90,lng表示经度/返回两点所在大圆劣弧对应圆心角,0=angle=pi+pi)dlng-=pi+pi;if (dlngpi)dlng=pi+pi-dlng;lat1*=pi/180,lat2*

29、=pi/180;return acos(cos(lat1)*cos(lat2)*cos(dlng)+sin(lat1)*sin(lat2);/计算距离,r为球半径double line_dist(double r,double lng1,double lat1,double lng2,double lat2)double dlng=fabs(lng1-lng2)*pi/180;while (dlng=pi+pi)dlng-=pi+pi;if (dlngpi)dlng=pi+pi-dlng;lat1*=pi/180,lat2*=pi/180;return r*sqrt(2-2*(cos(lat1)*cos(lat2)*cos(dlng)+sin(lat1)*sin(lat2);/计算球面距离,r为球半径inline double sphere_dist(double r,double lng1,double lat1,double lng2,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 教育教学

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁