How to resolve the algorithm Superellipse step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Superellipse step by step in the Nim programming language

Table of Contents

Problem Statement

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

where n, a, and b are positive numbers.

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Superellipse step by step in the Nim programming language

Source code in the nim programming language

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)


  

You may also check:How to resolve the algorithm Singleton step by step in the Sidef programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Logo programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Pascal programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Ring programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the Wren programming language