MATLAB:如何在三维空间中绘制一个矩形

huangapple go评论102阅读模式
英文:

MATLAB: How to plot a rectangle in 3d space

问题

我有以下示例数据:

  1. x = [2;4;6;8;10]
  2. y = [10;20;30;40;50]
  3. z = [5;5;5;5;5]

我试图在三维空间中绘制2D x/y 曲面,但由于 z 值是常数,我似乎无法弄清如何实现。我尝试过的所有示例都不起作用,因为这些点是共线的。

谢谢。

英文:

I have the following example data:

  1. x = [2;4;6;8;10]
  2. y = [10;20;30;40;50]
  3. z = [5;5;5;5;5]

I am trying to plot a the 2d x/y surface in 3d space but since z is constant I can't seem to figure out how to do it. All examples I've tried to follow don't work since the points are colinear.

Thanks

答案1

得分: 2

在MATLAB中,有不同的方法来生成一个**'3D矩形',也被称为平行六面体**,Wolfram定义

1.- 定义所有的实体点

  1. xrange=[0:1:10];
  2. yrange=[0:1:20];
  3. zrange=[0:1:5];
  4. [x,y,z]=meshgrid(xrange,yrange,zrange);
  5. x=x(:);y=y(:);z=z(:);
  6. P=[x y z];
  7. figure(1);
  8. plot3(P(:,1),P(:,2),P(:,3),'.');
  9. axis equal
  10. grid on
  11. shp1=alphaShape(P(:,1),P(:,2),P(:,3))
  12. figure(2);
  13. plot(shp1)
  14. axis equal

一旦建立了alphaShape,可以使用以下volumesurfaceArea命令:

  1. round(volume(shp1))

  1. (xrange(end)-xrange(1))*(yrange(end)-yrange(1))*(zrange(end)-zrange(1))

以及

  1. surfaceArea(shp1)

  1. 2*xrange(end)*yrange(end)+2*xrange(end)*zrange(end)+2*yrange(end)*zrange(end)

2.- 仅使用顶点

  1. xrange2=[0 10];
  2. yrange2=[0 20];
  3. zrange2=[0 5];
  4. [x2,y2,z2]=meshgrid(xrange2,yrange2,zrange2);
  5. x2=[x2(:);5];
  6. y2=[y2(:);10];
  7. z2=[z2(:);2.5];
  8. P2=[x2 y2 z2];
  9. Tes=delaunayn(P2);
  10. figure(3)
  11. tetramesh(Tes,P2);
  12. camorbit(40,0);

3.- 使用patch命令

  1. v1=[0 0 0;10 0 0;10 20 0;0 20 0
  2. 0 0 5;10 0 5;10 20 5;0 20 5];
  3. f1=[1 2 3 4 1;5 6 7 8 5];
  4. v2=[0 0 0;0 20 0;0 20 5;0 0 5
  5. 10 0 0;10 20 0;10 20 5;10 0 5];
  6. f2=[1 2 3 4 1;5 6 7 8 5];
  7. v3=[0 0 0;10 0 0;10 0 5;0 0 5
  8. 0 20 0;10 20 0;10 20 5;0 20 5];
  9. f3=[1 2 3 4 1;5 6 7 8 5];
  10. figure(4);
  11. hp1=patch('Faces',f1,'Vertices',v1)
  12. hp1.FaceColor=[0 .5 0];
  13. hp1.FaceAlpha=.5;
  14. hp1.EdgeColor='none';
  15. hp2=patch('Faces',f2,'Vertices',v2)
  16. hp2.FaceColor=[.5 0 0];
  17. hp2.FaceAlpha=.5;
  18. hp2.EdgeColor='none';
  19. hp3=patch('Faces',f3,'Vertices',v3)
  20. hp3.FaceColor=[0 0 .5];
  21. hp3.FaceAlpha=.5;
  22. hp3.EdgeColor='none';
  23. grid on
  24. xlabel('x');ylabel('y');zlabel('z');
  25. campos([477 -1122 120])

4.- 拓展一个2D矩形

extrude命令需要一个几何文件作为输入。

在MATLAB中,几何文件具有以下等效扩展名:.stl.step.ste.stl文件可以使用SolidWorks等CAD应用程序生成。

在这里,由于案例很简单,不需要SolidWorks,可以直接生成所需的.stl文件。

接下来,我生成一个三角剖分网格并将其保存为.stl文件,然后使用importGeometry命令构建一个可以作为extrude命令的输入的2D矩形

  1. P=[0 0;10 0;10 20;0 20]; % points
  2. T=[1 2 4;2 3 4]; % triangles definition
  3. TR=triangulation(T,P)
  4. figure(5)
  5. triplot(TR)
  6. axis equal
  7. grid on
  8. xlabel('x');ylabel('y');
  9. stlwrite(TR,'base1.stl','text')

这是base1.stl的内容:

  1. solid MATLAB_1
  2. facet normal 0 0 1
  3. outer loop
  4. vertex 0 0 0
  5. vertex 10 0 0
  6. vertex 0 20 0
  7. endloop
  8. endfacet
  9. facet normal 0 -0 1
  10. outer loop
  11. vertex 10 0 0
  12. vertex 10 20 0
  13. vertex 0 20 0
  14. endloop
  15. endfacet
  16. endsolid MATLAB_1

