A superellipse is a geometric figure defined as the set of all points (x, y) with



 





|


x
a


|


n



+


|


y
b


|


n



=
1
,


{\displaystyle \left|{\frac {x}{a}}\right|^{n}\!+\left|{\frac {y}{b}}\right|^{n}\!=1,}




where n, a, and b are positive numbers.



Task
Draw a superellipse with n = 2.5, and a = b = 200




import math
import imageman
 
const
  Size = 600
  X0 = Size div 2
  Y0 = Size div 2
  Background = ColorRGBU [byte 0, 0, 0]
  Foreground = ColorRGBU [byte 255, 255, 255]
 
 
proc drawSuperEllipse(img: var Image; n: float; a, b: int) =
 
  var yList = newSeq[int](a + 1)
  for x in 0..a:
    let an = pow(a.toFloat, n)
    let bn = pow(b.toFloat, n)
    let xn = pow(x.toFloat, n)
    let t = max(bn - xn * bn / an, 0.0)   # Avoid negative values due to rounding errors.
    yList[x] = pow(t, 1/n).toInt
 
  var pos: seq[Point]
  for x in countdown(a, 0):
    pos.add (X0 + x, Y0 - yList[x])
  for x in 0..a:
    pos.add (X0 - x, Y0 - yList[x])
  for x in countdown(a, 0):
    pos.add (X0 - x, Y0 + yList[x])
  for x in 0..a:
    pos.add (X0 + x, Y0 + yList[x])
  img.drawPolyline(true, Foreground, pos)
 
 
var image = initImage[ColorRGBU](Size, Size)
image.fill(Background)
image.drawSuperEllipse(2.5, 200, 200)
image.savePNG("super_ellipse.png", compression = 9)