转自《工程计算可视化与MATLAB实现》尚涛等编著 武汉大学出版社
两直线相交
function [X,Y]=pll(X1,Y1,X2,Y2)
% 直线相交求交点
A1=Y1(1)-Y1(2);
B1=X1(2)-X1(1);
C1=Y1(2)*X1(1)-Y1(1)*X1(2);
A2=Y2(1)-Y2(2);
B2=X2(2)-X2(1);
C2=Y2(2)*X2(1)-Y2(1)*X2(2);
D=det([A1,B1;A2,B2]);
X=det([-C1 B1;-C2 B2])/D;
Y=det([A1 -C1;A2,-C2])/D;
调用格式:
x1=[1 5];y1=[1 5];x2=[1 5];y2=[5,1];
[x,y]=pll(x1,y1,x2,y2);
plot(x1,y1,\'r\');
hold on
plot(x2,y2,\'b\');
plot(x,y,\'ko\');
直线与多条直线相交
xi=[1 2 3 4 5];yi=[2 6 3 6 1];
plot(xi,yi);hold on
x1=[1 5];y1=[4 5];line(x1,y1);
x=zeros(size(xi));
y=x;
for i=1:5-1
x2=xi([i i+1]);y2=yi([i i+1]);
[x,y]=pll(x1,y1,x2,y2);
plot(x,y,\'ro\')
end
直线与曲线相交
x=-8:0.1:8;y=x;[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;Z=sin(R)./R;
contour(Z,3);hold on
c=contour(Z,3);
x=[0 360];y=[0 400];
y=(y(2)-y(1))/(x(2)-x(1))*(x-x(1))+y(1);z=[0 0];
line(x,y,z);c=c\';
X=c(:,1);Y=c(:,2);
r0=abs(Y-(y(2)-y(1))/(x(2)-x(1))*(X-x(1))+y(1))<=.93;
zz=0;yy=r0.*Y;xx=r0.*X;
plot(xx(r0~=0),yy(r0~=0),\'r\')
曲线与曲线相交
x=0:pi/400:2*pi;
x=x\';
y1=sin(pi*x);y2=cos(pi*x);plot(x,y1,x,y2);hold on
r0=abs(y2-sin(pi*x))<=0.02;
yy=r0.*y1;xx=r0.*x;plot(xx(r0~=0),yy(r0~=0),\'r.\')
直线与曲面相交
x=-8:0.3:8;y=x;[X,Y]=meshgrid(x,y);
Z=X.^2+Y.^2;
mesh(X,Y,Z);hold on
x=[-10 10];y=[-10 3];z=[30 35];line(x,y,z);
r0=(abs(Y-y(1)-(y(2)-y(1))/(x(2)-x(1))*(X-x(1)))<=0.45)&...
(abs(Z-z(1)-(z(2)-z(1))/(x(2)-x(1))*(X-x(1)))<0.45)&...
(abs(Y-y(1)-(y(2)-y(1))/(z(2)-z(1))*(Z-z(1)))<=0.45);
zz=r0.*Z;yy=r0.*Y;xx=r0.*X;
plot3(xx(r0~=0),yy(r0~=0),zz(r0~=0),\'r*\');
平面与曲面相交
x=-8:0.1:8;
y=x;
[X,Y]=meshgrid(x,y);
Z1=2*ones(size(X));
Z2=X.^2-Y.^2;
mesh(X,Y,Z1);
hold on
mesh(X,Y,Z2);
r0=(abs(Z1-Z2)<=.65);
zz=r0.*Z1;yy=r0.*Y;xx=r0.*X;
plot3(xx(r0~=0),yy(r0~=0),zz(r0~=0),\'k*\')
clc
disp(\'观察曲面后,按任意键画交线\');
pause
clf
plot3(xx(r0~=0),yy(r0~=0),zz(r0~=0),\'k*\');
%曲面与多个截平面相交
y=-10:0.5:10;
z=y;
[Z,Y]=meshgrid(z,y);
X=Z;
X1=0*ones(size(Z));
X2=3*ones(size(Z));
X3=-3*ones(size(Z));
Z4=(X.^2-Y.^2)/10;
mesh(X1,Y,Z);hold on
mesh(X2,Y,Z)
mesh(X3,Y,Z);
mesh(X,Y,Z4);
r1=(abs(X1-X)<0.05);
r2=(abs(X2-X)<0.05);
r3=(abs(X3-X)<0.05);
zz1=r1.*Z4;yy1=r1.*Y;xx1=r1.*X;
zz2=r2.*Z4;yy2=r1.*Y;xx2=r1.*X;
zz3=r3.*Z4;yy3=r1.*Y;xx3=r1.*X;
plot3(xx1(r1~=0),yy1(r1~=0),zz1(r1~=0),\'k*\');
plot3(xx2(r2~=0),yy2(r2~=0),zz2(r2~=0),\'k*\');
plot3(xx3(r3~=0),yy3(r3~=0),zz3(r3~=0),\'k*\');
colormap(hsv)
clc;
disp(\'观察曲面后,按任意键画交线\');
hold off
平面与曲面相交
y=-8:0.4:8;
z=y;
[Z,Y]=meshgrid(z,y);
X=Z;
X1=zeros(size(Z));
Z2=zeros(size(Z));
Z3=(X.^2-Y.^2)/10;
mesh(X1,Y,Z);
hold on
mesh(X,Y,Z2);
mesh(X,Y,Z3);
r1=(abs(X1-X)<0.05);
r2=(abs(Z3-Z2)<0.05);
r3=(abs(X1-X)<0.05)&(abs(Z-Z2)<=0.05);
zz1=r1.*Z3;yy1=r1.*Y;xx1=r1.*X;
zz2=r2.*Z3;yy2=r2.*Y;xx2=r2.*X;
zz3=r3.*Z;yy1=r3.*Y;xx1=r3.*X1;
plot3(xx1(r1~=0),yy1(r1~=0),zz1(r1~=0),\'k*\');
plot3(xx2(r2~=0),yy2(r2~=0),zz2(r2~=0),\'k*\');
plot3(xx3(r3~=0),yy3(r3~=0),zz3(r3~=0),\'k*\');
colormap(hsv);
曲面与曲面求交
[x,y]=meshgrid(-2:.1:2);
z1=x.^2-2*y.^2;
z2=x.^2+y.^2-5;
mesh(x,y,z1);
hold on
mesh(x,y,z2);
r0=(abs(z1-z2)<=.1);
zz=r0.*z1;yy=r0.*y;xx=r0.*x;
plot3(xx(r0~=0),yy(r0~=0),zz(r0~=0),\'k*\')
colormap(cool)
hold off
[ 本帖最后由 happy 于 2006-10-24 20:00 编辑 ] |