Superellipse: Difference between revisions

m
 
(19 intermediate revisions by 12 users not shown)
Line 16:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC Superellipse(INT x0 BYTE y0 REAL POINTER n BYTE a)
Line 73:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Superellipse.png Screenshot from Atari 8-bit computer]
Line 79:
=={{header|Ada}}==
{{libheader|SDLAda}} Brute force calculation.
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Elementary_Functions;
 
with SDL.Video.Windows.Makers;
Line 151:
Window.Finalize;
SDL.Finalise;
end Superelipse;</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">n := 2.5
a := 200
b := 200
Line 234:
ExitApp
Return
;----------------------------------------------------------------</langsyntaxhighlight>
 
=={{header|C}}==
Interactive program to draw a SuperEllipse. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
#include<graphics.h>
#include<stdio.h>
Line 266:
closegraph();
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
[[File:DelphiSuperElipse.png|frame|none]]
<syntaxhighlight lang="Delphi">
 
procedure DrawSuperElipse(Image: TImage);
var Points: array of double;
const N = 2.5;
const Border = 10;
var A: integer;
var X: integer;
var W2,H2: integer;
begin
{Make elipse size and position based on window size}
W2:=Image.Width div 2;
H2:=Image.Height div 2;
A:=Min(W2,H2)-Border;
{Fill array with points}
SetLength(Points,A);
for X:=0 to High(Points) do
Points[X]:=Power(Power(A, N) - Power(X, N), 1 / N);
 
Image.Canvas.Pen.Color:=clRed;
Image.Canvas.Pen.Width:=2;
 
{Starting point}
Image.Canvas.MoveTo(W2+High(Points),trunc(H2-Points[High(Points)]));
{Draw Upper right}
for X:=High(Points) downto 0 do
begin
Image.Canvas.LineTo(W2+x, trunc(H2-Points[X]))
end;
{Draw Upper left}
for X:=0 to High(Points) do
begin
Image.Canvas.LineTo(W2-X, trunc(H2-Points[X]))
end;
 
