How to resolve the algorithm Polyspiral step by step in the Zig programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Polyspiral step by step in the Zig programming language

Table of Contents

Problem Statement

A Polyspiral is a spiral made of multiple line segments, whereby each segment is larger (or smaller) than the previous one by a given amount. Each segment also changes direction at a given angle.

Animate a series of polyspirals, by drawing a complete spiral then incrementing the angle, and (after clearing the background) drawing the next, and so on. Every spiral will be a frame of the animation. The animation may stop as it goes full circle or continue indefinitely. The given input values may be varied. If animation is not practical in your programming environment, you may show a single frame instead.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Polyspiral step by step in the Zig programming language

Source code in the zig programming language

const std = @import("std");
const rl = @cImport({
    @cInclude("raylib.h");
    @cInclude("raymath.h");
});

const SCREEN_WIDTH = 640;
const SCREEN_HEIGHT = 480;
var incr: f32 = 0;

pub fn main() void {
    rl.SetConfigFlags(rl.FLAG_WINDOW_RESIZABLE | rl.FLAG_VSYNC_HINT);
    rl.SetTargetFPS(60);

    rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Polyspiral");

    while (!rl.WindowShouldClose())
        updateDrawFrame();

    rl.CloseWindow();
}

fn updateDrawFrame() void {
    rl.BeginDrawing();

    rl.ClearBackground(rl.BLACK);

    incr = @mod(incr + 0.001, 360);

    drawSpiral(5, std.math.degreesToRadians(f32, incr));

    rl.EndDrawing();
}

fn drawSpiral(_length: f32, _angle: f32) void {
    const width = rl.GetScreenWidth();
    const height = rl.GetScreenHeight();
    var point0 = rl.Vector2{ .x = @as(f32, @floatFromInt(width)) / 2, .y = @as(f32, @floatFromInt(height)) / 2 };
    var length = _length;
    var angle = _angle;
    for (0..150) |_| {
        const line_vector = rl.Vector2Rotate(rl.Vector2{ .x = length, .y = 0 }, angle);
        const point1 = rl.Vector2Add(point0, line_vector);
        rl.DrawLineV(point0, point1, rl.LIME);
        point0 = point1;
        length += 3;
        angle += incr;
        angle = @mod(angle, comptime @as(f32, (2.0 * std.math.pi)));
    }
}


  

You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Ada programming language
You may also check:How to resolve the algorithm Zsigmondy numbers step by step in the SETL programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Java programming language
You may also check:How to resolve the algorithm A+B step by step in the Objeck programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the jq programming language