How do I draw a squircle?

2 views (last 30 days)
Charles Rambo
Charles Rambo on 26 Dec 2019
Commented: Star Strider on 27 Dec 2019
I am trying to draw a squircle with a given height and width as I need to approximate a rounded rectangle using a function. I have tried to use both equations for a squircle that wikipedia has recomended and while I think I would prefer the Fernández-Guasti squircle I have gotten further with the ellipse based squircle (which I can also get to work on Mathematica) but the best plot I have gotten so far resembles an exponential decay. The input I'm using at this point is below...
W=2.65;H=4.91;
x=linspace(0,2.65,360);
y=(1-(x./(W/2)).^(1/4)).*(H/2);
plot (x,y)

Answers (1)

Star Strider
Star Strider on 26 Dec 2019
The easiest way to approach this is to use the contour function to return the result of creating the ‘squircle’ function as an implicit function.
Try this (using the Fernández-Guasti expression):
W=2.65;
H=4.91;
s = 0.8;
r = 1.1;
x=linspace(-W/2,W/2,36); % Define Vector
y=linspace(-H/2,H/2,36); % Define Vector
[X,Y] = ndgrid(x,y); % Create Matrices (Alternatively, Use ‘meshgrid’)
squircle_fcn = @(x,y,r,s) x.^2 + y.^2 - s.^2.*x.^2.*y.^2./r.^2 - r.^2; % Fernández-Guasti Squircle Function
figure
c = contour(X, Y, squircle_fcn(X,Y,r,s), [0 0]); % Contour Plot
axis equal
x_squircle = c(1,2:end); % X-Coordinates Calculated By ‘contour’
y_squircle = c(2,2:end); % Y-Coordinates Calculated By ‘contour’
figure
plot(x_squircle*W/2, y_squircle*H/2, '-r') % Add Scaling
axis equal
axis([[-1 1]*W*0.6 [-1 1]*H*0.6])
producing (unscaled):
scaled:
How do I draw a squircle (2) - 2019 12 26.png
Make appropriate changes to the ‘r’ and ‘s’ parameters to get the result you want.
See the documentation on contour to understand why it works here. Specifically see M — Contour matrix for an explanation of the ‘x_squircle’ and ‘y_squircle’ vectors.
  2 Comments
Charles Rambo
Charles Rambo on 27 Dec 2019
Awesome! I've got this working and drawing my shape, but you could help me understand this more, I'd appreciate it. Just so that you know, I particularly need to know the (x,y) cordinates of the points around the shape so that I can for lack of my vocabulary put the shape through various transfrormations and replot it along the way, so....
From what I understand when reading about the contour funtion, obviously the first few steps define variable and setup vectors with the range of variable for the x,y axis, then the ngrid step defines the area that we are working in. Now you setup a function (not very familiar with this I'll be honest, as in I've never used an @ in MATLAB). Figure prepares MATLAB for a figure. then we define matrix 'c' which is filled in by the contour feature.
I understand that this creates a matrix, but I think I fully understand what the values filling the matric mean. As in what are/why are the different levels, and if the susequent numbers are the (x,y) cordinates for my shape, could I create a vector each for just my x's and y's? Really I think that's what you've done with the x,y_squircle comands, but I want to make sure, especially as those come before we add the scaling and really I need the values for the target shape.
Thank you for your patience, and of course all the help.
Star Strider
Star Strider on 27 Dec 2019
Thank you.
I am not certain that I can elaborate further than I already have with the comment-documentation.
The ndgrid call forms matrices from the previously-defined vectors. That is necessary for the contour function. See the contour documentation (that I already linked to) to understand what they mean.
The ‘squircle_fcn’ is an anonymous function. See the documentation on Anonymous Functions for details. The documentation explains them better than I could. It uses vectorised operations, so see the documentation on Array vs. Matrix Operations for those details.
The ‘x_squircle’ and ‘y_squircle’ vectors are the x- and y-coordinates (respectively) returned by the contour function for the [0 0] contour. This essentially evaluates an implicit function (a multivariate function that equals zero) at the [0 0] contour.
The ‘x_squircle’ and ‘y_squircle’ vectors are evaluated for the ‘X’ and ‘Y’ matrices and the previously defined ‘s’ and ’r’ values. The Wikipedia article indicates that the ‘squircle’ is symmetric about the origin, so the scaling simply multiplies the x- and y-dimensions to create the desired shape.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!