Basics of Rust

Descrition del contenete del págine

Conversion de old BASIC-programas a Rust por aprender lu elementari de ti-ci lingue.

Etiquettes:

3D Plot

/*
3D Plot

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

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

Written on 2024-12-26/27.

Last modified: 20241227T1235+0100.
*/

use std::io;
use std::io::Write;

const SPACE: char = ' ';
const DOT: char = '*';
const WIDTH: usize = 56;

fn move_cursor_home() {

    print!("\x1B[H")

}

fn erase_screen() {

    print!("\x1B[2J")

}

fn clear_screen() {

    erase_screen();
    move_cursor_home()

}

fn press_enter(prompt: &str) {

    print!("{prompt}");
    io::stdout().flush().unwrap(); // print pending text
    let mut input = String::new();
    let _ = io::stdin().read_line(&mut input);

}

// Display the credits and wait for a keypress.
//
fn print_credits() {

    println!("3D Plot\n");
    println!("Original version in BASIC:");
    println!("    Creative computing (Morristown, New Jersey, USA), ca. 1980.\n");
    println!("This version in Rust:");
    println!("    Copyright (c) 2024, Marcos Cruz (programandala.net)");
    println!("    SPDX-License-Identifier: Fair\n");
    press_enter("Press Enter to start the program. ")

}

fn a(z: f64) -> f64 {

    return 30.0 * f64::exp(-z * z / 100.0)

}

fn draw() {

    let mut l: isize;
    let mut z: isize;
    let mut y1: isize;
    let mut line : [char; WIDTH];

    let mut x: f64 = -30.0;
    while x <= 30.0 {
        line = [SPACE; WIDTH];
        l = 0;
        y1 = 5 * (f64::sqrt(900.0 - x * x) / 5.0) as isize;
        let mut y = y1;
        while y >= -y1 {
            z = (25.0 + a(f64::sqrt(x * x + (y * y) as f64)) - 0.7 * (y as f64)) as isize;
            if z > l {
                l = z;
                line[z as usize] = DOT
            }
            y += -5
        } // y loop
        for pos in 0 .. WIDTH {
            print!("{}", line[pos])
        }
        println!();
        x += 1.5
    } // x loop

}

fn 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 Rust:
    Copyright (c) 2024, Marcos Cruz (programandala.net)
    SPDX-License-Identifier: Fair

Written on 2024-12-26.

Last modified: 20241226T2041+0100.
*/


const LINES:isize = 17;

fn main() {

    for i in 1 ..= LINES / 2 + 1 {
        for _ in 1 ..= (LINES + 1) / 2 - i + 1 {
            print!(" ")
        }
        for _ in 1 ..= i * 2 - 1 {
            print!("*")
        }
        println!()
    }
    for i in 1 ..= LINES / 2 {
        for _ in 1 ..= i + 1 {
            print!(" ")
        }
        for _ in 1 ..= ((LINES + 1) / 2 - i) * 2 - 1 {
            print!("*")
        }
        println!()
    }

}

Sine Wave

/*
Sine Wave

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

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

Written in 2024-12-27/28.

Last modified: 20241228T2054+0100.
*/

use std::io;
use std::io::Write;

fn move_cursor_home() {

    print!("\x1B[H")

}

fn erase_screen() {

    print!("\x1B[2J")

}

fn clear_screen() {

    erase_screen();
    move_cursor_home();

}

fn read_string(prompt: &str) -> String {

    print!("{prompt}");
    io::stdout().flush().unwrap(); // print pending text

    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("");
    return input;

}

// Display the credits and wait for a keypress.
//
fn print_credits() {

    println!("Sine Wave\n");
    println!("Original version in BASIC:");
    println!("    Creative Computing (Morristown, New Jersey, USA), ca. 1980.\n");
    println!("This version in Rust:");
    println!("    Copyright (c) 2024, Marcos Cruz (programandala.net)");
    println!("    SPDX-License-Identifier: Fair\n");
    let _ = read_string("Press Enter to start the program. ");

}

fn draw() {

    let mut even = false;
    let mut angle = 0.0;

    const ORDER: [&str; 2]  = ["first", "second"];
    let mut word: [String; 2] = ["".into(), "".into()];

    for n in 0 ..= 1 {
        while word[n].is_empty() {
            let prompt = format!("Enter the {} word: ", ORDER[n]);
            word[n] = read_string(&prompt).trim().to_string();
        }
    }

    while angle <= 40.0 {
        let margin = 26 + f64::floor(25.0 * f64::sin(angle)) as isize;
        for _ in 0 .. margin {
            print!(" ");
        }
        println!("{}", word[even as usize]);
        even = ! even;
        angle += 0.25;
    }

}

fn main() {

    clear_screen();
    print_credits();
    clear_screen();
    draw();

}

Págines relatet

