简体   繁体   English

如何在 MATLAB/OCTAVE 中创建一个带孔的矩形?

[英]How can I create a rectangle with a hole in MATLAB/OCTAVE?

I would like to plot/draw exactly this shape in MATLAB or OCTAVE.我想在 MATLAB 或 OCTAVE 中准确地绘制/绘制这个形状。 Of course I do know how to plot, and how to create rectangles, using either the plot, the line or the rectangle functions.当然我知道如何 plot,以及如何使用 plot、直线或矩形函数创建矩形。 But I have not yet managed to add this "hole" on the top side of the rectangle.但我还没有设法在矩形的顶部添加这个“洞”。 I figure, it's a (half-)circle of radius 0.5 and center point (1.5|2).我想,它是一个半径为 0.5 和中心点 (1.5|2) 的(半)圆。 In OCTAVE, there is a drawCircleArc function, but I don't want to only draw that thing, but also to have the necessary coordinates defining the whole shape for further manipulation.在 OCTAVE 中,有一个drawCircleArc function,但我不想只画那个东西,还想有必要的坐标来定义整个形状以便进一步操作。

带有“孔”的所需矩形。

Here is one way (matlab/octave compatible) :这是一种方式(matlab/octave 兼容)

% Specify all polygon points, excluding the semi-circle outline
  X = [1, 0, 0, 3, 3, 2];
  Y = [2, 2, 0, 0, 2, 2];

% Add semi-circle outline to array of polygon points
  t = 0 : -0.01 : -pi;
  X = [X, 1.5 + 0.5 * cos(t)];
  Y = [Y, 2   + 0.5 * sin(t)];

% Use fill to plot the filled polygon, with desired settings
  fill( X, Y, [0.8, 0.8, 0.8], 'linewidth', 1.5 );
  axis( [-2, 4, -2, 4] );   axis equal;

As of 2017b you can also use polyshape s and boolean operators .从 2017b 开始,您还可以使用polyshapeboolean 运算符

rect = polyshape([0 3 3 0], [0 0 2 2]);
t = linspace(0, 2*pi, 32);
circ = polyshape(1.5+.5*cos(t), 2+.5*sin(t));
subplot 121, hold on
plot(rect)
plot(circ)
axis equal

shape = subtract(rect, circ);
subplot 122
plot(shape)
axis equal

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM