|
午夜流星很早以前写的,可以参考:
- function tangency(x1,y1,r1,x2,y2,r2)
- % x1,y1,r1,x2,y2,r2为两圆的圆心坐标与半径
- R=sqrt((y2-y1)^2+(x2-x1)^2); %圆心距
- %外切线
- r0=R*r2/(r1-r2); %切线与两圆心连线的交点与第个圆心的矢量距离
- syms x y
- [x,y]=solve((y-y2)^2+(x-x2)^2-r0^2,y*(x2-x1)-x*(y2-y1)-x2*y1+x1*y2);% 求解焦点
- a=asin(r2/r0); %切线与两圆心连线的夹角
- b=atan((y2-y1)/(x2-x1));%两圆心连线与x轴的夹角
- %画圆
- t=0:0.01:2*pi;
- x11=x1+r1*cos(t);
- y11=y1+r1*sin(t);
- x22=x2+r2*cos(t);
- y22=y2+r2*sin(t);
- plot(x11,y11 ,x22,y22)
- hold on
- % 画切线
- if r1>r2
- xe=x(2);
- ye=y(2);
- xx=double([x1,xe]);
- else
- xe=x(1);
- ye=y(1);
- xx=double([xe,x2]);
- end
- yy1=double(tan(a-b)*(xe-xx)+ye);%两条切线
- yy2=double(tan(-a-b)*(xe-xx)+ye);
- plot(xx,yy1,xx,yy2)
- hold on
- axis equal
- %内切线,意义同上
- if R>=r1+r2
- r0=R*r2/(r1+r2);
- syms x y
- [x,y]=solve((y-y2)^2+(x-x2)^2-r0^2,y*(x2-x1)-x*(y2-y1)-x2*y1+x1*y2);
- a=asin(r2/r0);
- b=atan((y2-y1)/(x2-x1));
- xe=x(1);
- ye=y(1);
- xx=double([x1,x2]);
- yy1=double(tan(a-b)*(xe-xx)+ye);
- yy2=double(tan(-a-b)*(xe-xx)+ye);
- plot(xx,yy1,xx,yy2)
- hold
- end
- axis equal
- axis([-2*abs(min(x1-r1,x2-r2)),2*abs(max(x1+r1,x2+r2)),-2*abs(min(y1-r1,y2-r2)),2*abs(max(y1+r1,y2+r2))])
复制代码 |
评分
-
1
查看全部评分
-
|