1
votes

I would like to plot/draw exactly this shape in MATLAB or OCTAVE. Of course I do know how to plot, and how to create rectangles, using either the plot, the line or the rectangle functions. 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). 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.

The desired rectangle with a "hole".

2
What's the equation for the circumference with a given radius and given center point? Once you get that, remove the points above the center point and now you have your lower semi-circumference.Matteo V
but also to have the necessary coordinates defining the whole shape Do you mean sampled coordinates (say with step 0.01 in x) of the straight lines and the semicircle? Or a pixel map of the inner area (again with a given step)? Or just 5 parameters defining rectangle corners and circle radius?Luis Mendo
@LuisMendo: That is not particularly important to me. I'd just like to be able to transform the shape, but this is possible either way. I figure for the rectangle the corners would suffice, while for the semi-circle a more detailed sampling will be needed.Michael

2 Answers

2
votes

Here is one way (matlab/octave compatible):

% 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;
2
votes

As of 2017b you can also use 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

enter image description here