How to make Rounded Edges of rectangle corners with polyshape function ?
33 views (last 30 days)
Show older comments
hello, everybody
I would like to make the rounded rectangle and I would like them to translated and rotated.
First I tried with rectangle function using 'Curvature' of 1. then I can make the rounded rectangle.
However, it is not possible for them to translated and rotated.
Therefore, I made the rectangle with polyshape function. and it is okay for them to translated and rotated.
However, i do not know how I can make them to have rounded edges.
Could you please helpe me how to make them to rounded edges with polyshape function?
clear; close all; clc;
figure(1); hold on; axis equal
% Before translating and rotating
rectangle('Position',[-14.1276, 226.1976, 6.5929, 9.4184],'FaceColor','r','Curvature',1)
% After translating and rotating
% translate and rotate polygon
x = [-14.1276; -14.1276+6.5929; -14.1276+6.5929; -14.1276];
y = [226.1976; 226.1976; 226.1976+9.4184; 226.1976+9.4184];
p = [x,y];
pgon = polyshape(p);
pgon = translate(pgon,50,-75);
pgon = rotate(pgon,18,[57, 349]);
plot(pgon,'FaceColor','red','FaceAlpha',1)
% object by rectangle function can not translated and rotated.
1 Comment
Stephen23
on 28 Jun 2025
"However, it is not possible for them to translated and rotated."
fh0 = figure();
rh0 = rectangle('Position',[-14.1276, 226.1976, 6.5929, 9.4184],'FaceColor','r','Curvature',1);
axis equal
fh1 = figure();
ax1 = axes(fh1);
rh1 = copyobj(rh0,ax1);
axis equal
c = [57,349];
m = makehgtform('translate',[50,-75,0],...
'translate',[-c,0], 'zrotate',deg2rad(18), 'translate',[c,0]);
ht = hgtransform('Matrix',m);
set(rh1, 'Parent',ht)
Accepted Answer
DGM
on 19 Dec 2022
Edited: DGM
on 28 Jun 2025
The rotate() function only works on certain types of graphics objects, rectangles not included, You can use hgtransform() on rectangle() objects, though.
Alternatively, the following can be used to generate a vertex list that corresponds to the input syntax supported by rectangle(). You can use the vertex data to generate a patch or other type object if you don't want to deal with a rectangle() object.
%% plot two identical rounded rectangles atop each other
% parameters
pos = [0 0 10 20]; % [x y w h]
cur = 1; % curvature (scalar or 2-tuple)
th = 45; % rotation (CCW about the origin)
% using rectangle() and hgtransform()
hg = hgtransform;
rectangle('parent',hg,'position',[0 0 10 20],'curvature',cur);
hg.Matrix = makehgtform('zrotate',th/180*pi);
hold on; grid on; axis equal
% making vertex data and using basic transformations
[x y] = myrectangle('position',pos,'curvature',cur);
R = [cosd(-th) -sind(-th); sind(-th) cosd(-th)];
xy = [x y]*R ; % rotate the points
plot(xy(:,1),xy(:,2),'--y')
function [x y] = myrectangle(varargin)
% [X Y] = MYRECTANGLE({OPTIONS})
% Similar in usage to built-in rectangle(), but this tool
% simply returns XY vertex data which can be used directly with
% plot(), patch(), polyshape(), etc. This allows a familiar means
% of generating simple rounded rectangles without the limitations
% that come with using rectangle().
%
% OPTIONS include the following key-value pairs:
% 'position' specifies the rectangle geometry (default [0 0 1 1]).
% This describes both location and size [xos yos width height].
% 'curvature' specifies the relative corner radius (default 0).
% This radius is relative to half the side lengths of the rectangle.
% May be specified as a scalar or a 2-tuple in the form of [xcur ycur].
% For example, 0 is a rectangle with square corners. 1 is a stadium,
% but [1 1] is an ellipse.
% 'npoints' specifies the nominal number of points to use. (default 100).
% Actual number of points will be rounded to the nearest mulitple of 4,
% with one additional point used to close the curve. This parameter
% is ignored when CUR == 0.
%
% See also: rectangle, makeoval
% defaults
pos = [0 0 1 1];
cur = [0 0];
npts = 100;
% parse inputs
if ~isempty(varargin)
for k = 1:2:numel(varargin)
thisarg = lower(varargin{k});
switch thisarg
case {'pos','position'}
pos = varargin{k+1};
case {'cur','curvature'}
cur = varargin{k+1};
case 'npoints'
npts = varargin{k+1};
otherwise
error('MYRECTANGLE: unknown option %s',varargin{k})
end
end
end
npts = max(npts,4);
% generate xy data
npts = round(npts/4)*4 + 1;
th = linspace(0,2*pi,npts);
thm = reshape(th(2:end),[],4);
thm = [th(1) thm(end,1:3); thm];
rr = pos(3:4)/2;
if isscalar(cur)
er = cur.*[1 1]*min(rr);
else
er = cur.*rr;
end
x = er(1)*cos(thm) + pos(1) + rr(1) + (rr(1)-er(1))*[1 -1 -1 1];
y = er(2)*sin(thm) + pos(2) + rr(2) + (rr(2)-er(2))*[1 1 -1 -1];
% remove redundant corner and midpoint vertices
% which will occur at the extreme values of cur
% then close the curve
xy = [x(:) y(:)];
xy = unique(xy,'rows','stable');
x = xy([1:end 1],1); y = xy([1:end 1],2);
end
While the second example here is just using plot() and a basic transformation matrix, if the vertex data were used to create a polyshape, you could use the rotate, translate, or scale method on it instead of messing around with setting up the transformation matrices.
EDIT: Made the example more complete and usable as a function.
More Answers (1)
S M Ragib Shahriar Islam
on 19 Dec 2022
Hi, I was also searching for similar type of solution. This following link might help you....
0 Comments
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

