| 1 |
% Copyright 2022 by Qrrbrbirlbel |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
\usepgflibrary{shapes.geometric,intersections} |
| 9 |
\unless\ifcsname pgfextset\endcsname |
| 10 |
\input tikzext-util.tex |
| 11 |
\fi |
| 12 |
|
| 13 |
\pgfextset{ |
| 14 |
superellipse x exponent/.initial=2.5, |
| 15 |
superellipse y exponent/.initial=2.5, |
| 16 |
superellipse step/.initial=4, |
| 17 |
superellipse exponent/.style={/pgf/ext/superellipse x exponent={#1},/pgf/ext/superellipse y exponent={#1}}} |
| 18 |
|
| 19 |
% Evaluates the x-value of a superellipse around the center |
| 20 |
% #1 = t (0≤t≤90) |
| 21 |
% #2 = 2/m (x-exp) |
| 22 |
% #3 = a (x-rad) |
| 23 |
\pgfmathdeclarefunction{ext_superellipsex}{3}{% |
| 24 |
\pgfmathcos@{#1}% |
| 25 |
\pgfmathpow@{\pgfmathresult}{#2}% |
| 26 |
\pgfmathmultiply@{\pgfmathresult}{#3}} |
| 27 |
|
| 28 |
% Evaluates the y-value of a superellipse around the center |
| 29 |
% #1 = t (0≤t≤90) |
| 30 |
% #2 = 2/n (y-exp) |
| 31 |
% #3 = a (y-rad) |
| 32 |
\pgfmathdeclarefunction{ext_superellipsey}{3}{% |
| 33 |
\pgfmathsin@{#1}% |
| 34 |
\pgfmathpow@{\pgfmathresult}{#2}% |
| 35 |
\pgfmathmultiply@{\pgfmathresult}{#3}} |
| 36 |
|
| 37 |
\expandafter\let\expandafter\pgfmathextsuperellipsex\csname pgfmathext_superellipsex\endcsname |
| 38 |
\expandafter\let\expandafter\pgfmathextsuperellipsex@\csname pgfmathext_superellipsex@\endcsname |
| 39 |
\expandafter\let\expandafter\pgfmathextsuperellipsey\csname pgfmathext_superellipsey\endcsname |
| 40 |
\expandafter\let\expandafter\pgfmathextsuperellipsey@\csname pgfmathext_superellipsey@\endcsname |
| 41 |
|
| 42 |
% Evaluates the x- and y- value of superellipse around the center |
| 43 |
% #1 = t (0≤t≤90) |
| 44 |
% #2 = 2/m (x-exp) |
| 45 |
% #3 = 2/n (y-exp) |
| 46 |
% #4 = a (x-rad) |
| 47 |
% #5 = b (y-rad) |
| 48 |
\def\pgfextmathsuperellipseXY#1#2#3#4#5{% |
| 49 |
\begingroup |
| 50 |
\pgfmathextsuperellipsex@{#1}{#2}{#4}% |
| 51 |
\let\pgfmath@temp\pgfmathresult |
| 52 |
\pgfmathextsuperellipsey@{#1}{#3}{#5}% |
| 53 |
\edef\pgfmath@temp{\def\noexpand\pgfmathresultX{\pgfmath@temp}% |
| 54 |
\def\noexpand\pgfmathresultY{\pgfmathresult}}% |
| 55 |
\expandafter |
| 56 |
\endgroup\pgfmath@temp} |
| 57 |
|
| 58 |
\pgfdeclareshape{ext_superellipse}{% |
| 59 |
\inheritsavedanchors[from=ellipse] |
| 60 |
\inheritanchor[from=ellipse]{text}\inheritanchor[from=ellipse]{center} |
| 61 |
\inheritanchor[from=ellipse]{mid}\inheritanchor[from=ellipse]{base} |
| 62 |
\inheritanchor[from=ellipse]{north}\inheritanchor[from=ellipse]{south} |
| 63 |
\inheritanchor[from=ellipse]{west}\inheritanchor[from=ellipse]{east} |
| 64 |
\inheritanchor[from=ellipse]{mid west}\inheritanchor[from=ellipse]{base west} |
| 65 |
\inheritanchor[from=ellipse]{mid east}\inheritanchor[from=ellipse]{base east} |
| 66 |
\anchor{north east}{% |
| 67 |
\pgf@process{\radius}% |
| 68 |
\pgfmathpow@{.70710678118}{\xexponent}% |
| 69 |
\pgf@x=\pgfmathresult\pgf@x |
| 70 |
\pgfmathpow@{.70710678118}{\yexponent}% |
| 71 |
\pgf@y=\pgfmathresult\pgf@y |
| 72 |
\pgfpointadd{}{\centerpoint}% |
| 73 |
} |
| 74 |
\anchor{north west}{% |
| 75 |
\pgf@process{\radius}% |
| 76 |
\pgfmathpow@{.70710678118}{\xexponent}% |
| 77 |
\pgf@x=-\pgfmathresult\pgf@x |
| 78 |
\pgfmathpow@{.70710678118}{\yexponent}% |
| 79 |
\pgf@y=\pgfmathresult\pgf@y |
| 80 |
\pgfpointadd{}{\centerpoint}% |
| 81 |
} |
| 82 |
\anchor{south west}{% |
| 83 |
\pgf@process{\radius}% |
| 84 |
\pgfmathpow@{.70710678118}{\xexponent}% |
| 85 |
\pgf@x=-\pgfmathresult\pgf@x |
| 86 |
\pgfmathpow@{.70710678118}{\yexponent}% |
| 87 |
\pgf@y=-\pgfmathresult\pgf@y |
| 88 |
\pgfpointadd{}{\centerpoint}% |
| 89 |
} |
| 90 |
\anchor{south east}{% |
| 91 |
\pgf@process{\radius}% |
| 92 |
\pgfmathpow@{.70710678118}{\xexponent}% |
| 93 |
\pgf@x=\pgfmathresult\pgf@x |
| 94 |
\pgfmathpow@{.70710678118}{\yexponent}% |
| 95 |
\pgf@y=-\pgfmathresult\pgf@y |
| 96 |
\pgfpointadd{}{\centerpoint}% |
| 97 |
} |
| 98 |
\savedmacro\xexponent{% |
| 99 |
\pgfmathreciprocal{\pgfkeysvalueof{/pgf/ext/superellipse x exponent}}% |
| 100 |
\pgfmathmultiply@{\pgfmathresult}{2}% |
| 101 |
\let\xexponent\pgfmathresult |
| 102 |
} |
| 103 |
\savedmacro\yexponent{% |
| 104 |
\pgfmathreciprocal{\pgfkeysvalueof{/pgf/ext/superellipse y exponent}}% |
| 105 |
\pgfmathmultiply@{\pgfmathresult}{2}% |
| 106 |
\let\yexponent\pgfmathresult |
| 107 |
} |
| 108 |
\savedmacro\step{% |
| 109 |
\pgfmathtruncatemacro\step{\pgfkeysvalueof{/pgf/ext/superellipse step}}% |
| 110 |
} |
| 111 |
\backgroundpath{% |
| 112 |
\pgf@process{\radius}% |
| 113 |
\pgfmathsetmacro\xradius{\pgf@x-(\pgfkeysvalueof{/pgf/outer xsep})}% |
| 114 |
\pgfmathsetmacro\yradius{\pgf@y-(\pgfkeysvalueof{/pgf/outer ysep})}% |
| 115 |
\let\pgf@tempa\pgfutil@empty\let\pgf@tempb\pgfutil@empty |
| 116 |
\let\pgf@tempc\pgfutil@empty\let\pgf@tempd\pgfutil@empty |
| 117 |
\c@pgf@counta=\step |
| 118 |
\pgfutil@loop |
| 119 |
\ifnum\c@pgf@counta<90 |
| 120 |
\pgfextmathsuperellipseXY{\the\c@pgf@counta}{\xexponent}{\yexponent}{\xradius}{\yradius}% |
| 121 |
\edef\pgf@temp{\noexpand\pgfplotstreampoint{\noexpand\pgfqpoint{\pgfmathresultX pt}{\pgfmathresultY pt}}}% |
| 122 |
\pgfutil@append@macrotomacro\pgf@tempa\pgf@temp |
| 123 |
\edef\pgf@temp{\noexpand\pgfplotstreampoint{\noexpand\pgfqpoint{-\pgfmathresultX pt}{\pgfmathresultY pt}}}% |
| 124 |
\tikzextutil@prefix@macrotomacro\pgf@tempb\pgf@temp |
| 125 |
\edef\pgf@temp{\noexpand\pgfplotstreampoint{\noexpand\pgfqpoint{-\pgfmathresultX pt}{-\pgfmathresultY pt}}}% |
| 126 |
\pgfutil@append@macrotomacro\pgf@tempc\pgf@temp |
| 127 |
\edef\pgf@temp{\noexpand\pgfplotstreampoint{\noexpand\pgfqpoint{\pgfmathresultX pt}{-\pgfmathresultY pt}}}% |
| 128 |
\tikzextutil@prefix@macrotomacro\pgf@tempd\pgf@temp |
| 129 |
\advance\c@pgf@counta by\step |
| 130 |
\pgfutil@repeat |
| 131 |
\pgftransformshift{\centerpoint}% |
| 132 |
\pgfplothandlerclosedcurve |
| 133 |
\pgfplotstreamstart |
| 134 |
\pgfplotstreampoint{\pgfqpoint{\xradius pt}{0pt}}% east |
| 135 |
\pgf@tempa |
| 136 |
\pgfplotstreampoint{\pgfqpoint{0pt}{\yradius pt}}% north |
| 137 |
\pgf@tempb |
| 138 |
\pgfplotstreampoint{\pgfqpoint{-\xradius pt}{0pt}}% west |
| 139 |
\pgf@tempc |
| 140 |
\pgfplotstreampoint{\pgfqpoint{0pt}{-\yradius pt}}% south |
| 141 |
\pgf@tempd |
| 142 |
\pgfplotstreamend |
| 143 |
\pgftransformshift{\centerpoint\pgf@x=-\pgf@x\pgf@y=-\pgf@y} |
| 144 |
} |
| 145 |
\anchorborder{% |
| 146 |
\pgfextract@process\externalpoint{}% |
| 147 |
\ifdim\pgf@x=0pt % catch special case x = 0 |
| 148 |
\ifdim\pgf@y<0pt |
| 149 |
\csname pgf@anchor@ext_superellipse@south\endcsname |
| 150 |
\else |
| 151 |
\csname pgf@anchor@ext_superellipse@north\endcsname |
| 152 |
\fi |
| 153 |
\else |
| 154 |
\ifdim\pgf@y=0pt % catch special case y = 0 |
| 155 |
\ifdim\pgf@x<0pt |
| 156 |
\csname pgf@anchor@ext_superellipse@west\endcsname |
| 157 |
\else |
| 158 |
\csname pgf@anchor@ext_superellipse@east\endcsname |
| 159 |
\fi |
| 160 |
\else % both are not zero |
| 161 |
% save original direction |
| 162 |
\pgf@xa=\pgf@x |
| 163 |
\pgf@ya=\pgf@y |
| 164 |
% make both positive, we're not looking at one quadrant |
| 165 |
\ifdim\pgf@xa<0pt \pgf@x=-\pgf@x\fi |
| 166 |
\ifdim\pgf@ya<0pt \pgf@y=-\pgf@y\fi |
| 167 |
% save that point again |
| 168 |
\pgfextract@process\externalpoint{}% |
| 169 |
% we need to do the calculations without any transformations |
| 170 |
% since we're using plots and \pgfpointborderrectangle |
| 171 |
\pgftransformreset |
| 172 |
\pgfintersectionofpaths{% |
| 173 |
% from center to point on rectangle that encompasses superellipse |
| 174 |
\pgf@relevantforpicturesizefalse |
| 175 |
\pgfpathmoveto{\pgfpointorigin}% |
| 176 |
\pgfpathlineto{\pgfpointborderrectangle{\externalpoint}{\radius}}% |
| 177 |
}{% |
| 178 |
\pgf@relevantforpicturesizefalse |
| 179 |
\pgf@process{\radius} |
| 180 |
\edef\xradius{\pgf@sys@tonumber\pgf@x}% |
| 181 |
\edef\yradius{\pgf@sys@tonumber\pgf@y}% |
| 182 |
\let\pgf@tempa\pgfutil@empty |
| 183 |
\c@pgf@counta=\step |
| 184 |
\pgfutil@loop |
| 185 |
\ifnum\c@pgf@counta<90 |
| 186 |
\pgfextmathsuperellipseXY{\the\c@pgf@counta}{\xexponent}{\yexponent}{\xradius}{\yradius}% |
| 187 |
\edef\pgf@temp{\noexpand\pgfplotstreampoint{\noexpand\pgfqpoint{\pgfmathresultX pt}{\pgfmathresultY pt}}}% |
| 188 |
\pgfutil@append@macrotomacro\pgf@tempa\pgf@temp |
| 189 |
\advance\c@pgf@counta by\step |
| 190 |
\pgfutil@repeat |
| 191 |
% we're only using curveto since closedcurve |
| 192 |
% messes with our other path |
| 193 |
\pgfplothandlercurveto |
| 194 |
\pgfplotstreamstart |
| 195 |
\pgfplotstreampoint{\pgfqpoint{\xradius pt}{0pt}}% east |
| 196 |
\pgf@tempa |
| 197 |
\pgfplotstreampoint{\pgfqpoint{0pt}{\yradius pt}}% north |
| 198 |
\pgfplotstreamend |
| 199 |
}% |
| 200 |
\ifnum\pgfintersectionsolutions>0 % only if a solution was found |
| 201 |
\pgf@process{\pgfpointintersectionsolution{1}}% |
| 202 |
\else % otherwise take the border on the rectangle (close enough?) |
| 203 |
\pgf@process{\pgfpointborderrectangle{\externalpoint}{\radius}}% |
| 204 |
\fi |
| 205 |
\ifdim\pgf@xa<0pt \pgf@x=-\pgf@x\fi |
| 206 |
\ifdim\pgf@ya<0pt \pgf@y=-\pgf@y\fi |
| 207 |
\pgf@process{\pgfpointadd{}{\centerpoint}}% |
| 208 |
\fi |
| 209 |
\fi |
| 210 |
} |
| 211 |
} |
| 212 |
%%% Compatibility |
| 213 |
\pgfkeysifdefined{/tikz/ext/compat/shapes/.@cmd}{}{\pgfextset{/tikz-ext/compat/add library={shapes}{pre 0.6}}} |
| 214 |
\tikzextset{ |
| 215 |
compat/shapes/warn/.append style=/tikz/ext/compat/shapes.superellipse/warn, |
| 216 |
compat/shapes/pre 0.6/.append style=/tikz/ext/compat/shapes.superellipse/pre 0.6, |
| 217 |
compat/shapes.superellipse/warn/.code={% |
| 218 |
\tikzextutil@compatwarning@key {shapes}{pre 0.6}{/pgf/superellipse exponent} {/pgf/ext/superellipse exponent}% |
| 219 |
\tikzextutil@compatwarning@valuekey{shapes}{pre 0.6}{/pgf/superellipse x exponent}{/pgf/ext/superellipse x exponent}% |
| 220 |
\tikzextutil@compatwarning@valuekey{shapes}{pre 0.6}{/pgf/superellipse y exponent}{/pgf/ext/superellipse y exponent}% |
| 221 |
\tikzextutil@compatwarning@valuekey{shapes}{pre 0.6}{/pgf/superellipse step} {/pgf/ext/superellipse step}% |
| 222 |
\tikzextutil@compat@cloneshape{superellipse}{text,north,north west,north east,center,west,east,mid,mid west,mid east,base,base west,base east,south,south west,south east,center}% |
| 223 |
\def\tikzext@temp{\tikzextutil@compatmaybewarning{shape 'superellipse'}{'ext_superellipse'}{shapes = pre 0.6}}% |
| 224 |
\tikzextutil@prefix@macrotomacro\pgf@sh@s@superellipse\tikzext@temp |
| 225 |
}, |
| 226 |
compat/shapes.superellipse/pre 0.6/.code={% |
| 227 |
\pgfkeysdef{/pgf/superellipse exponent}{\pgfkeysvalueof{/pgf/ext/superellipse exponent/.@cmd}##1\pgfeov}% |
| 228 |
\pgfkeysdef{/pgf/superellipse x exponent}{\pgfkeyssetvalue{/pgf/ext/superellipse x exponent}{##1}}% |
| 229 |
\pgfkeysdef{/pgf/superellipse y exponent}{\pgfkeyssetvalue{/pgf/ext/superellipse y exponent}{##1}}% |
| 230 |
\pgfkeysdef{/pgf/superellipse steps}{\pgfkeyssetvalue{/pgf/ext/superellipse steps}{##1}}% |
| 231 |
\pgfkeyssetvalue{/pgf/superellipse x exponent}{\pgfkeysvalueof{/pgf/ext/superellipse x exponent}}% |
| 232 |
\pgfkeyssetvalue{/pgf/superellipse y exponent}{\pgfkeysvalueof{/pgf/ext/superellipse y exponent}}% |
| 233 |
\pgfkeyssetvalue{/pgf/superellipse step} {\pgfkeysvalueof{/pgf/ext/superellipse step}}% |
| 234 |
\tikzextutil@compat@cloneshape{superellipse}{text,north,north west,north east,center,west,east,mid,mid west,mid east,base,base west,base east,south,south west,south east,center}% |
| 235 |
}, |
| 236 |
compat/shapes.superellipse/\pgfkeysvalueof{/tikz/ext/compat/shapes}% |
| 237 |
} |
| 238 |
\endinput |