Basics of Vala

Description of the page content

Conversion of old BASIC programs to Vala in order to learn the basics of this language.

Tags:

3D Plot

/*
3D Plot

Original version in BASIC:
    Creative Computing (Morristown, New Jersey, USA), ca. 1980.

This version in Vala:
    Copyright (c) 2023, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2023-08-30/31.

Last modified 20231103T2132+0100.
*/

using GLib; // Math needed

// Clear the screen, reset the attributes and move the cursor to the home position.
void clear() {
    print("\033[2J\033[0m\033[H");
}

// Display the credits and wait until the user presses Enter.
void print_credits() {
    print("3D Plot\n\n");
    print("Original version in BASIC:\n");
    print("    Creative computing (Morristown, New Jersey, USA), ca. 1980.\n\n");
    print("This version in Vala:\n");
    print("    Copyright (c) 2023, Marcos Cruz (programandala.net)\n");
    print("    SPDX-License-Identifier: Fair\n\n");
    print("Press Enter to start the program.\n");
    stdin.read_line();
}

double a(double z) {
    return 30 * Math.exp(-z * z / 100);
}

void draw() {
    const int width = 56;
    const char space = ' ';
    const char dot   = '*';
    char line[width];
    int l = 0;
    int z = 0;
    int y1 = 0;
    for (double x = -30.0; x <= 30.0; x += 1.5) {
        for (int pos = 0; pos < width; pos++) {
            line[pos] = space;
        }
        l = 0;
        y1 = 5 * (int)(Math.sqrt(900 - x * x) / 5);
        for (int y = y1; y >= -y1; y += -5) {
            z = (int)(25 + a(Math.sqrt(x * x + (y * y))) - 0.7 * y);
            if (z > l) {
                l = z;
                line[z] = dot;
            }
        } // y loop
        for (int pos = 0; pos < width; pos++) {
            print(line[pos].to_string());
        }
        print("\n");
    } // x loop
}

void main() {
    clear();
    print_credits();
    clear();
    draw();
}

Bunny

/*
Bunny

Original version in BASIC:
    Creative Computing (Morristown, New Jersey, USA), 1978.

This version in Vala:
    Copyright (c) 2023, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2023-08-31.

Last modified 20231103T2132+0100.
*/

// Erase the screen, reset the attributes and move the cursor to the home position.
void clear_screen() {
    print("\x1B[2J\x1B[0m\x1B[H");
}

// Print the credits and wait for a keypress.
void print_credits() {
    print("Bunny\n\n");
    print("Original version in BASIC:\n");
    print("    Creative Computing (Morristown, New Jersey, USA), 1978.\n\n");
    print("This version in Vala:\n");
    print("    Copyright (c) 2023, Marcos Cruz (programandala.net)\n");
    print("    SPDX-License-Identifier: Fair\n\n");
    print("Press Enter to start the program.\n");
    stdin.read_line();
}

const int WIDTH = 53;
unichar line[WIDTH]; // buffer
int col; // `line` index

// Clear the line buffer with spaces.
void clear_line() {
    for (int c = 0; c < WIDTH; c++) {
        line[c] = ' ';
    }
    col = 0;
}

// Print the line buffer.
void print_line() {
    for (int c = 0; c < WIDTH; c++) {
        print(line[c].to_string());
    }
    print("\n");
}

const string LETTER = "BUNNY"; // letters pattern
const int EOL = -1; // end of line identifier
const int[] data = {
    1, 2, EOL, 0, 2, 45, 50, EOL, 0, 5, 43, 52, EOL, 0, 7, 41, 52, EOL,
    1, 9, 37, 50, EOL, 2, 11, 36, 50, EOL, 3, 13, 34, 49, EOL, 4, 14,
    32, 48, EOL, 5, 15, 31, 47, EOL, 6, 16, 30, 45, EOL, 7, 17, 29, 44,
    EOL, 8, 19, 28, 43, EOL, 9, 20, 27, 41, EOL, 10, 21, 26, 40, EOL,
    11, 22, 25, 38, EOL, 12, 22, 24, 36, EOL, 13, 34, EOL, 14, 33, EOL,
    15, 31, EOL, 17, 29, EOL, 18, 27, EOL, 19, 26, EOL, 16, 28, EOL,
    13, 30, EOL, 11, 31, EOL, 10, 32, EOL, 8, 33, EOL, 7, 34, EOL, 6,
    13, 16, 34, EOL, 5, 12, 16, 35, EOL, 4, 12, 16, 35, EOL, 3, 12, 15,
    35, EOL, 2, 35, EOL, 1, 35, EOL, 2, 34, EOL, 3, 34, EOL, 4, 33,
    EOL, 6, 33, EOL, 10, 32, 34, 34, EOL, 14, 17, 19, 25, 28, 31, 35,
    35, EOL, 15, 19, 23, 30, 36, 36, EOL, 14, 18, 21, 21, 24, 30, 37, 37,
    EOL, 13, 18, 23, 29, 33, 38, EOL, 12, 29, 31, 33, EOL, 11, 13, 17,
    17, 19, 19, 22, 22, 24, 31, EOL, 10, 11, 17, 18, 22, 22, 24, 24, 29,
    29, EOL, 22, 23, 26, 29, EOL, 27, 29, EOL, 28, 29, EOL };
