1

I would like to create a superellipse shape. I found the following question "How do I make a superellipse node shape in tikz?" with @Jake's answer that does indeed produce a superellipse. Unfortunately I would like to fill the superellipse with a color and it does not work by adding fill=blue for example to Jake's superellipse node parameters.

\node [fill=blue,minimum width=4cm, minimum height=2cm,
 superellipse, superellipse parameter=1.5] (a) {};

I don't know if this is due to the fact that it is a node shape. This is not necessary in my case of use and a path in the shape of a superellipse is enough for me. In fact I am looking for more or less the equivalent of a

\fill [blue] (0,0) circle [x radius=2cm, y radius=1cm];

but for a superellipse.

Moreover the code in the referred answer was written more than 9 years ago, maybe there is a way to do things more efficiently today, following the improvements of tikz etc.

Ingmar
  • 6,690
  • 5
  • 26
  • 47
Xorios
  • 307
  • Hmm. Try (in the cited @Jake's answer) to change the \pgfusepath{stroke} to \pgfusepath{stroke, fill}. I am not at the computer now, so I can't test it.... – Rmano Apr 03 '22 at 20:48
  • 2
    You are doing the correct thing by creating a new question. Never add sub questions to old question - just link to them as you do. What could be better is to add some compilable code instead of fragments. - e.g. just with the circle so people can see your documentclass and packages. – hpekristiansen Apr 03 '22 at 21:53
  • @Rmano: It works indeed, thank you very much! – Xorios Apr 04 '22 at 19:32

1 Answers1

3
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[
declare function={
  sx(\t)= a*cos(\t r)^(2/n);
  sy(\t)= b*sin(\t r)^(2/n);
  a=1;
  b=1;
  n=4;
}]
\draw[fill=green, variable=\t, domain=0:pi/2] plot ({sx(\t)},{sy(\t)}) -- plot({-sx(pi/2-\t)},{sy(pi/2-\t)}) -- plot({-sx(\t)},{-sy(\t)}) -- plot({sx(pi/2-\t)},{-sy(pi/2-\t)}) -- cycle;
\end{tikzpicture}
\end{document}

Green superellipse

Edit: Here is an alternative way to draw the same:

\draw[fill=green, variable=\t]
  plot[domain=0:pi/2] ({sx(\t)},{sy(\t)}) -- 
  plot[domain=pi/2:0] ({-sx(\t)},{sy(\t)}) --
  plot[domain=0:pi/2] ({-sx(\t)},{-sy(\t)}) --
  plot[domain=pi/2:0] ({sx(\t)},{-sy(\t)}) -- cycle;
  • Wow, this is (almost) perfect. And much simpler than Jake's code. A little typo though, I think. It should always be sx(t) first, sy(t) second (with the right sign each time). Your 2nd and 4th plot have inverted sx and sy (it doesn't show if a = b). If you correct it I will validate the answer. – Xorios Apr 04 '22 at 19:39
  • 2
    No - it is not a typo. I am using the fact that sin and cos is pi/2 out of phase. If you prefer, you can invert the domain and then use sx(pi/2-\t) instead of sy(\t) to get the correct orientation (draw direction). – hpekristiansen Apr 04 '22 at 20:34
  • I understand but there is also a in sx(t) and b in sy(t). If you reverse the 2 for some points it still gives a superellipse for a=b but it gives a weird shape for a different from b. Take your first code and put a=1 and b=2 for example and you will see the result. – Xorios Apr 05 '22 at 07:02
  • @Xorios: You are correct - I have corrected the code. – hpekristiansen Apr 05 '22 at 07:26