一. 将隐式函数化作参数形式,如球面,x2 +y2 +z2 =R2
- ezsurf
sphere.m
function sphere(R)
x=[num2str(R),'*sin(s)*cos(t)'];
y=[num2str(R),'*sin(s)*sin(t)'];
z=[num2str(R),'*cos(s)'];
ezsurf(x,y,z,[0,pi,0,2*pi]);
axis square;
shading interp;
colormap(spring);
view(10,40)
在命令行中执行命令sphere(r)就可以得到半径为r的球形了
- 转化成参数方程后,用mesh或surf画
% x,y,z are equally sized matrices with coordinates.
s=0:0.05:10;
t=-0.05:0.05:2*pi;%取-0.05 使得圆锥体没有缝隙
[s,t]=meshgrid(s,t);
x=(s+10).*cos(t);
y=(s+10).*sin(t);
z=s;
mesh(x,y,z);
saveobjmesh('temp.obj',x,y,z);
二. 利用isosurface, 可将隐式函数直接画出来
1. isosurface与patch连用,可将曲面显示出来
Example: 在同一个图内,画出球面,圆柱面
x=-10:0.1:10;y=x;z=x;
[x,y,z]=meshgrid(x,y,z);
f1=x.^2+y.^2+z.^2-4;
f2=x.^2+y.^2-1;
f3=x.^2+z.^2-1;
f4=z.^2+y.^2-1;
p1=patch(isosurface(x,y,z,f1,0));
set(p1, 'FaceColor', 'b', 'EdgeColor', 'none');
p2=patch(isosurface(x,y,z,f2,0));
set(p2, 'FaceColor', 'r', 'EdgeColor', 'none');
p3=patch(isosurface(x,y,z,f3,0));
set(p3, 'FaceColor', 'y', 'EdgeColor', 'none');
p4=patch(isosurface(x,y,z,f4,0));
set(p4, 'FaceColor', 'h', 'EdgeColor', 'none');
daspect([1 1 1])
view(3); axis tight
camlight;
lighting phong
2. 将isosurface输出.obj文件
首先,先写vertface2obj.m
function vertface2obj(v,f,name)
% VERTFACE2OBJ Save a set of vertice coordinates and faces as a Wavefront/Alias Obj file
% VERTFACE2OBJ(v,f,fname)
% v is a Nx3 matrix of vertex coordinates.
% f is a Mx3 matrix of vertex indices.
% fname is the filename to save the obj file.
fid = fopen(name,'w');
for i=1:size(v,1)
fprintf(fid,'v %f %f %f/n',v(i,1),v(i,2),v(i,3));
end
fprintf(fid,'g foo/n');
for i=1:size(f,1);
fprintf(fid,'f %d %d %d/n',f(i,1),f(i,2),f(i,3));
end
fprint