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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Superellipse step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "graphics" for Canvas, Color, Point

class Game {
    static init() {
        Canvas.resize(500, 500)
        // draw 200 concentric superellipses with gradually decreasing 'n'.
        for (a in 200..1) {
            superEllipse(a/80, a)
        }
    }

    static update() {}

    static draw(alpha) {}

    static superEllipse(n, a) {
        var hw = Canvas.width / 2
        var hh = Canvas.height / 2

        // calculate y for each x
        var y = List.filled(a + 1, 0)
        for (x in 0..a) {
            var aa = a.pow(n)
            var xx = x.pow(n)
            y[x] = (aa-xx).pow(1/n)
        }

        // draw quadrants
        var prev = Point.new(hw + a, hh - y[a])
        for (x in a-1..0) {
            var curr = Point.new(hw + x, hh - y[x])
            Canvas.line(prev.x, prev.y, curr.x, curr.y, Color.white)
            prev = Point.new(curr.x, curr.y)
        }

        prev = Point.new(hw, hh + y[0])
        for (x in 1..a) {
            var curr = Point.new(hw + x, hh + y[x])
            Canvas.line(prev.x, prev.y, curr.x, curr.y, Color.white)
            prev = Point.new(curr.x, curr.y)
        }

        prev = Point.new(hw - a, hh + y[a])
        for (x in a-1..0) {
            var curr = Point.new(hw - x, hh + y[x])
            Canvas.line(prev.x, prev.y, curr.x, curr.y, Color.white)
            prev = Point.new(curr.x, curr.y)
        }

        prev = Point.new(hw, hh - y[0])
        for (x in 1..a) {
            var curr = Point.new(hw - x, hh - y[x])
            Canvas.line(prev.x, prev.y, curr.x, curr.y, Color.white)
            prev = Point.new(curr.x, curr.y)
        }
    }
}

  

You may also check:How to resolve the algorithm Index finite lists of positive integers step by step in the Arturo programming language
You may also check:How to resolve the algorithm Jacobsthal numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Main step of GOST 28147-89 step by step in the C++ programming language
You may also check:How to resolve the algorithm Anagrams step by step in the C# programming language
You may also check:How to resolve the algorithm Padovan n-step number sequences step by step in the C programming language