How do I draw a squircle?
显示 更早的评论
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)
回答(1 个)
Star Strider
2019-12-26
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:
%20-%202019%2012%2026.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 个评论
Charles Rambo
2019-12-27
Star Strider
2019-12-27
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!
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!