I have x and y values, but, how can I draw the shape?

I have the following x and y values:
149.5000 75.5000 451.5000 279.5000
which denote (respectively):
x-min y-min x-max y-max
Having those dimensions, how can I draw them as a shape that will represent the boundary with the above values?
Thanks.

5 Comments

You mean a rectangle?
@José-Luis. Thanks for your reply. A rectangle could be. But, I prefer a "polygon" or "ellipse" for instance.
There are infinitely many polygons that could be defined that way. I am afraid you will need to be more specific. Is something like convhull() what you are looking for?
@José-Luis. If "convexhull()" draws a convex, yes, that would do. How can we draw it using the dimensions above? Can it also be filled with a colour? Thanks
With a convex hull, if the shape were a "E" shape, you'd get a rectangle. The convex hull is like if you stretched a rubber band around all of your vertices. It's the shape of the rubber band. All of the concave parts would get "filled in."

Sign in to comment.

Answers (2)

doc convhull
nVal = 50;
xx = 149.5 + rand(nVal,1).*(451.5-149.5);
yy = 75.5 + rand(nVal,1).*(279.5-75.5);
k = convhull(xx,yy);
plot(xx(k),yy(k),'r-',xx,yy,'b+')
doc patch if you want a filled polygon.

10 Comments

@José-Luis. Yes, this is what I would like to have. The remaining thing here is, how can I plot the shape and fill it with some colour. Thanks
Have you tried patch? Please look at the documentation and feel free to come back with questions if you don't understand something.
@José-Luis. I checked the "patch" documentation. I have used it as follows:
patch(xx,yy,'r')
But, I didn't get all the region filled with "red". I just got some small regions from hee in there inside the main region filled.
I may be using it wrong?
nVal = 50;
xx = 149.5 + rand(nVal,1).*(451.5-149.5);
yy = 75.5 + rand(nVal,1).*(279.5-75.5);
k = convhull(xx,yy);
plot(xx(k),yy(k),'r-',xx,yy,'b+');
patch(xx(k),yy(k),[1 0 0],'FaceAlpha',0.5);
Please accept an answer if it helps you.
But med-sweng shouldn't call convhull unless he needs to.
@Image Analyst : convhull seems mandatory if the shape is not rectangle no?
There's always an alpha shape, but Matlab does not do that natively.
convhull is not mandatory. The original poster merely said that he had corners of the bounding box of some shape and he wanted to plot it. No convex hull is necessary. Moreover, even if the shape inside the bounding box was weird, say a star shape or irregular blob shape, and say you wanted to plot that, then there is no reason to call convhull. For example, look at this code that draws a soft star. No convhull is called. And med-swing didn't ask for convhull he just said he wanted to know how to plot the bounding box defined by a vector where the x and y were all in the same vector.
% Demo macro to draw a rounded star (like a splat).
%
clc;
close all;
clear all;
workspace;
% Select the inner and outer radius.
outerRadius = 44 % You can change this
innerRadius = 19 % You can change this
% Select the number of lobes around the circle.
numberOfLobes = 8; % You can change this
period = 2 * pi / numberOfLobes;
meanRadius = (outerRadius + innerRadius)/2
amplitude = (outerRadius - innerRadius)/2
t = (0:.005:1)*2*pi; % Independent parameter.
variableRadius = amplitude * cos(2*pi*t/period) + meanRadius
subplot(2,2,1);
plot(variableRadius);
ylim([0 outerRadius]);
title('VariableRadius');
period = 2*pi; % Need to change this now.
x2 = variableRadius .* cos(2*pi*t/period);
y2 = variableRadius .* sin(2*pi*t/period);
subplot(2,2,2);
plot(t, x2);
title('x2 vs. t');
subplot(2,2,3);
plot(t, y2);
title('y2 vs. t');
subplot(2,2,4);
plot(x2,y2,'b')
title('x2 vs y2');
% Maximize window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
I think José-Luis may have misunderstood the question when med-sweng asked how to draw "a shape that will represent the boundary with the above values" - he had the shape already - an x min and max, and a y min and max. He was not asking how to find a different shape of the bounding shape. If he had , then a convex hull might have been a good suggestion (or a call to regionprops), but he already has the bounding box, given by that array, and just needed to know how to plot it.
he did not specify the shape so there is an infinity, you first gave the first shape ; a rectangle, then José gave a deformed rectangle using rand. The idea is clear now .

Sign in to comment.

Try this:
m = [149.5000 75.5000 451.5000 279.5000]
x1 = m(1);
x2 = m(3);
y1 = m(2);
y2 = m(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'r', 'LineWidth', 3);
grid on;

7 Comments

@Image Analyst. Very nice! Can I have different shape than a rectangle (i.e; polygon)? Thanks
@Image Analyst. can the region inside be filled? Thanks
@ImageAnalyst. Why do you repeat "x1" and "x2" and "y1" and "y2" in the vectors? Thanks
You can use
plot(x,y)
where x and y are vectors of the x coordinates and y coordinates of a polygon. I repeated x and y because I had 5 points to visit: the upper left, the upper right, the bottom right, the bottom left, and, to close off the rectangle I had to visit the upper left again (otherwise you'd get a rectangle with no left side.)
You can fill using fill(), patch(), or, if you have a rectangle or circle, using the rectangle() function.
For example, I added a call to patch onto my previous answer.
m = [149.5000 75.5000 451.5000 279.5000]
x1 = m(1);
x2 = m(3);
y1 = m(2);
y2 = m(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'r', 'LineWidth', 3);
grid on;
patch(x, y, 'g');
to the code I gave you above to fill your rectangle with green color.
also could you say how can one draw an ellipse with the major axis coords without knowing the minor axis values?

Sign in to comment.

Asked:

on 19 Dec 2013

Commented:

on 26 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!