{Draw Lower left}
for X:=High(Points) downto 0 do
begin
Image.Canvas.LineTo(W2-X, trunc(H2+Points[X]))
end;
{Draw Lower right}
for X:=0 to High(Points) do
begin
Image.Canvas.LineTo(W2+X, trunc(H2+Points[X]))
end;
{Connect back to beginning}
Image.Canvas.LineTo(W2+High(Points),trunc(H2-Points[High(Points)]));
Image.Repaint;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Elapsed Time: 13.282 ms.
</pre>
 
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=Vc1LCsMgFEbh+V3FGbYJGLHYUV2MtqERgikomOw+2Bft6L9wPrgJh1FWfFutJbx3jmms8VYmtDJSpziPFC6O01kLsOJ4LBUfMtclUzgYBtKRDk9Hjvf0Ck1vPzrH9KfDRz9D0+03KwOWHqvZvmerhd6hlRUlOw== Run it]
<syntaxhighlight>
n = 2.5
a = 200
b = 200
linewidth 0.2
while t <= 360
x = pow abs cos t (2 / n) * a * sign cos t
y = pow abs sin t (2 / n) * b * sign sin t
line x / 5 + 50 y / 5 + 50
t += 0.5
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Link to the super-ellipse [http://www.echolalie.org/echolisp/images/super-ellipse.png image].
<langsyntaxhighlight lang="scheme">
(lib 'plot)
(define (eaxpt x n) (expt (abs x) n))
Line 277 ⟶ 354:
(plot-xy Ellie -400 -400)
→ (("x:auto" -400 400) ("y:auto" -400 400))
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 23-10-2016
' compile with: fbc -s console
 
Line 334 ⟶ 411:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
=={{header|Go}}==
{{libheader|Go Graphics}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 382 ⟶ 459:
superEllipse(dc, 2.5, 200)
dc.SavePNG("superellipse.png")
}</langsyntaxhighlight>
 
{{out}}
Line 391 ⟶ 468:
=={{header|Haskell}}==
Use the [https://github.com/ghcjs/ghcjs ghcjs compiler ] to compile to JavaScript that runs in a browser. The [https://github.com/reflex-frp/reflex-dom reflex-dom ] library is used to help with SVG rendering and input.
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings, RankNTypes #-}
import Reflex
import Reflex.Dom
Line 491 ⟶ 568:
-- At end to avoid Rosetta Code unmatched quotes problem.
elSvgns :: forall t m a. MonadWidget t m => Text -> Dynamic t (Map Text Text) -> m a -> m (El t, a)
elSvgns = elDynAttrNS' (Just "http://www.w3.org/2000/svg")</langsyntaxhighlight>
 
Link to live demo: https://dc25.github.io/superEllipseReflex/
Line 500 ⟶ 577:
We will fill the ellipse so that we do not have to worry about the size and shape of our pixels:
 
<langsyntaxhighlight Jlang="j">selips=: 4 :0
'n a b'=. y
1 >: ((n^~a%~]) +&|/ n^~b%~]) i:x
Line 506 ⟶ 583:
 
require'viewmat'
viewmat 300 selips 2.5 200 200</langsyntaxhighlight>
 
=={{header|Java}}==
[[File:superellipse.png|300px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.geom.Path2D;
import static java.lang.Math.pow;
Line 636 ⟶ 713:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
var n = 2.5, a = 200, b = 200, ctx;
 
Line 662 ⟶ 739:
}
}
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
'''Adapted from [[#Sidef|Sidef]]'''
{{works with|jq}}
 
'''Also works with gojq, the Go implementation of jq'''.
 
This entry uses jq to generate SVG.
 
'''Generic functions'''
<syntaxhighlight lang=jq>
# Input: [x, y]
def mult($a; $b): [.[0]*$a, .[1]*$b] ;
 
# Input: a number
def round($n): . * $n | floor / $n;
 
# svg header boilerplate
def svg($h; $w):
"<?xml version='1.0' standalone='no'?>",
"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>",
"<svg height='\($h)' width='\($w)' version='1.1' xmlns='http://www.w3.org/2000/svg'>";
</syntaxhighlight�>
'''Superellipse functions'''
<syntaxhighlight lang=jq>
# y in terms of x
# input: {a,b,n}
def y($x): (.b * pow( (1 - pow( ($x/.a)|length ; .n) ) ; 1/.n )) | round(10);
 
# input: {a,b,n}
def pline(q):
"<polyline points='\(q|map(join(","))|join(" "))'",
" style='fill:none; stroke:black; stroke-width:3' transform='translate(\(.a + 10), \(.b + 10))' />";
 
# input: {a,b,n}
def plot:
# points for one quadrant
[range(0;400) as $i | [$i, y($i)] | select(.[1] | isnan | not) ] as $q
|
pline($q),
pline($q | map( mult(1;-1))), # flip and mirror
pline($q | map( mult(-1;-1))), # for the other
pline($q | map( mult(-1;1))) # three quadrants
;
 
# Input: {a,b,n} - the constants for the superellipse
def superellipse:
svg(.b*2 + 10; .a*2 + 10), plot, "</svg>";
 
{ a: 200, b: 200, n: 2.5 }
| superellipse
</syntaxhighlight>
{{output}}
Similar to [https://github.com/SqrtNegInf/Rosettacode-Perl-Smoke/blob/master/ref/superellipse.svg Perl solution].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function superellipse(n, a, b, step::Int=100)
@assert n > 0 && a > 0 && b > 0
na = 2 / n
Line 684 ⟶ 815:
 
x, y = superellipse(2.5, 200, 200)
println(lineplot(x, y))</langsyntaxhighlight>{{out}}
<pre>
┌────────────────────────────────────────┐
Line 709 ⟶ 840:
=={{header|Kotlin}}==
The following is based on the Java entry but dispenses with the grid and slider as these aren't really part of the task.
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.*
Line 771 ⟶ 902:
}
}
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
 
