#include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) #define pi acos(-1.0) struct point { double x,y; }; struct line { point a,b; }; struct point3 { double x,y,z; }; struct line3 { point3 a,b; }; struct plane3 { point3 a,b,c; }; //計算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); } //計算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); } //計算cross product U . V point3 xmult(point3 u,point3 v) { point3 ret; ret.x=u.y*v.z-v.y*u.z; ret.y=u.z*v.x-u.x*v.z; ret.z=u.x*v.y-u.y*v.x; return ret; } //計算dot product U . V double dmult(point3 u,point3 v) { return u.x*v.x+u.y*v.y+u.z*v.z; } //兩點距離 double distance(point p1,point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } //判三點共線 bool dots_inline(point p1,point p2,point p3) { return zero(xmult(p1,p2,p3)); } //判點是否在線段上,包括端點 bool 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; } //判點是否在線段上,不包括端點 bool dot_online_ex(point p,line l) { return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y)); } //判兩點在線段同側,點在線段上返回0 bool same_side(point p1,point p2,line l) { return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)>eps; } //判兩點在線段異側,點在線段上返回0 bool opposite_side(point p1,point p2,line l) { return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)<-eps; } //判兩直線平行 bool parallel(line u,line v) { return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y)); } //判兩直線垂直 bool perpendicular(line u,line v) { return zero((u.a.x-u.b.x)*(v.a.x-v.b.x)+(u.a.y-u.b.y)*(v.a.y-v.b.y)); } //判兩線段相交,包括端點和部分重合 bool intersect_in(line u,line v) { if (!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b)) return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u); return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u); } //判兩線段相交,不包括端點和部分重合 bool intersect_ex(line u,line v) { return opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u); } //計算兩直線交點,注意事先判斷直線是否平行! //線段交點請另外判線段相交(同時還是要判斷是否平行!) 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-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 intersection(point u1,point u2,point 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; } //點到直線上的最近點 point ptoline(point p,line l) { point t=p; t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x; return intersection(p,t,l.a,l.b); } //點到直線距離 double disptoline(point p,line l) { return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b); } //點到線段上的最近點 point ptoseg(point p,line l) { point t=p; t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x; if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps) return distance(p,l.a)<distance(p,l.b)?l.a:l.b; return intersection(p,t,l.a,l.b); } //點到線段距離 double disptoseg(point p,line l) { point t=p; t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x; if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps) return distance(p,l.a)<distance(p,l.b)?distance(p,l.a):distance(p,l.b); return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b); } //矢量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; } //計算三角形面積,輸入三頂點 double area_triangle(point p1,point p2,point p3) { return fabs(xmult(p1,p2,p3))/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)); } //計算多邊形面積,頂點按順時針或逆時針給出 double area_polygon(int n,point* p) { double s1=0,s2=0; int i; for (i=0; i<n; i++) s1+=p[(i+1)%n].y*p[i].x,s2+=p[(i+1)%n].y*p[(i+2)%n].x; return fabs(s1-s2)/2; } //計算圓心角lat 表示緯度,-90<=w<=90,lng 表示經度 //返回兩點所在大圓劣弧對應圓心角,0<=angle<=pi double angle(double lng1,double lat1,double lng2,double lat2) { double dlng=fabs(lng1-lng2)*pi/180; while (dlng>=pi+pi) dlng-=pi+pi; if (dlng>pi) dlng=pi+pi-dlng; lat1*=pi/180,lat2*=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 (dlng>pi) 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,double lat2) { return r*angle(lng1,lat1,lng2,lat2); } //外心 point circumcenter(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.x=u.a.x-a.y+b.y; u.b.y=u.a.y+a.x-b.x; v.a.x=(a.x+c.x)/2; v.a.y=(a.y+c.y)/2; v.b.x=v.a.x-a.y+c.y; v.b.y=v.a.y+a.x-c.x; return intersection(u,v); } //內心 point incenter(point a,point b,point c) { line u,v; double m,n; u.a=a; m=atan2(b.y-a.y,b.x-a.x); n=atan2(c.y-a.y,c.x-a.x); u.b.x=u.a.x+cos((m+n)/2); u.b.y=u.a.y+sin((m+n)/2); v.a=b; m=atan2(a.y-b.y,a.x-b.x); n=atan2(c.y-b.y,c.x-b.x); v.b.x=v.a.x+cos((m+n)/2); v.b.y=v.a.y+sin((m+n)/2); return intersection(u,v); } //垂心 point perpencenter(point a,point b,point c) { line u,v; u.a=c; u.b.x=u.a.x-a.y+b.y; u.b.y=u.a.y+a.x-b.x; v.a=b; v.b.x=v.a.x-a.y+c.y; v.b.y=v.a.y+a.x-c.x; return intersection(u,v); } //重心 //到三角形三頂點距離的平方和最小的點 //三角形內到三邊距離之積最大的點 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 fermentpoint(point a,point b,point c) { point u,v; double step=fabs(a.x)+fabs(a.y)+fabs(b.x)+fabs(b.y)+fabs(c.x)+fabs(c.y); int i,j,k; u.x=(a.x+b.x+c.x)/3; u.y=(a.y+b.y+c.y)/3; while (step>1e-10) { for (k=0; k<10; step/=2,k++) { for (i=-1; i<=1; i++) { for (j=-1; j<=1; j++) { v.x=u.x+step*i; v.y=u.y+step*j; if(distance(u,a)+distance(u,b)+distance(u,c)>distance(v,a)+distance(v,b)+distance(v,c)) { u=v; } } } } } return u; } //矢量差 U - V point3 subt(point3 u,point3 v) { point3 ret; ret.x=u.x-v.x; ret.y=u.y-v.y; ret.z=u.z-v.z; return ret; } //取平面法向量 point3 pvec(plane3 s) { return xmult(subt(s.a,s.b),subt(s.b,s.c)); } point3 pvec(point3 s1,point3 s2,point3 s3) { return xmult(subt(s1,s2),subt(s2,s3)); } //兩點距離,單參數取向量大小 double distance(point3 p1,point3 p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z)); } ///三維/// //向量大小 double vlen(point3 p) { return sqrt(p.x*p.x+p.y*p.y+p.z*p.z); } //判三點共線 bool dots_inline(point3 p1,point3 p2,point3 p3) { return vlen(xmult(subt(p1,p2),subt(p2,p3)))<eps; } //判四點共面 bool dots_onplane(point3 a,point3 b,point3 c,point3 d) { return zero(dmult(pvec(a,b,c),subt(d,a))); } //判點是否在線段上,包括端點和共線 bool dot_online_in(point3 p,line3 l) { return zero(vlen(xmult(subt(p,l.a),subt(p,l.b))))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps&&(l.a.z-p.z)*(l.b.z-p.z)<eps; } //判點是否在線段上,不包括端點 bool dot_online_ex(point3 p,line3 l) { return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y)||!zero(p.z-l.a.z))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y)||!zero(p.z-l.b.z)); } //判點是否在空間三角形上,包括邊界,三點共線無意義 bool dot_inplane_in(point3 p,plane3 s) { return zero(vlen(xmult(subt(s.a,s.b),subt(s.a,s.c)))-vlen(xmult(subt(p,s.a),subt(p,s.b)))-vlen(xmult(subt(p,s.b),subt(p,s.c)))-vlen(xmult(subt(p,s.c),subt(p,s.a)))); } //判點是否在空間三角形上,不包括邊界,三點共線無意義 bool dot_inplane_ex(point3 p,plane3 s) { return dot_inplane_in(p,s)&&vlen(xmult(subt(p,s.a),subt(p,s.b)))>eps&&vlen(xmult(subt(p,s.b),subt(p,s.c)))>eps&&vlen(xmult(subt(p,s.c),subt(p,s.a)))>eps; } //判兩點在線段同側,點在線段上返回0,不共面無意義 bool same_side(point3 p1,point3 p2,line3 l) { return dmult(xmult(subt(l.a,l.b),subt(p1,l.b)),xmult(subt(l.a,l.b),subt(p2,l.b)))>eps; } //判兩點在線段異側,點在線段上返回0,不共面無意義 bool opposite_side(point3 p1,point3 p2,line3 l) { return dmult(xmult(subt(l.a,l.b),subt(p1,l.b)),xmult(subt(l.a,l.b),subt(p2,l.b)))<-eps; } //判兩點在平面同側,點在平面上返回0 bool same_side(point3 p1,point3 p2,plane3 s) { return dmult(pvec(s),subt(p1,s.a))*dmult(pvec(s),subt(p2,s.a))>eps; } bool same_side(point3 p1,point3 p2,point3 s1,point3 s2,point3 s3) { return dmult(pvec(s1,s2,s3),subt(p1,s1))*dmult(pvec(s1,s2,s3),subt(p2,s1))>eps; } //判兩點在平面異側,點在平面上返回0 bool opposite_side(point3 p1,point3 p2,plane3 s) { return dmult(pvec(s),subt(p1,s.a))*dmult(pvec(s),subt(p2,s.a))<-eps; } bool opposite_side(point3 p1,point3 p2,point3 s1,point3 s2,point3 s3) { return dmult(pvec(s1,s2,s3),subt(p1,s1))*dmult(pvec(s1,s2,s3),subt(p2,s1))<-eps; } //判兩直線平行 bool parallel(line3 u,line3 v) { return vlen(xmult(subt(u.a,u.b),subt(v.a,v.b)))<eps; } //判兩平面平行 bool parallel(plane3 u,plane3 v) { return vlen(xmult(pvec(u),pvec(v)))<eps; } //判直線與平面平行 bool parallel(line3 l,plane3 s) { return zero(dmult(subt(l.a,l.b),pvec(s))); } bool parallel(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3) { return zero(dmult(subt(l1,l2),pvec(s1,s2,s3))); } //判兩直線垂直 bool perpendicular(line3 u,line3 v) { return zero(dmult(subt(u.a,u.b),subt(v.a,v.b))); } //判兩平面垂直 bool perpendicular(plane3 u,plane3 v) { return zero(dmult(pvec(u),pvec(v))); } //判直線與平面平行 bool perpendicular(line3 l,plane3 s) { return vlen(xmult(subt(l.a,l.b),pvec(s)))<eps; } //判兩線段相交,包括端點和部分重合 bool intersect_in(line3 u,line3 v) { if (!dots_onplane(u.a,u.b,v.a,v.b)) return 0; if (!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b)) return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u); return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u); } //判兩線段相交,不包括端點和部分重合 bool intersect_ex(line3 u,line3 v) { return dots_onplane(u.a,u.b,v.a,v.b)&&opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u); } //判線段與空間三角形相交,包括交於邊界和(部分)包含 bool intersect_in(line3 l,plane3 s) { return !same_side(l.a,l.b,s)&&!same_side(s.a,s.b,l.a,l.b,s.c)&&!same_side(s.b,s.c,l.a,l.b,s.a)&&!same_side(s.c,s.a,l.a,l.b,s.b); } //判線段與空間三角形相交,不包括交於邊界和(部分)包含 bool intersect_ex(line3 l,plane3 s) { return opposite_side(l.a,l.b,s)&&opposite_side(s.a,s.b,l.a,l.b,s.c)&&opposite_side(s.b,s.c,l.a,l.b,s.a)&&opposite_side(s.c,s.a,l.a,l.b,s.b); } //計算兩直線交點,注意事先判斷直線是否共面和平行! //線段交點請另外判線段相交(同時還是要判斷是否平行!) point3 intersection(line3 u,line3 v) { point3 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-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; ret.z+=(u.b.z-u.a.z)*t; return ret; } //計算直線與平面交點,注意事先判斷是否平行,並保證三點不共線! //線段和空間三角形交點請另外判斷 point3 intersection(line3 l,plane3 s) { point3 ret=pvec(s); double t=(ret.x*(s.a.x-l.a.x)+ret.y*(s.a.y-l.a.y)+ret.z*(s.a.z-l.a.z))/(ret.x*(l.b.x-l.a.x)+ret.y*(l.b.y-l.a.y)+ret.z*(l.b.z-l.a.z)); ret.x=l.a.x+(l.b.x-l.a.x)*t; ret.y=l.a.y+(l.b.y-l.a.y)*t; ret.z=l.a.z+(l.b.z-l.a.z)*t; return ret; } point3 intersection(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3) { point3 ret=pvec(s1,s2,s3); double t=(ret.x*(s1.x-l1.x)+ret.y*(s1.y-l1.y)+ret.z*(s1.z-l1.z))/ (ret.x*(l2.x-l1.x)+ret.y*(l2.y-l1.y)+ret.z*(l2.z-l1.z)); ret.x=l1.x+(l2.x-l1.x)*t; ret.y=l1.y+(l2.y-l1.y)*t; ret.z=l1.z+(l2.z-l1.z)*t; return ret; } //計算兩平面交線,注意事先判斷是否平行,並保證三點不共線! line3 intersection(plane3 u,plane3 v) { line3 ret; ret.a=parallel(v.a,v.b,u.a,u.b,u.c)?intersection(v.b,v.c,u.a,u.b,u.c):intersection(v.a,v.b,u.a,u.b,u.c); ret.b=parallel(v.c,v.a,u.a,u.b,u.c)?intersection(v.b,v.c,u.a,u.b,u.c):intersection(v.c,v.a,u.a,u.b,u.c); return ret; } line3 intersection(point3 u1,point3 u2,point3 u3,point3 v1,point3 v2,point3 v3) { line3 ret; ret.a=parallel(v1,v2,u1,u2,u3)?intersection(v2,v3,u1,u2,u3):intersection(v1,v2,u1,u2,u3); ret.b=parallel(v3,v1,u1,u2,u3)?intersection(v2,v3,u1,u2,u3):intersection(v3,v1,u1,u2,u3); return ret; } //點到直線距離 double ptoline(point3 p,line3 l) { return vlen(xmult(subt(p,l.a),subt(l.b,l.a)))/distance(l.a,l.b); } //點到平面距離 double ptoplane(point3 p,plane3 s) { return fabs(dmult(pvec(s),subt(p,s.a)))/vlen(pvec(s)); } //直線到直線距離 double linetoline(line3 u,line3 v) { point3 n=xmult(subt(u.a,u.b),subt(v.a,v.b)); return fabs(dmult(subt(u.a,v.a),n))/vlen(n); } //兩直線夾角cos 值 double angle_cos(line3 u,line3 v) { return dmult(subt(u.a,u.b),subt(v.a,v.b))/vlen(subt(u.a,u.b))/vlen(subt(v.a,v.b)); } //兩平面夾角cos 值 double angle_cos(plane3 u,plane3 v) { return dmult(pvec(u),pvec(v))/vlen(pvec(u))/vlen(pvec(v)); } //直線平面夾角sin 值 double angle_sin(line3 l,plane3 s) { return dmult(subt(l.a,l.b),pvec(s))/vlen(subt(l.a,l.b))/vlen(pvec(s)); } int main() { return 0; } #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) #define pi acos(-1.0) struct point { double x,y; }; struct line { point a,b; }; struct point3 { double x,y,z; }; struct line3 { point3 a,b; }; struct plane3 { point3 a,b,c; }; //計算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); } //計算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); } //計算cross product U . V point3 xmult(point3 u,point3 v) { point3 ret; ret.x=u.y*v.z-v.y*u.z; ret.y=u.z*v.x-u.x*v.z; ret.z=u.x*v.y-u.y*v.x; return ret; } //計算dot product U . V double dmult(point3 u,point3 v) { return u.x*v.x+u.y*v.y+u.z*v.z; } //兩點距離 double distance(point p1,point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } //判三點共線 bool dots_inline(point p1,point p2,point p3) { return zero(xmult(p1,p2,p3)); } //判點是否在線段上,包括端點 bool 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; } //判點是否在線段上,不包括端點 bool dot_online_ex(point p,line l) { return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y)); } //判兩點在線段同側,點在線段上返回0 bool same_side(point p1,point p2,line l) { return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)>eps; } //判兩點在線段異側,點在線段上返回0 bool opposite_side(point p1,point p2,line l) { return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)<-eps; } //判兩直線平行 bool parallel(line u,line v) { return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y)); } //判兩直線垂直 bool perpendicular(line u,line v) { return zero((u.a.x-u.b.x)*(v.a.x-v.b.x)+(u.a.y-u.b.y)*(v.a.y-v.b.y)); } //判兩線段相交,包括端點和部分重合 bool intersect_in(line u,line v) { if (!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b)) return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u); return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u); } //判兩線段相交,不包括端點和部分重合 bool intersect_ex(line u,line v) { return opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u); } //計算兩直線交點,注意事先判斷直線是否平行! //線段交點請另外判線段相交(同時還是要判斷是否平行!) 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-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 intersection(point u1,point u2,point 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; } //點到直線上的最近點 point ptoline(point p,line l) { point t=p; t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x; return intersection(p,t,l.a,l.b); } //點到直線距離 double disptoline(point p,line l) { return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b); } //點到線段上的最近點 point ptoseg(point p,line l) { point t=p; t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x; if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps) return distance(p,l.a)<distance(p,l.b)?l.a:l.b; return intersection(p,t,l.a,l.b); } //點到線段距離 double disptoseg(point p,line l) { point t=p; t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x; if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps) return distance(p,l.a)<distance(p,l.b)?distance(p,l.a):distance(p,l.b); return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b); } //矢量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; } //計算三角形面積,輸入三頂點 double area_triangle(point p1,point p2,point p3) { return fabs(xmult(p1,p2,p3))/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)); } //計算多邊形面積,頂點按順時針或逆時針給出 double area_polygon(int n,point* p) { double s1=0,s2=0; int i; for (i=0; i<n; i++) s1+=p[(i+1)%n].y*p[i].x,s2+=p[(i+1)%n].y*p[(i+2)%n].x; return fabs(s1-s2)/2; } //計算圓心角lat 表示緯度,-90<=w<=90,lng 表示經度 //返回兩點所在大圓劣弧對應圓心角,0<=angle<=pi double angle(double lng1,double lat1,double lng2,double lat2) { double dlng=fabs(lng1-lng2)*pi/180; while (dlng>=pi+pi) dlng-=pi+pi; if (dlng>pi) dlng=pi+pi-dlng; lat1*=pi/180,lat2*=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 (dlng>pi) 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,double lat2) { return r*angle(lng1,lat1,lng2,lat2); } //外心 point circumcenter(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.x=u.a.x-a.y+b.y; u.b.y=u.a.y+a.x-b.x; v.a.x=(a.x+c.x)/2; v.a.y=(a.y+c.y)/2; v.b.x=v.a.x-a.y+c.y; v.b.y=v.a.y+a.x-c.x; return intersection(u,v); } //內心 point incenter(point a,point b,point c) { line u,v; double m,n; u.a=a; m=atan2(b.y-a.y,b.x-a.x); n=atan2(c.y-a.y,c.x-a.x); u.b.x=u.a.x+cos((m+n)/2); u.b.y=u.a.y+sin((m+n)/2); v.a=b; m=atan2(a.y-b.y,a.x-b.x); n=atan2(c.y-b.y,c.x-b.x); v.b.x=v.a.x+cos((m+n)/2); v.b.y=v.a.y+sin((m+n)/2); return intersection(u,v); } //垂心 point perpencenter(point a,point b,point c) { line u,v; u.a=c; u.b.x=u.a.x-a.y+b.y; u.b.y=u.a.y+a.x-b.x; v.a=b; v.b.x=v.a.x-a.y+c.y; v.b.y=v.a.y+a.x-c.x; return intersection(u,v); } //重心 //到三角形三頂點距離的平方和最小的點 //三角形內到三邊距離之積最大的點 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 fermentpoint(point a,point b,point c) { point u,v; double step=fabs(a.x)+fabs(a.y)+fabs(b.x)+fabs(b.y)+fabs(c.x)+fabs(c.y); int i,j,k; u.x=(a.x+b.x+c.x)/3; u.y=(a.y+b.y+c.y)/3; while (step>1e-10) { for (k=0; k<10; step/=2,k++) { for (i=-1; i<=1; i++) { for (j=-1; j<=1; j++) { v.x=u.x+step*i; v.y=u.y+step*j; if(distance(u,a)+distance(u,b)+distance(u,c)>distance(v,a)+distance(v,b)+distance(v,c)) { u=v; } } } } } return u; } //矢量差 U - V point3 subt(point3 u,point3 v) { point3 ret; ret.x=u.x-v.x; ret.y=u.y-v.y; ret.z=u.z-v.z; return ret; } //取平面法向量 point3 pvec(plane3 s) { return xmult(subt(s.a,s.b),subt(s.b,s.c)); } point3 pvec(point3 s1,point3 s2,point3 s3) { return xmult(subt(s1,s2),subt(s2,s3)); } //兩點距離,單參數取向量大小 double distance(point3 p1,point3 p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z)); } ///三維/// //向量大小 double vlen(point3 p) { return sqrt(p.x*p.x+p.y*p.y+p.z*p.z); } //判三點共線 bool dots_inline(point3 p1,point3 p2,point3 p3) { return vlen(xmult(subt(p1,p2),subt(p2,p3)))<eps; } //判四點共面 bool dots_onplane(point3 a,point3 b,point3 c,point3 d) { return zero(dmult(pvec(a,b,c),subt(d,a))); } //判點是否在線段上,包括端點和共線 bool dot_online_in(point3 p,line3 l) { return zero(vlen(xmult(subt(p,l.a),subt(p,l.b))))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps&&(l.a.z-p.z)*(l.b.z-p.z)<eps; } //判點是否在線段上,不包括端點 bool dot_online_ex(point3 p,line3 l) { return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y)||!zero(p.z-l.a.z))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y)||!zero(p.z-l.b.z)); } //判點是否在空間三角形上,包括邊界,三點共線無意義 bool dot_inplane_in(point3 p,plane3 s) { return zero(vlen(xmult(subt(s.a,s.b),subt(s.a,s.c)))-vlen(xmult(subt(p,s.a),subt(p,s.b)))-vlen(xmult(subt(p,s.b),subt(p,s.c)))-vlen(xmult(subt(p,s.c),subt(p,s.a)))); } //判點是否在空間三角形上,不包括邊界,三點共線無意義 bool dot_inplane_ex(point3 p,plane3 s) { return dot_inplane_in(p,s)&&vlen(xmult(subt(p,s.a),subt(p,s.b)))>eps&&vlen(xmult(subt(p,s.b),subt(p,s.c)))>eps&&vlen(xmult(subt(p,s.c),subt(p,s.a)))>eps; } //判兩點在線段同側,點在線段上返回0,不共面無意義 bool same_side(point3 p1,point3 p2,line3 l) { return dmult(xmult(subt(l.a,l.b),subt(p1,l.b)),xmult(subt(l.a,l.b),subt(p2,l.b)))>eps; } //判兩點在線段異側,點在線段上返回0,不共面無意義 bool opposite_side(point3 p1,point3 p2,line3 l) { return dmult(xmult(subt(l.a,l.b),subt(p1,l.b)),xmult(subt(l.a,l.b),subt(p2,l.b)))<-eps; } //判兩點在平面同側,點在平面上返回0 bool same_side(point3 p1,point3 p2,plane3 s) { return dmult(pvec(s),subt(p1,s.a))*dmult(pvec(s),subt(p2,s.a))>eps; } bool same_side(point3 p1,point3 p2,point3 s1,point3 s2,point3 s3) { return dmult(pvec(s1,s2,s3),subt(p1,s1))*dmult(pvec(s1,s2,s3),subt(p2,s1))>eps; } //判兩點在平面異側,點在平面上返回0 bool opposite_side(point3 p1,point3 p2,plane3 s) { return dmult(pvec(s),subt(p1,s.a))*dmult(pvec(s),subt(p2,s.a))<-eps; } bool opposite_side(point3 p1,point3 p2,point3 s1,point3 s2,point3 s3) { return dmult(pvec(s1,s2,s3),subt(p1,s1))*dmult(pvec(s1,s2,s3),subt(p2,s1))<-eps; } //判兩直線平行 bool parallel(line3 u,line3 v) { return vlen(xmult(subt(u.a,u.b),subt(v.a,v.b)))<eps; } //判兩平面平行 bool parallel(plane3 u,plane3 v) { return vlen(xmult(pvec(u),pvec(v)))<eps; } //判直線與平面平行 bool parallel(line3 l,plane3 s) { return zero(dmult(subt(l.a,l.b),pvec(s))); } bool parallel(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3) { return zero(dmult(subt(l1,l2),pvec(s1,s2,s3))); } //判兩直線垂直 bool perpendicular(line3 u,line3 v) { return zero(dmult(subt(u.a,u.b),subt(v.a,v.b))); } //判兩平面垂直 bool perpendicular(plane3 u,plane3 v) { return zero(dmult(pvec(u),pvec(v))); } //判直線與平面平行 bool perpendicular(line3 l,plane3 s) { return vlen(xmult(subt(l.a,l.b),pvec(s)))<eps; } //判兩線段相交,包括端點和部分重合 bool intersect_in(line3 u,line3 v) { if (!dots_onplane(u.a,u.b,v.a,v.b)) return 0; if (!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b)) return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u); return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u); } //判兩線段相交,不包括端點和部分重合 bool intersect_ex(line3 u,line3 v) { return dots_onplane(u.a,u.b,v.a,v.b)&&opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u); } //判線段與空間三角形相交,包括交於邊界和(部分)包含 bool intersect_in(line3 l,plane3 s) { return !same_side(l.a,l.b,s)&&!same_side(s.a,s.b,l.a,l.b,s.c)&&!same_side(s.b,s.c,l.a,l.b,s.a)&&!same_side(s.c,s.a,l.a,l.b,s.b); } //判線段與空間三角形相交,不包括交於邊界和(部分)包含 bool intersect_ex(line3 l,plane3 s) { return opposite_side(l.a,l.b,s)&&opposite_side(s.a,s.b,l.a,l.b,s.c)&&opposite_side(s.b,s.c,l.a,l.b,s.a)&&opposite_side(s.c,s.a,l.a,l.b,s.b); } //計算兩直線交點,注意事先判斷直線是否共面和平行! //線段交點請另外判線段相交(同時還是要判斷是否平行!) point3 intersection(line3 u,line3 v) { point3 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-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; ret.z+=(u.b.z-u.a.z)*t; return ret; } //計算直線與平面交點,注意事先判斷是否平行,並保證三點不共線! //線段和空間三角形交點請另外判斷 point3 intersection(line3 l,plane3 s) { point3 ret=pvec(s); double t=(ret.x*(s.a.x-l.a.x)+ret.y*(s.a.y-l.a.y)+ret.z*(s.a.z-l.a.z))/(ret.x*(l.b.x-l.a.x)+ret.y*(l.b.y-l.a.y)+ret.z*(l.b.z-l.a.z)); ret.x=l.a.x+(l.b.x-l.a.x)*t; ret.y=l.a.y+(l.b.y-l.a.y)*t; ret.z=l.a.z+(l.b.z-l.a.z)*t; return ret; } point3 intersection(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3) { point3 ret=pvec(s1,s2,s3); double t=(ret.x*(s1.x-l1.x)+ret.y*(s1.y-l1.y)+ret.z*(s1.z-l1.z))/ (ret.x*(l2.x-l1.x)+ret.y*(l2.y-l1.y)+ret.z*(l2.z-l1.z)); ret.x=l1.x+(l2.x-l1.x)*t; ret.y=l1.y+(l2.y-l1.y)*t; ret.z=l1.z+(l2.z-l1.z)*t; return ret; } //計算兩平面交線,注意事先判斷是否平行,並保證三點不共線! line3 intersection(plane3 u,plane3 v) { line3 ret; ret.a=parallel(v.a,v.b,u.a,u.b,u.c)?intersection(v.b,v.c,u.a,u.b,u.c):intersection(v.a,v.b,u.a,u.b,u.c); ret.b=parallel(v.c,v.a,u.a,u.b,u.c)?intersection(v.b,v.c,u.a,u.b,u.c):intersection(v.c,v.a,u.a,u.b,u.c); return ret; } line3 intersection(point3 u1,point3 u2,point3 u3,point3 v1,point3 v2,point3 v3) { line3 ret; ret.a=parallel(v1,v2,u1,u2,u3)?intersection(v2,v3,u1,u2,u3):intersection(v1,v2,u1,u2,u3); ret.b=parallel(v3,v1,u1,u2,u3)?intersection(v2,v3,u1,u2,u3):intersection(v3,v1,u1,u2,u3); return ret; } //點到直線距離 double ptoline(point3 p,line3 l) { return vlen(xmult(subt(p,l.a),subt(l.b,l.a)))/distance(l.a,l.b); } //點到平面距離 double ptoplane(point3 p,plane3 s) { return fabs(dmult(pvec(s),subt(p,s.a)))/vlen(pvec(s)); } //直線到直線距離 double linetoline(line3 u,line3 v) { point3 n=xmult(subt(u.a,u.b),subt(v.a,v.b)); return fabs(dmult(subt(u.a,v.a),n))/vlen(n); } //兩直線夾角cos 值 double angle_cos(line3 u,line3 v) { return dmult(subt(u.a,u.b),subt(v.a,v.b))/vlen(subt(u.a,u.b))/vlen(subt(v.a,v.b)); } //兩平面夾角cos 值 double angle_cos(plane3 u,plane3 v) { return dmult(pvec(u),pvec(v))/vlen(pvec(u))/vlen(pvec(v)); } //直線平面夾角sin 值 double angle_sin(line3 l,plane3 s) { return dmult(subt(l.a,l.b),pvec(s))/vlen(subt(l.a,l.b))/vlen(pvec(s)); } int main() { return 0; }