aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-09-30 00:18:27 -0500
committernic-gaffney <gaffney_nic@protonmail.com>2023-09-30 00:18:27 -0500
commitc748aedbc265fdc7d62768d368161a1f1d88b9a4 (patch)
tree2371ae4c1ce98e72c9e414f0e4176c3d8bf35778 /examples
parent84c77656e1d5f0414050d8a2dae6c86b022bf125 (diff)
downloadsloth-c748aedbc265fdc7d62768d368161a1f1d88b9a4.tar.gz
spacing
Diffstat (limited to 'examples')
-rw-r--r--examples/cgol.sloth136
-rw-r--r--examples/mandelbrot.sloth55
2 files changed, 92 insertions, 99 deletions
diff --git a/examples/cgol.sloth b/examples/cgol.sloth
index a0e1878..3700395 100644
--- a/examples/cgol.sloth
+++ b/examples/cgol.sloth
@@ -1,74 +1,66 @@
fn populate() [Int]
{
- # Initialize life vector
- var life: [Int] = [0];
- vpopi(life);
+ # Initialize life vector
+ var life: [Int] = [0];
+ vpopi(life);
- # Fill the vector with random values
- var i: Int = 0;
- while i < 57600
+ # Fill the vector with random values
+ var i: Int = 0;
+ while i < 57600
{
- var n: Int = randGen(0,1);
- vpushi(life, n);
- i = i+1;
- }
+ var n: Int = randGen(0,1);
+ vpushi(life, n);
+ i = i+1;
+ }
- return life;
+ return life;
}
-fn coord(x: Int, y: Int) Int
+fn coord(x: Int, y: Int) Int
{
- var res: Int = -1;
- # Calculate index based on coordinates
- if x >= 0 && y >= 0
+ var res: Int = -1;
+ # Calculate index based on coordinates
+ if x >= 0 && y >= 0
{
- res = y*240+ x;
- }
- # if coordinate is invalid, return -1
- return res;
-}
-
-fn cval(x: Int, y: Int, life: [Int]) Int
-{
- # Check to make sure index exists before returning
- var res: Int = 0;
- var c: Int = coord(x, y);
- if c >= 0 {
- res = vgeti(life, c);
- }
- return res;
+ res = y*240+ x;
+ }
+ # if coordinate is invalid, return -1
+ return res;
}
-fn gol(total: Int, alive: Bool) Int
+fn cval(x: Int, y: Int, life: [Int]) Int
{
-
- if !alive && total == 3 {
- return 1;
+ # Check to make sure index exists before returning
+ var res: Int = 0;
+ var c: Int = coord(x, y);
+ if c >= 0 {
+ res = vgeti(life, c);
}
- if alive && ()
- return 0;
+ return res;
}
+
+
fn update(life: [Int], new: [Int])
{
- # Iterate through life
- for x in 0..64
+ # Iterate through life
+ for x in 0..64
{
- for y in 0..240
+ for y in 0..240
{
- # Calculate total score around selected cell
- var total: Int =
+ # Calculate total score around selected cell
+ var total: Int =
cval(x-1, y-1, life) + # Top Left
- cval(x-1, y , life) +
- cval(x-1, y+1, life) +
- cval(x , y-1, life) +
- cval(x , y+1, life) +
- cval(x+1, y-1, life) +
- cval(x+1, y , life) +
+ cval(x-1, y , life) +
+ cval(x-1, y+1, life) +
+ cval(x , y-1, life) +
+ cval(x , y+1, life) +
+ cval(x+1, y-1, life) +
+ cval(x+1, y , life) +
cval(x+1, y+1, life);
- # Apply game of life rules
-
+ # Apply game of life rules
+
var idx: Int = coord(x, y);
if cval(x, y, life) == 1
@@ -93,36 +85,36 @@ fn update(life: [Int], new: [Int])
vseti(new, idx, 0);
}
}
- }
- }
+ }
+ }
}
fn display(life: [Int]) {
- # Iterate through life
- for x in 3..62 {
- for y in 0..240 {
- termpos(x-3, y);
- if cval(x-3, y, life) == 1 {
- print("█");
- } else {
- print(" ");
- }
- }
- }
+ # Iterate through life
+ for x in 3..62 {
+ for y in 0..240 {
+ termpos(x-3, y);
+ if cval(x-3, y, life) == 1 {
+ print("█");
+ } else {
+ print(" ");
+ }
+ }
+ }
}
fn main() Int {
- # Populate
- var life: [Int] = populate();
- display(life);
+ # Populate
+ var life: [Int] = populate();
+ display(life);
curshide();
- # Play forever
- while true {
+ # Play forever
+ while true {
var new: [Int] = populate();
- update(life, new);
- display(new);
+ update(life, new);
+ display(new);
life = new;
- wait(100);
- }
- return 0;
+ wait(100);
+ }
+ return 0;
}
diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth
index 275d32b..09286bd 100644
--- a/examples/mandelbrot.sloth
+++ b/examples/mandelbrot.sloth
@@ -1,3 +1,26 @@
+fn printMan(size: Float, maxVal: Float, maxIter: Float, plane: Float, x: Int, y: Int)
+{
+ var cReal = (as_float(x) * plane / size) - 2.0;
+ var cImg = (as_float(y) * plane / size) - 2.0;
+ var zReal = 0.0;
+ var zImg = 0.0;
+ var count = 0.0;
+
+ # Calculate
+ while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
+ var temp = (zReal * zReal) - (zImg * zImg) + cReal;
+ zImg = 2.0 * zReal * zImg + cImg;
+ zReal = temp;
+ count = count + 1.0;
+
+ # Check
+ if as_int(count) == as_int(maxIter) {
+ termpos(x, y);
+ print("█");
+ }
+ }
+}
+
fn main() Int {
# Configure
var size = 1000.0;
@@ -6,35 +29,13 @@ fn main() Int {
var plane = 4.0;
# loop over coordinates
- var x = 0.0;
- while x < size {
- var y = 0.0;
- while y < size {
- # Initialize
- var cReal = (x * plane / size) - 2.0;
- var cImg = (y * plane / size) - 2.0;
- var zReal = 0.0;
- var zImg = 0.0;
- var count = 0.0;
-
- # Calculate
- while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
- var temp = (zReal * zReal) - (zImg * zImg) + cReal;
- zImg = 2.0 * zReal * zImg + cImg;
- zReal = temp;
- count = count + 1.0;
- }
- # Check
- if as_int(count) == as_int(maxIter) {
- termpos(as_int(x), as_int(y));
- print("█");
- }
-
- y = y + 1.0;
+ for x in 0..as_int(size) {
+ for y in 0..as_int(size) {
+ # Initialize
+ printMan(size, maxVal, maxIter, plane, x, y);
}
- x = x + 1.0;
+
}
return 0;
}
-