现在进行挤压:

  1. model = createpde;
  2. b1 = importGeometry(model,'base1.stl');
  3. figure(6)
  4. pdegplot(b1,'FaceLabels','on')
  5. extrude(b1,5)
  6. pdegplot(b1,'FaceLabels','on','FaceAlpha',0.5)
  7. grid on
  8. xlabel('x');ylabel('y');zlabel('z');
英文:

In MATLAB there are different was to generate a '3D rectangle' also known as parallelepiped, Wolfram definition.

1.- Defining all solid points

  1. xrange=[0:1:10];
  2. yrange=[0:1:20];
  3. zrange=[0:1:5];
  4. [x,y,z]=meshgrid(xrange,yrange,zrange);
  5. x=x(:);y=y(:);z=z(:);
  6. P=[x y z];
  7. figure(1);
  8. plot3(P(:,1),P(:,2),P(:,3),'.');
  9. axis equal
  10. grid on

MATLAB:如何在三维空间中绘制一个矩形

  1. shp1=alphaShape(P(:,1),P(:,2),P(:,3))
  2. figure(2);
  3. plot(shp1)
  4. axis equal

MATLAB:如何在三维空间中绘制一个矩形

once an alphaShape is built the following volume and surfaceArea commands can be used :

  1. round(volume(shp1))

same as

  1. (xrange(end)-xrange(1))*(yrange(end)-yrange(1))*(zrange(end)-zrange(1))
  2. surfaceArea(shp1)

same as

  1. 2*xrange(end)*yrange(end)+2*xrange(end)*zrange(end)+2*yrange(end)*zrange(end)

2.- Just using vertices

  1. xrange2=[0 10];
  2. yrange2=[0 20];
  3. zrange2=[0 5];
  4. [x2,y2,z2]=meshgrid(xrange2,yrange2,zrange2);
  5. x2=[x2(:);5];
  6. y2=[y2(:);10];
  7. z2=[z2(:);2.5];
  8. P2=[x2 y2 z2];
  9. Tes=delaunayn(P2);
  10. figure(3)
  11. tetramesh(Tes,P2);
  12. camorbit(40,0);

MATLAB:如何在三维空间中绘制一个矩形

3.- With command patch

  1. v1=[0 0 0;10 0 0;10 20 0;0 20 0
  2. 0 0 5;10 0 5;10 20 5;0 20 5];
  3. f1=[1 2 3 4 1;5 6 7 8 5];
  4. v2=[0 0 0;0 20 0;0 20 5;0 0 5
  5. 10 0 0;10 20 0;10 20 5;10 0 5];
  6. f2=[1 2 3 4 1;5 6 7 8 5];
  7. v3=[0 0 0;10 0 0;10 0 5;0 0 5
  8. 0 20 0;10 20 0;10 20 5;0 20 5];
  9. f3=[1 2 3 4 1;5 6 7 8 5];
  10. figure(4);
  11. hp1=patch('Faces',f1,'Vertices',v1)
  12. hp1.FaceColor=[0 .5 0];
  13. hp1.FaceAlpha=.5;
  14. hp1.EdgeColor='none';
  15. hp2=patch('Faces',f2,'Vertices',v2)
  16. hp2.FaceColor=[.5 0 0];
  17. hp2.FaceAlpha=.5;
  18. hp2.EdgeColor='none';
  19. hp3=patch('Faces',f3,'Vertices',v3)
  20. hp3.FaceColor=[0 0 .5];
  21. hp3.FaceAlpha=.5;
  22. hp3.EdgeColor='none';
  23. grid on
  24. xlabel('x');ylabel('y');zlabel('z');
  25. campos([477 -1122 120])

MATLAB:如何在三维空间中绘制一个矩形

4.- Extruding a 2D rectangle

Command extrude requires as import a geometry file.

In MATLAB geometry files have these equivalent extensions : .stl .step and .ste
.stl and can be generated with CAD applications like SolidWorks.

Here, given how simple is the case, there's no need for SolidWorks, one can directly generate the required .stl file directly.

Following, I generate a triangulation mesh and save it to .stl to then use command importGeometry to build a 2D rectangle that has the right type to be used as input to command extrude.

  1. P=[0 0;10 0;10 20;0 20]; % points
  2. T=[1 2 4;2 3 4]; % triangles definition
  3. TR=triangulation(T,P)
  4. figure(5)
  5. triplot(TR)
  6. axis equal
  7. grid on
  8. xlabel('x');ylabel('y');

MATLAB:如何在三维空间中绘制一个矩形

  1. stlwrite(TR,'base1.stl','text')

this is the contents of base1.stl :

  1. solid MATLAB_1
  2. facet normal 0 0 1
  3. outer loop
  4. vertex 0 0 0
  5. vertex 10 0 0
  6. vertex 0 20 0
  7. endloop
  8. endfacet
  9. facet normal 0 -0 1
  10. outer loop
  11. vertex 10 0 0
  12. vertex 10 20 0
  13. vertex 0 20 0
  14. endloop
  15. endfacet
  16. endsolid MATLAB_1

now for the extrusion :

  1. model = createpde;
  2. b1 = importGeometry(model,'base1.stl');
  3. figure(6)
  4. pdegplot(b1,'FaceLabels','on')
  5. extrude(b1,5)
  6. pdegplot(b1,'FaceLabels','on','FaceAlpha',0.5)
  7. grid on
  8. xlabel('x');ylabel('y');zlabel('z');

MATLAB:如何在三维空间中绘制一个矩形

huangapple
  • 本文由 发表于 2023年6月16日 00:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定