Basics off
Metaprojecte pri li projectes «Basics of…».
Basics of 8th
Conversion de old BASIC-programas a 8th por aprender lu elementari de ti-ci lingue.
Basics of Ada
Conversion de old BASIC-programas a Ada por aprender lu elementari de ti-ci lingue.
Basics of Arturo
Conversion de old BASIC-programas a Arturo por aprender lu elementari de ti-ci lingue.
Basics of C#
Conversion de old BASIC-programas a C# por aprender lu elementari de ti-ci lingue.
Basics of C3
Conversion de old BASIC-programas a C3 por aprender lu elementari de ti-ci lingue.
Basics of Chapel
Conversion de old BASIC-programas a Chapel por aprender lu elementari de ti-ci lingue.
Basics of Clojure
Conversion de old BASIC-programas a Clojure por aprender lu elementari de ti-ci lingue.
Basics of Crystal
Conversion de old BASIC-programas a Crystal por aprender lu elementari de ti-ci lingue.
Basics of D
Conversion de old BASIC-programas a D por aprender lu elementari de ti-ci lingue.
Basics of Elixir
Conversion de old BASIC-programas a Elixir por aprender lu elementari de ti-ci lingue.
Basics of F#
Conversion de old BASIC-programas a F# por aprender lu elementari de ti-ci lingue.
Basics of Factor
Conversion de old BASIC-programas a Factor por aprender lu elementari de ti-ci lingue.
Basics of FreeBASIC
Conversion de old BASIC-programas a FreeBASIC por aprender lu elementari de ti-ci lingue.
Basics of Gleam
Conversion de old BASIC-programas a Gleam por aprender lu elementari de ti-ci lingue.
Basics of Go
Conversion de old BASIC-programas a Go por aprender lu elementari de ti-ci lingue.
Basics of Hare
Conversion de old BASIC-programas a Hare por aprender lu elementari de ti-ci lingue.
Basics of Haxe
Conversion de old BASIC-programas a Haxe por aprender lu elementari de ti-ci lingue.
Basics of Icon
Conversion de old BASIC-programas a Icon por aprender lu elementari de ti-ci lingue.
Basics of Io
Conversion de old BASIC-programas a Io por aprender lu elementari de ti-ci lingue.
Basics of Janet
Conversion de old BASIC-programas a Janet por aprender lu elementari de ti-ci lingue.
Basics of Julia
Conversion de old BASIC-programas a Julia por aprender lu elementari de ti-ci lingue.
Basics of Kotlin
Conversion de old BASIC-programas a Kotlin por aprender lu elementari de ti-ci lingue.
Basics of Lobster
Conversion de old BASIC-programas a Lobster por aprender lu elementari de ti-ci lingue.
Basics of Lua
Conversion de old BASIC-programas a Lua por aprender lu elementari de ti-ci lingue.
Basics of Nature
Conversion de old BASIC-programas a Nature por aprender lu elementari de ti-ci lingue.
Basics of Neat
Conversion de old BASIC-programas a Neat por aprender lu elementari de ti-ci lingue.
Basics of Neko
Conversion de old BASIC-programas a Neko por aprender lu elementari de ti-ci lingue.
Basics of Nelua
Conversion de old BASIC-programas a Nelua por aprender lu elementari de ti-ci lingue.
Basics of Nim
Conversion de old BASIC-programas a Nim por aprender lu elementari de ti-ci lingue.
Basics of Nit
Conversion de old BASIC-programas a Nit por aprender lu elementari de ti-ci lingue.
Basics of Oberon-07
Conversion de old BASIC-programas a Oberon-07 por aprender lu elementari de ti-ci lingue.
Basics of OCaml
Conversion de old BASIC-programas a OCaml por aprender lu elementari de ti-ci lingue.
Basics of Odin
Conversion de old BASIC-programas a Odin por aprender lu elementari de ti-ci lingue.
Basics of Pike
Conversion de old BASIC-programas a Pike por aprender lu elementari de ti-ci lingue.
Basics of Pony
Conversion de old BASIC-programas a Pony por aprender lu elementari de ti-ci lingue.
Basics of Python
Conversion de old BASIC-programas a Python por aprender lu elementari de ti-ci lingue.
Basics of Racket
Conversion de old BASIC-programas a Racket por aprender lu elementari de ti-ci lingue.
Basics of Raku
Conversion de old BASIC-programas a Raku por aprender lu elementari de ti-ci lingue.
Basics of Retro
Conversion de old BASIC-programas a Retro por aprender lu elementari de ti-ci lingue.
Basics of Rexx
Conversion de old BASIC-programas a Rexx por aprender lu elementari de ti-ci lingue.
Basics of Ring
Conversion de old BASIC-programas a Ring por aprender lu elementari de ti-ci lingue.
Basics of Scala
Conversion de old BASIC-programas a Scala por aprender lu elementari de ti-ci lingue.
Basics of Scheme
Conversion de old BASIC-programas a Scheme por aprender lu elementari de ti-ci lingue.
Basics of Styx
Conversion de old BASIC-programas a Styx por aprender lu elementari de ti-ci lingue.
Basics of Swift
Conversion de old BASIC-programas a Swift por aprender lu elementari de ti-ci lingue.
Basics of V
Conversion de old BASIC-programas a V por aprender lu elementari de ti-ci lingue.
Basics of Vala
Conversion de old BASIC-programas a Vala por aprender lu elementari de ti-ci lingue.
Basics of Zig
Conversion de old BASIC-programas a Zig por aprender lu elementari de ti-ci lingue.

Extern ligamentes relatet