Drawing four super-ellipses, a circle, a rounded square, a square, an astroid.
 
<syntaxhighlight lang="scheme">
{def superellipse
{def sgn {lambda {:n} {if {< :n 0} then - else +}}}
 
{lambda {:a :n :t}
{let { {:a :a} {:n {/ 2 :n}}
{:cost {cos {* {PI} :t}}}
{:sint {sin {* {PI} :t}}}
} {sgn :cost}{* :a {pow {abs :cost} :n}}
{sgn :sint}{* :a {pow {abs :sint} :n}}
}}}
-> superellipse
</syntaxhighlight>
 
We use SVG and the lib_plot library defining the SVG, AXES, stroke functions to draw four superellipses, a circle, a rounded square (as required), a square and an astroid.
 
<syntaxhighlight lang="scheme">
{{SVG 600 600}
{g {AXES 600 600}
{polyline
{@ points="{S.map {superellipse 200 2.5} {S.serie -1 1.01 0.01}}"
{stroke #f00 4}}}
{polyline
{@ points="{S.map {superellipse 200 0.5} {S.serie -1 1.01 0.01}}"
{stroke #0f0 4}}}
{polyline
{@ points="{S.map {superellipse 200 1} {S.serie -1 1.01 0.01}}"
{stroke #888 2}}}
{polyline
{@ points="{S.map {superellipse 200 2} {S.serie -1 1.01 0.01}}"
{stroke #888 2}}}
}}
</syntaxhighlight>
 
The output can be seen in http://lambdaway.free.fr/lambdawalks/?view=super_ellipse
 
=={{header|Liberty BASIC}}==
Reworked the Julia version to work and added a loop with a spread on n values.
<syntaxhighlight lang="lb">
<lang lb>
[start]
nomainwin
Line 820 ⟶ 991:
if x=0 then sign=0
end function
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Scale of a and b were reduced to facilitate an ASCII solution:
<langsyntaxhighlight lang="lua">local abs,cos,floor,pi,pow,sin = math.abs,math.cos,math.floor,math.pi,math.pow,math.sin
local bitmap = {
init = function(self, w, h, value)
Line 862 ⟶ 1,033:
bitmap:init(80, 60, "..")
bitmap:superellipse(40, 30, 2.5, 38, 28, "[]")
bitmap:render()</langsyntaxhighlight>
{{out}}
<pre style="font-size:25%">................................................................................................................................................................
Line 928 ⟶ 1,099:
=={{header|Maple}}==
The built-in command ImplicitPlot accepts an equation in 2 variables:
<langsyntaxhighlight lang="maple">plots:-implicitplot(abs((1/200)*x^2.5)+abs((1/200)*y^2.5) = 1, x = -10 .. 10, y = -10 .. 10);</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
The built-in function ContourPlot accepts andan equation in 2 variables and creates the desired plot
<langsyntaxhighlight Mathematicalang="mathematica">ContourPlot[Abs[x/200]^2.5 + Abs[y/200]^2.5 == 1, {x, -200, 200}, {y, -200, 200}]</langsyntaxhighlight>
 
=={{header|Nim}}==
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import math
import imageman
 
Line 972 ⟶ 1,143:
image.fill(Background)
image.drawSuperEllipse(2.5, 200, 200)
image.savePNG("super_ellipse.png", compression = 9)<lang/syntaxhighlight>
 
=={{header|ooRexx}}==
Line 982 ⟶ 1,153:
black 280,280,4</pre>
 
<langsyntaxhighlight lang="oorexx">/* REXX ***************************************************************
* Create a BMP file showing a few super ellipses
**********************************************************************/
Line 1,058 ⟶ 1,229:
Return
 
::requires rxMath library</langsyntaxhighlight>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang ="perl">my $a =use 200v5.36;
my($a, $b, $n, @q) = (200, 200, 2.5);
my $n = 2.5;
 
# y in terms of x
sub y_from_x ($x) { int $b * abs(1 - ($x/$a) ** $n ) ** (1/$n) }
my($x) = @_;
int $b * abs(1 - ($x / $a) ** $n ) ** (1/$n)
}
 
# find point pairs for one quadrant
push @q, $_, y_from_x($_) for 0..200$a;
 
# Generate an SVG image
open $fh, '>', 'superellipse.svg';
print $fh
Line 1,085 ⟶ 1,251:
'</svg>';
 
sub pline ($sx, $sy, @q) {
my(@q[2*$_] *= $sx,$sy, @q)[1+2*$_] *= @_$sy) for 0 .. $#q/2;
qq|<polyline points="@q"
 
for (0..$#q/2) {
$q[ 2*$_] *= $sx;
$q[1+2*$_] *= $sy;
}
 
qq|<polyline points="@{[join ' ',@q]}"
style="fill:none;stroke:black;stroke-width:3"
transform="translate($a, $b)" />\n|
}</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/superellipse.svg Superellipse] (offsite image)
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
<lang Phix>-- demo\rosetta\Superellipse.exw
You can run this online [http://phix.x10.mx/p2js/Superellipse.htm here].
atom n = 2.5 -- '+' and '-' increase/decrease in steps of 0.1
<!--<syntaxhighlight lang="phix">(phixonline)-->
integer a = 200, -- resize window to set this from canvas width
<span style="color: #000080;font-style:italic;">--
b = 200 -- resize window to set this from canvas height
-- demo\rosetta\Superellipse.exw
-- =============================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2.5</span> <span style="color: #000080;font-style:italic;">-- '+' and '-' increase/decrease in steps of 0.1</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">hw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">hh</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hw</span><span style="color: #0000FF;">-</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (initially 200, from 602x )</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hh</span><span style="color: #0000FF;">-</span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (initially 200, from x502)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">y</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">/</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasBegin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_OPEN_LINES</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hw</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hh</span><span style="color: #0000FF;">-</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- starting point</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hw</span><span style="color: #0000FF;">+</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hh</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">a</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hw</span><span style="color: #0000FF;">+</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hh</span><span style="color: #0000FF;">+</span><span style="color: #000000;">y</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">a</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hw</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hh</span><span style="color: #0000FF;">+</span><span style="color: #000000;">y</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">a</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hw</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hh</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasEnd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_WHITE</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">130</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">0.1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (otherwise [&gt;130] power overflow)</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">0.1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (otherwise [=0.0] divide by zero)</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=602x502"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE=Superellipse"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- release the minimum limitation</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Processing}}==
include pGUI.e
{{trans|C}}
<syntaxhighlight lang="java">
//Aamrun, 29th June 2022
 
float a = 200, b = 200, n = 2.5;
Ihandle dlg, canvas
float i, incr = 0.001;
cdCanvas cddbuffer, cdcanvas
int xMul,yMul;
 
size(500,500);
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
 
stroke(#ff0000);
integer {hw, hh} = sq_floor_div(IupGetIntInt(canvas, "DRAWSIZE"),2)
a = max(10,hw-100) -- (initially 200, from 602x )
b = max(10,hh-50) -- (initially 200, from x502)
sequence y = b&repeat(0,a)
for x=1 to a-1 do
y[x+1] = floor(exp(log(1-power(x/a,n))/n)*b)
end for
 
for(i=0;i<2*PI;i+=incr){
cdCanvasActivate(cddbuffer)
if(PI/2<i && i<3*PI/2)
cdCanvasClear(cddbuffer)
xMul = -1;
cdCanvasBegin(cddbuffer, CD_OPEN_LINES)
else
cdCanvasVertex(cddbuffer, hw, hh-b) -- starting point
xMul = 1;
for x=1 to a-1 do cdCanvasVertex(cddbuffer, hw+x, hh-y[x+1]) end for
if(PI<i && i<2*PI)
for x=a to 0 by -1 do cdCanvasVertex(cddbuffer, hw+x, hh+y[x+1]) end for
yMul = -1;
for x=0 to a do cdCanvasVertex(cddbuffer, hw-x, hh+y[x+1]) end for
else
for x=a to 0 by -1 do cdCanvasVertex(cddbuffer, hw-x, hh-y[x+1]) end for
yMul = 1;
cdCanvasEnd(cddbuffer)
cdCanvasFlush(cddbuffer)
 
return IUP_DEFAULT
end function
 
function map_cb(Ihandle ih)
cdcanvas = cdCreateCanvas(CD_IUP, ih)
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
cdCanvasSetBackground(cddbuffer, CD_WHITE)
cdCanvasSetForeground(cddbuffer, CD_BLACK)
return IUP_DEFAULT
end function
 
function esc_close(Ihandle /*ih*/, atom c)
if c=K_ESC then return IUP_CLOSE end if
if c='+' then
n = min(130,n+0.1) -- (otherwise [>130] power overflow)
IupUpdate(canvas)
elsif c='-' then
n = max(0.1,n-0.1) -- (otherwise [=0.0] divide by zero)
IupUpdate(canvas)
end if
return IUP_CONTINUE
end function
 
procedure main()
IupOpen()
ellipse(width/2 + xMul * a*pow(abs(cos(i)),2/n),height/2 + yMul * b*pow(abs(sin(i)),2/n),1,1);
canvas = IupCanvas(NULL)
}
IupSetAttribute(canvas, "RASTERSIZE", "602x502") -- initial size
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
 
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Superellipse")
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
</syntaxhighlight>
IupMap(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL) -- release the minimum limitation
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
IupMainLoop()
IupClose()
end procedure
main()</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
# Superellipse drawing in Python 2.7.9
# pic can see at http://www.imgup.cz/image/712
Line 1,203 ⟶ 1,396:
plt.title("Superellipse with parameter "+str(n))
plt.show()
</syntaxhighlight>
</lang>
 
=={{header|QB64}}==
<langsyntaxhighlight lang="qb64">_Title "Super Ellipse"
 
Dim As Integer sw, sh
Line 1,242 ⟶ 1,435:
Line -(x1, y1)
Next
End Sub</langsyntaxhighlight>
 
 
=={{header|QBasic}}==
<syntaxhighlight lang="qbasic">SCREEN 12
CLS
a = 200
b = 200
n = 2.5
na = 2 / n
t = .01
 
LINE -(520, 245), 0, BF
FOR i = 0 TO 314
xp = a * SGN(COS(t)) * ABS((COS(t))) ^ na + 320
yp = b * SGN(SIN(t)) * ABS((SIN(t))) ^ na + 240
t = t + .02
LINE -(xp, yp), 1, BF
NEXT i</syntaxhighlight>
 
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
(require plot)
#;(plot-new-window? #t)
Line 1,255 ⟶ 1,466:
(plot (isoline (superellipse 200 200 2.5) 1
-220 220 -220 220))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>my (\a, \b, \n) = 200, 200, 2.5;
{{works with|rakudo|2018-10}}
Generate an svg image to STDOUT. Redirect into a file to capture it.
<lang perl6>constant a = 200;
constant b = 200;
constant n = 2.5;
 
# y in terms of x
sub y ($x) { sprintf "%d",floor b *× (1 - ($x / a).abs ** n ) ** (1/n) }
 
# find point pairs for one quadrant
my @q = flat map -> \x { x, y(x) }, ^(0, a+1 ... 200);
 
my $out = open('superellipse.svg', :w);
# Generate an SVG image
$out.print: [~] qq|<svg height="{b×2}" width="{a×2}" xmlns="http://www.w3.org/2000/svg">\n|,
INIT say qq:to/STOP/;
pline( @q ),
<?xml version="1.0" standalone="no"?>
pline( @q «×» < 1 -1> ), # flip and mirror
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
pline( @q «×» <-1 -1> ), # for the other
<svg height="{b*2}" width="{a*2}" version="1.1" xmlns="http://www.w3.org/2000/svg">
pline( @q «×» <-1 1> ), # three quadrants
STOP
END say '</svg>';
 
.put for
pline( @q ),
pline( @q «*» ( 1,-1) ), # flip and mirror
pline( @q «*» (-1,-1) ), # for the other
pline( @q «*» (-1, 1) ); # three quadrants
 
sub pline (@q) {
qq|<polyline points="{@q}"
qq:to/END/;
style="fill:none;stroke:black;stroke-width:3"
<polyline points="{@q}"
style="fill:none; stroke:black; stroke-width:3" transform="translate({a}, {b})" />\n|
}</syntaxhighlight>
END
}</lang>
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/superellipse.svg Superellipse] (offsite image)
 
Line 1,297 ⟶ 1,497:
Here you can see a picture: http://austria-forum.org/af/User/Pachl%20Walter
 
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* Create a BMP file showing a few super ellipses
**********************************************************************/
Line 1,458 ⟶ 1,658:
r=r/ln(b)
Numeric Digits (prec)
Return r+0</langsyntaxhighlight>
 
=={{header|Scala}}==
===Java Swing Interoperability===
<langsyntaxhighlight Scalalang="scala">import java.awt._
import java.awt.geom.Path2D
import java.util
Line 1,565 ⟶ 1,765:
})
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">const (
a = 200,
b = 200,
Line 1,602 ⟶ 1,802:
].each { .print }
 
say '</svg>'</langsyntaxhighlight>
 
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">sca a=200
sca b=200
sca n=2.5
twoway function y=b*(1-(abs(x/a))^n)^(1/n), range(-200 200) || function y=-b*(1-(abs(x/a))^n)^(1/n), range(-200 200)</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
Uses Go's drawing code but produces a more complex image.
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color, Point
 
class Game {
Line 1,670 ⟶ 1,870:
}
}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">def X0=640/2, Y0=480/2, Scale=25.0, N=2.5;
real X, Y; int IX, IY;
 
proc OctPoint; [
Point(X0+IX, Y0-IY, $F);
Point(X0-IX, Y0-IY, $F);
Point(X0+IX, Y0+IY, $F);
Point(X0-IX, Y0+IY, $F);
Point(X0+IY, Y0-IX, $F);
Point(X0-IY, Y0-IX, $F);
Point(X0+IY, Y0+IX, $F);
Point(X0-IY, Y0+IX, $F);
];
 
[SetVid($101); \VESA graphics 640x480x8
IX:= 0;
repeat X:= float(IX)/Scale;
Y:= Pow(200.0 - Pow(X,N), 1.0/N);
IY:= fix(Y*Scale);
OctPoint;
IX:= IX+1;
until IX >= IY;
]</syntaxhighlight>
 
{{out}}
<pre>
http://www.xpl0.org/rcell.gif
</pre>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">open window 700, 600
backcolor 0,0,0
clear window
Line 1,688 ⟶ 1,918:
t=t+.02
line to xp, yp
next i</syntaxhighlight>
 
=={{header|zkl}}==
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
[[File:SuperEllipse.zkl.jpg|250px|thumb|right]]
<langsyntaxhighlight lang="zkl">fcn superEllipse(plot,n,color=0xff0000){ // we'll assume width <= height
a,p:=(plot.w/2).toFloat(), 1.0/n; // just calculate upper right quadrant
foreach x in ([0.0 .. a]){
Line 1,701 ⟶ 1,931:
}
plot
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">w:=h:=600;
plot:=PPM(w+1,h+1,0x909090); plot.cross(w/2,h/2);
foreach n in ([0.01..1, 0.14]){ superEllipse(plot,n, 0x0000bb) }// 0-1: blue
Line 1,708 ⟶ 1,938:
foreach n in ([2.0..10, 1.4]) { superEllipse(plot,n, 0xff0000) }// 2+: red
 
plot.writeJPGFile("superEllipse.jpg");</langsyntaxhighlight>
1,995

edits