const uint DATA_LEN = data.length;

// Draw the graphic out of `data` and `LETTER`.
void draw() {
    uint letters = LETTER.length;
    int to_col; // last col to print to
    uint d = 0; // data pointer
    clear_line();
    while (d < DATA_LEN) {
        col = data[d];
        d += 1;
        if (col == EOL) {
            print_line();
            clear_line();
        } else {
            to_col = data[d];
            d += 1;
            for (int c = col; c <= to_col; c++) {
                line[c] = LETTER[c % letters];
            }
        }
    }
}

void main() {
    clear_screen();
    print_credits();
    clear_screen();
    draw();
}

Diamond

/*
Diamond

Original version in BASIC:
    Example included in Vintage BASIC 1.0.3.
    http://www.vintage-basic.net

This version in Vala:
    Copyright (c) 2023, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2023-08-30/31.

Last modified 20231103T2130+0100.
*/

void main() {

    const int lines = 17;
    int i = 1;
    int j;

    while (i <= lines / 2 + 1) {
        j = 1;
        while (j <= (lines + 1) / 2 - i + 1) {
            print(" ");
            j += 1;
        }
        j = 1;
        while (j <= i * 2 - 1) {
            print("*");
            j += 1;
        }
        print("\n");
        i += 1;
    }
    i = 1;
    while (i <= lines / 2) {
        j = 1;
        while (j <= i + 1) {
            print(" ");
            j += 1;
        }
        j = 1;
        while (j <= ((lines + 1) / 2 - i) * 2 - 1) {
            print("*");
            j += 1;
        }
        print("\n");
        i += 1;
    }
}

Sine Wave

/*
Sine Wave

Original version in BASIC:
    Creative Computing (Morristown, New Jersey, USA), ca. 1980.

This version in Vala:
    Copyright (c) 2023, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written in 2023-08, 2023-09.

Last modified 20231103T2132+0100.
*/

using GLib; // Math needed

// Erase the screen, reset the attributes and move the cursor to the home position.
void clear() {
    print("\033[2J\033[0m\033[H");
}

void print_credits() {
    print("Sine Wave\n\n");
    print("Original version in BASIC:\n");
    print("    Creative computing (Morristown, New Jersey, USA), ca. 1980.\n\n");
    print("This version in Vala:\n");
    print("    Copyright (c) 2023, Marcos Cruz (programandala.net)\n");
    print("    SPDX-License-Identifier: Fair\n\n");
    print("Press Enter to start the program.\n");
    stdin.read_line();
}

string word[2]; // user words

void get_words() {
    string order[2] = {"first", "second"};
    for (int i = 0; i < word.length; i++) {
        print(@"Enter the $(order[i]) word: ");
        word[i] = stdin.read_line();
    }

}

string repeat_char(char c, int times) {
    string s = "";
    for (int i = 0; i < times; i++) {
        s += c.to_string();
    }
    return s;
}

void draw() {
    bool even = false;
    double angle = 0.0;
    for (angle = 0.0; angle <= 40.0; angle += 0.25) {
        print(repeat_char(' ', (int) (26 + 25 * Math.sin(angle))));
        stdout.printf("%s\n", word[(int) even]);
        even = !even;
    }
}

void main() {
    clear();
    print_credits();
    clear();
    get_words();
    clear();
    draw();
}

Related pages

