How to resolve the algorithm Chaos game step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Chaos game step by step in the zkl programming language

Table of Contents

Problem Statement

The Chaos Game is a method of generating the attractor of an iterated function system (IFS). One of the best-known and simplest examples creates a fractal, using a polygon and an initial point selected at random.

Play the Chaos Game using the corners of an equilateral triangle as the reference points.   Add a starting point at random (preferably inside the triangle).   Then add the next point halfway between the starting point and one of the reference points.   This reference point is chosen at random. After a sufficient number of iterations, the image of a Sierpinski Triangle should emerge.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Chaos game step by step in the zkl programming language

Source code in the zkl programming language

w,h:=640,640;
bitmap:=PPM(w,h,0xFF|FF|FF);  // White background
colors:=T(0xFF|00|00,0x00|FF|00,0x00|00|FF);   // red,green,blue
 
margin,size:=60, w - 2*margin;
points:=T(T(w/2, margin), T(margin,size), T(margin + size,size) );
N,done:=Atomic.Int(0),Atomic.Bool(False);
 
Thread.HeartBeat('wrap(hb){  // a thread
   var a=List(-1,-1);

   if(N.inc()<50){
      do(500){ 
	 colorIndex:=(0).random(3);  // (0..2)
	 b,p:=points[colorIndex], halfwayPoint(a,b);
	 x,y:=p;
	 bitmap[x,y]=colors[colorIndex];
	 a=p;
      }
      bitmap.writeJPGFile("chaosGame.jpg",True);
   }
   else{ hb.cancel(); done.set(); }  // stop thread and signal done
},2).go();     // run every 2 seconds, starting now
 
fcn halfwayPoint([(ax,ay)], [(bx,by)]){ T((ax + bx)/2, (ay + by)/2) }

done.wait();  // don't exit until thread is done
println("Done");

  

You may also check:How to resolve the algorithm 100 doors step by step in the Falcon programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Lasso programming language
You may also check:How to resolve the algorithm Abstract type step by step in the Red programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Phix programming language
You may also check:How to resolve the algorithm CUSIP step by step in the Python programming language