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

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

我想 plot/draw 在 MATLAB 或 OCTAVE 中准确地计算出这个形状。当然,我确实知道如何绘图,以及如何使用绘图、直线或矩形函数创建矩形。但是我还没有设法在矩形的顶部添加这个“洞”。我想,它是一个半径为 0.5 且中心点为 (1.5|2) 的(半)圆。在 OCTAVE 中,有一个 drawCircleArc 功能,但我不想只画那个东西,还想有必要的坐标来定义整个形状以供进一步操作。

这是一种方式(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;

自 2017b 起,您还可以使用 polyshapes and boolean operators

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