How to resolve the algorithm Plasma effect step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Plasma effect step by step in the Wren programming language

Table of Contents

Problem Statement

The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.

Create a plasma effect.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Plasma effect step by step in the Wren programming language

Source code in the wren programming language

import "graphics" for Canvas, Color, ImageData
import "dome" for Window
import "math" for Math

class PlasmaEffect {
    construct new(width, height) {
        Window.title = "Plasma Effect"
        Window.resize(width, height)
        Canvas.resize(width, height)
        _w = width
        _h = height
        _bmp = ImageData.create("plasma_effect", _w, _h)
        _plasma = createPlasma() // stores the hues for the colors
    }

    init() {
        _frame = 0
        _hueShift = 0
        Canvas.cls(Color.white)
    }

    createPlasma() {
        var buffer = List.filled(_w, null)
        for (x in 0..._w) {
            buffer[x] = List.filled(_h, 0)
            for (y in 0..._h) {
                var value = Math.sin(x / 16)
                value = value + Math.sin(y / 8)
                value = value + Math.sin((x + y) / 16)
                value = value + Math.sin((x * x + y * y).sqrt / 8)
                value = value + 4  // shift range from -4 .. 4 to 0 .. 8
                value = value / 8  // bring range down to 0 .. 1
                if (value < 0 || value > 1) Fiber.abort("Hue value out of bounds")
                buffer[x][y] = value
                pset(x, y, Color.hsv(value * 360, 1, 1))
            }
        }
        return buffer
    }

    pset(x, y, col) { _bmp.pset(x, y, col) }

    pget(x, y) { _bmp.pget(x, y) }

    update() {
        _frame = _frame + 1
        if (_frame % 3 == 0) {  // update every 3 frames or 1/20th second
            _hueShift = (_hueShift + 0.02) % 1
        }
    }

    draw(alpha) {
        for (x in 0..._w) {
            for (y in 0..._h) {
                var hue = (_hueShift + _plasma[x][y]) % 1
                var col = Color.hsv(hue * 360, 1, 1)
                pset(x, y, col)
            }
        }
        _bmp.draw(0, 0)
    }
}

var Game = PlasmaEffect.new(640, 640)

  

You may also check:How to resolve the algorithm Order disjoint list items step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Negative base numbers step by step in the Racket programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the C# programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the Erlang programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Zoea Visual programming language