簡體   English   中英

如何在 MATLAB/OCTAVE 中創建一個帶孔的矩形?

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

我想在 MATLAB 或 OCTAVE 中准確地繪制/繪制這個形狀。 當然我知道如何 plot,以及如何使用 plot、直線或矩形函數創建矩形。 但我還沒有設法在矩形的頂部添加這個“洞”。 我想,它是一個半徑為 0.5 和中心點 (1.5|2) 的(半)圓。 在 OCTAVE 中,有一個drawCircleArc function,但我不想只畫那個東西,還想有必要的坐標來定義整個形狀以便進一步操作。

帶有“孔”的所需矩形。

這是一種方式(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 開始,您還可以使用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