Basics of Haxe
Descripción del contenido de la página
Conversión de antiguos programas de BASIC a Haxe para aprender los rudimentos de este lenguaje.
Etiquetas:
3D Plot
/*
D3 Plot
Original version in BASIC, "3D Plot":
Creative Computing (Morristown, New Jersey, USA), ca. 1980.
This version in Haxe:
Copyright (c) 2024, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written on 2024-12-19.
Last modified: 20241219T1328+0100.
*/
class D3_plot {
static function clear_screen() {
Sys.print("\x1B[0;0H\x1B[2J");
}
static function print_credits() {
Sys.println("3D Plot\n");
Sys.println("Original version in BASIC:");
Sys.println(" Creative computing (Morristown, New Jersey, USA), ca. 1980.\n");
Sys.println("This version in Haxe:");
Sys.println(" Copyright (c) 2024, Marcos Cruz (programandala.net)");
Sys.println(" SPDX-License-Identifier: Fair\n");
Sys.print("Press Enter to start the program. ");
Sys.stdin().readLine();
}
static function a(z : Float) {
return 30 * Math.exp(-z * z / 100);
}
static function draw() {
final WIDTH = 56;
final SPACE = ' ';
final DOT = '*';
var l = 0;
var z = 0;
var y = 0;
var y1 = 0;
var line : Array<String> = [];
for (pos in 0 ... WIDTH) {
line.push(SPACE);
}
var x = -30.0;
while (x <= 30.0) {
for (pos in 0 ... WIDTH) {
line[pos] = SPACE;
}
l = 0;
y1 = 5 * Std.int(Math.sqrt(900 - x * x) / 5);
y = y1;
while (y >= -y1) {
z = Std.int(25 + a(Math.sqrt(x * x + cast(y * y, Float))) - 0.7 * y);
if (z > l) {
l = z;
line[z] = DOT;
};
y += -5;
} // y loop
for (pos in 0 ... WIDTH) {
Sys.print(line[pos]);
}
Sys.println("");
x += 1.5;
} // x loop
}
static function 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 Haxe:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written on 2023-08-24, 2024-12-19.
Last modified 20241219T1225+0100.
*/
class Diamond {
static public function main() {
var lines = 17;
var i;
var j;
i = 1;
while (i <= lines / 2 + 1) {
j = 1;
while (j <= (lines + 1) / 2 - i + 1) {
Sys.print(" ");
j += 1;
}
j = 1;
while (j <= i * 2 - 1) {
Sys.print("*");
j += 1;
}
Sys.println("");
i += 1;
}
i = 1;
while (i <= lines / 2) {
j = 1;
while (j <= i + 1) {
Sys.print(" ");
j += 1;
}
j = 1;
while (j <= ((lines + 1) / 2 - i) * 2 - 1) {
Sys.print("*");
j += 1;
}
Sys.println("");
i += 1;
}
}
}
Sine Wave
/*
Sine Wave
Original version in BASIC:
Creative Computing (Morristown, New Jersey, USA), ca. 1980.
This version in Haxe:
Copyright (c) 2024, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written on 2024-12-19.
Last modified: 20241219T1611+0100.
*/
class Sine_wave {
static function clear_screen() {
Sys.print("\x1B[0;0H\x1B[2J");
}
static var word : Array<String> = ["", ""];
// Ask the user to enter two words and store them.
//
static function get_words() {
final order : Array<String> = ["first", "second"];
clear_screen();
for (n in 0 ... 2) {
while (word[n] == "") {
Sys.print("Enter the " + order[n] + " word: ");
word[n] = Sys.stdin().readLine();
}
}
}
static function print_credits() {
Sys.println("Sine Wave\n");
Sys.println("Original version in BASIC:");
Sys.println(" Creative Computing (Morristown, New Jersey, USA), ca. 1980.\n");
Sys.println("This version in Haxe:");
Sys.println(" Copyright (c) 2024, Marcos Cruz (programandala.net)");
Sys.println(" SPDX-License-Identifier: Fair\n");
Sys.print("Press Enter to start the program. ");
Sys.stdin().readLine();
}
static function draw() {
get_words();
var even = false;
var angle = 0.0;
while (angle <= 40.0) {
for (i in 0 ... Std.int(26 + 25 * Math.sin(angle))) {
Sys.print(" ");
}
Sys.println(word[even ? 1 : 0]);
even = ! even;
angle += 0.25;
}
}
static function main() {
clear_screen();
print_credits();
clear_screen();
draw();
}
}