Basics off
Metaproject about the "Basics of…" projects.
Basics of 8th
Conversion of old BASIC programs to 8th in order to learn the basics of this language.
Basics of Ada
Conversion of old BASIC programs to Ada in order to learn the basics of this language.
Basics of Arturo
Conversion of old BASIC programs to Arturo in order to learn the basics of this language.
Basics of C#
Conversion of old BASIC programs to C# in order to learn the basics of this language.
Basics of C3
Conversion of old BASIC programs to C3 in order to learn the basics of this language.
Basics of Chapel
Conversion of old BASIC programs to Chapel in order to learn the basics of this language.
Basics of Clojure
Conversion of old BASIC programs to Clojure in order to learn the basics of this language.
Basics of Crystal
Conversion of old BASIC programs to Crystal in order to learn the basics of this language.
Basics of D
Conversion of old BASIC programs to D in order to learn the basics of this language.
Basics of Elixir
Conversion of old BASIC programs to Elixir in order to learn the basics of this language.
Basics of F#
Conversion of old BASIC programs to F# in order to learn the basics of this language.
Basics of Factor
Conversion of old BASIC programs to Factor in order to learn the basics of this language.
Basics of FreeBASIC
Conversion of old BASIC programs to FreeBASIC in order to learn the basics of this language.
Basics of Gleam
Conversion of old BASIC programs to Gleam in order to learn the basics of this language.
Basics of Go
Conversion of old BASIC programs to Go in order to learn the basics of this language.
Basics of Hare
Conversion of old BASIC programs to Hare in order to learn the basics of this language.
Basics of Haxe
Conversion of old BASIC programs to Haxe in order to learn the basics of this language.
Basics of Icon
Conversion of old BASIC programs to Icon in order to learn the basics of this language.
Basics of Io
Conversion of old BASIC programs to Io in order to learn the basics of this language.
Basics of Janet
Conversion of old BASIC programs to Janet in order to learn the basics of this language.
Basics of Julia
Conversion of old BASIC programs to Julia in order to learn the basics of this language.
Basics of Kotlin
Conversion of old BASIC programs to Kotlin in order to learn the basics of this language.
Basics of Lobster
Conversion of old BASIC programs to Lobster in order to learn the basics of this language.
Basics of Lua
Conversion of old BASIC programs to Lua in order to learn the basics of this language.
Basics of Nature
Conversion of old BASIC programs to Nature in order to learn the basics of this language.
Basics of Neat
Conversion of old BASIC programs to Neat in order to learn the basics of this language.
Basics of Neko
Conversion of old BASIC programs to Neko in order to learn the basics of this language.
Basics of Nelua
Conversion of old BASIC programs to Nelua in order to learn the basics of this language.
Basics of Nim
Conversion of old BASIC programs to Nim in order to learn the basics of this language.
Basics of Nit
Conversion of old BASIC programs to Nit in order to learn the basics of this language.
Basics of Oberon-07
Conversion of old BASIC programs to Oberon-07 in order to learn the basics of this language.
Basics of OCaml
Conversion of old BASIC programs to OCaml in order to learn the basics of this language.
Basics of Odin
Conversion of old BASIC programs to Odin in order to learn the basics of this language.
Basics of Pike
Conversion of old BASIC programs to Pike in order to learn the basics of this language.
Basics of Pony
Conversion of old BASIC programs to Pony in order to learn the basics of this language.
Basics of Python
Conversion of old BASIC programs to Python in order to learn the basics of this language.
Basics of Racket
Conversion of old BASIC programs to Racket in order to learn the basics of this language.
Basics of Raku
Conversion of old BASIC programs to Raku in order to learn the basics of this language.
Basics of Retro
Conversion of old BASIC programs to Retro in order to learn the basics of this language.
Basics of Rexx
Conversion of old BASIC programs to Rexx in order to learn the basics of this language.
Basics of Ring
Conversion of old BASIC programs to Ring in order to learn the basics of this language.
Basics of Rust
Conversion of old BASIC programs to Rust in order to learn the basics of this language.
Basics of Scala
Conversion of old BASIC programs to Scala in order to learn the basics of this language.
Basics of Scheme
Conversion of old BASIC programs to Scheme in order to learn the basics of this language.
Basics of Styx
Conversion of old BASIC programs to Styx in order to learn the basics of this language.
Basics of Swift
Conversion of old BASIC programs to Swift in order to learn the basics of this language.
Basics of V
Conversion of old BASIC programs to V in order to learn the basics of this language.
Basics of Zig
Conversion of old BASIC programs to Zig in order to learn the basics of this language.

External related links