简体   繁体   中英

Rounded corner rectangle coordinate representation

Simple rounded corner rectangle code in Matlab can be written as follows.

rectangle('Position',[0,-1.37/2,3.75,1.37],...
      'Curvature',[1],...
     'LineWidth',1,'LineStyle','-')
daspect([1,1,1])

How to get the x and y coordinates arrays of this figure?

To get the axes units boundaries, do:

axisUnits = axis(axesHandle) % axesHandle could be gca

axisUnits will be an four elements array, with the following syntax: [xlowlim xhighlim ylowlim yhighlim] , it will also contain the zlow and zhigh for 3-D plots.

But I think that is not what you need to know. Checking the matlab documentation for the rectangle properties , we find:

Position four-element vector [x,y,width,height]

Location and size of rectangle. Specifies the location and size of the rectangle in the data units of the axes. The point defined by x, y specifies one corner of the rectangle, and width and height define the size in units along the x- and y-axes respectively.

It is also documented on the rectangle documentation :

rectangle('Position',[x,y,w,h]) draws the rectangle from the point x,y and having a width of w and a height of h. Specify values in axes data units .

See if this illustrate what you want. You have an x axis that goes from −100 to 100 and y axis that goes from 5 to 15. Suppose you want to put a rectangle from −30 to −20 in x and 8 to 10 in y.

rectangle('Position',[-30,8,10,2]);

As explained by the comments there appears to be no direct way to query the figure created by rectangle and extract x/y coordinates. On the other hand, I can think of two simple strategies to arrive at coordinates that will closely reproduce the curve generated with rectangle:

(1) Save the figure as an image (say .png) and process the image to extract points corresponding to the curve. Some degree of massaging is necessary but this is relatively straightforward if blunt and I expect the code to be somewhat slow at execution compared to getting data from an axes object.

(2) Write your own code to draw a rectangle with curved edges. While recreating precisely what matlab draws may not be so simple, you may be satisfied with your own version.

Whether you choose one of these approaches boils down to (a) what speed of execution you consider acceptable (b) how closely you need to replicate what rectangle draws on screen (c) whether you have image processing routines, say for reading an image file.

Edit

If you have the image processing toolbox you can arrive at a set of points representing the rectangle as follows:

h=rectangle('Position',[0,-1.37/2,3.75,1.37],...
      'Curvature',[1],...
     'LineWidth',1,'LineStyle','-')
daspect([1,1,1])
axis off
saveas(gca,'test.png');
im = imread('test.png');
im = rgb2gray(im);
figure, imshow(im)

Note that you will still need to apply a threshold to pick the relevant points from the image and then transform the coordinate system and rearrange the points in order to display properly as a connected set. You'll probably also want to tinker with resolution of the initial image file or apply image processing functions to get a smooth curve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM