Conveyor Belt in Cellular Automata

c

var canvas = document.getElementById("canvas");
var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 
var img; var current, prev, imageData;

function RunOnce()  
{ 
   for (var y = 1; y < img.height - 1; ++y) 
   { 
      for (var x = 1; x < img.width - 1; ++x)
      { 
          var n = (y * img.width + x) * 4; // pixel address 
          var c = prev[n]; // color var neighbours = 0; 
          for (var a = -1; a < 2; ++a) 
              for (var b = -1; b < 2; ++b) 
                 if (!(a == 0 && b == 0) && prev[((y + b) * img.width + (x + a)) * 4] == 240) 
                     neighbours++; 

          if (c == 240 && neighbours >= 3 && neighbours <= 5) 
              continue; 

          if (c >= 80) 
              current[n] = c - 80; 
          else 
              current[n] = neighbours == 2 ? 240 : 0; 
       } 
    } 

    ctx.putImageData(imageData, 0, 0); 
    prev.set(current);  
    setTimeout(function() { RunOnce(); }, 10); 
}

function loadImageFromFile(event)
{
    document.getElementById("label").style.visibility = "hidden";
    var reader = new FileReader();
    img = document.getElementById("world");
    reader.onload = function(event) 
    {
       img.onload = function() 
       { 
           ctx.drawImage(img, 0, 0); 
           imageData = ctx.getImageData(0, 0, img.width, img.height);
           current = imageData.data;
           prev = new Uint8ClampedArray(imageData.data);
           RunOnce();
       };
       img.src = event.target.result;
    } 
    reader.readAsDataURL(event.target.files[0]);
}
conveyor

Source image for conveyor belt example. Should be in 24bit bmp format

glider_gun

Source image for glider gun

burning_arrow

Source image for burning arow

 

Advertisements

Strange Attractors

attractor

var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 
var width = 800; 
var height = 800; 
var pixels = Array(width).fill(0).map(x => Array(height).fill(0.0)); 

//var a = -1.4, b = 1.6, c = 1.0, d = 0.7; 
//var a = 1.7, b = 1.7, c = 0.6, d = 1.2; 
//var a = -1.8, b = -2.0, c = -0.5, d = -0.9; 
var a = -1.4, b = 1.6, c = -1.0, d = 0.9; 
//var a = 1.5, b = -1.8, c = 1.6, d = 0.9; 

var x = 0, y = 0; 
var max = 0.0; 

// generate 
for (var i = 0; i < 40000000; ++i) 
{ 
   var x1 = Math.sin(a * y) + c * Math.cos(a * x); 
   var y1 = Math.sin(b * x) + d * Math.cos(b * y); 

   var m = Math.round((x1 + 2.5) * width / 5); 
   var n = Math.round((2.5 - y1) * height / 5); 
   var h = ++pixels[m][n]; 
   max = Math.max(max, h); 

   x = x1; 
   y = y1; 
} 

// draw 
for (var m = 0; m < width; ++m) 
{ 
   for (var n = 0; n < height; ++n) 
   { 
       var color = 2 * Math.round(Math.sqrt(pixels[m][n] / max) * 100.0); 
       color = Math.max(0, Math.min(color, 100)); 
       ctx.fillStyle = "hsl("+ 30 + "," + 100 + "%," + (100 - color) +"%)"; 
       ctx.fillRect(m, n, 1, 1); 
   } 
}

A Bunch of Dragon Flowers

Capture

var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 

for (var x = 0; x < 1000; ++x) 
{ 
   for (var y = 0; y < 600; ++y) 
   { 
       var c = -0.835, ci = -0.2321; 
       var z = x / 300.0 - 1.7; 
       var zi = y / 300.0 - 1; 

       for (var i = 255; i > 0; --i) 
       { 
           var a = z * z - zi * zi + c; 
           var b = 2 * z * zi + ci; 
           if (a * a + b * b > 50) 
           { 
               ctx.fillStyle = "rgb("+ i + "," + i + "," + 255 +")"; 
               ctx.fillRect(x, y, 1, 1); 
               break; 
           } 
           z = a; 
           zi = b; 
       } 
    } 
}

 

බීමට වැහි වතුර

ඉස්සර අපි වැව් වතුර බිව්වේ. ඒත් දැන් වතුරේ ආසනික්. ඒ නිසා පුද්ගලික වැව් හදාගන්න වෙලා. සාමූහිකත්වයෙන් පුද්ගලිකත්වයට මාරුවෙලා.

123

4

අඟුරු-වැලි පෙරනය

5

Automatic Water Pump Code

#include <12F675.h>
//#device ADC=10
#use delay(internal=4000000)

#define LED                PIN_A2
#define WATER_PUMP         PIN_A0
#define WATER_LOW_SENSOR   PIN_A4
#define WATER_HIGH_SENSOR  PIN_A5

void delay_s(int8 t)
{
   while (t > 0)
   {
      delay_ms(1000);
      t--;
   }
}

void blink(int16 t, int16 n)
{
   while (n > 0)
   {
      output_high(LED);
      delay_ms(t);
      output_low(LED);
      delay_ms(t);
      n--;
   }
}

void pumpWater()
{
   int n = 240; // 4 minutes
   output_high(LED);
   output_high(WATER_PUMP);
   
   while (n != 0)
   {
      delay_ms(1000);
      
      if (input(WATER_HIGH_SENSOR) == 1)
      {
         output_low(WATER_PUMP);
         output_low(LED);
         return;
      }
   }
   
   output_low(WATER_PUMP);
   output_low(LED);
}

int1 debounce(int8 pin)
{
   int8 i = 100;
   int n = 0;
  
   while (--i)
   {
      delay_ms(30);
      if (input(pin))
         n++;
   }
   
   return n > 80;
}

void main()
{
   blink(1000, 2);
   
   while(TRUE)
   {
      delay_ms(10000);
      blink(100, 1);
      
      if (input(WATER_LOW_SENSOR) == 1)
      {
         if (debounce(WATER_LOW_SENSOR) == 1)
         {
            pumpWater();
            
            if (input(WATER_LOW_SENSOR) == 1)
            {
               // Water level is still low. Pump looks not primed or 
               // possible pump failure. Manual intervention is required
               while (TRUE)
                  blink(500, 1);
            }
         }
      }
   }
}

Automatic Watering Code

#include <12F675.h>
//#device ADC=10
#use delay(internal=4000000)

#define LED PIN_A1

#define WATERING_DURATION 120

signed int32 timeToWaterSec = 300;

void delay_s(int8 t)
{
   while (t > 0)
   {
      delay_ms(1000);
      t--;
   }
}

void blink(int16 t, int16 n)
{
   while (n > 0)
   {
      output_high(LED);
      delay_ms(t);
      output_low(LED);
      delay_ms(t);
      n--;
   }
   
   timeToWaterSec -= t * n / 60000 * 2;
}

void showTime()
{
   if (timeToWaterSec < 3600)
      blink(100, timeToWaterSec / 60);
   else if (timeToWaterSec < 36000)
      blink(500, timeToWaterSec / 60);
   else
      blink(3000, 1);
}

void water()
{
   output_high(LED);
   output_high(PIN_A0);
   delay_s(WATERING_DURATION);
   output_low(PIN_A0);
   output_low(LED);
   delay_ms(500);

   output_high(LED);
   output_high(PIN_A2);
   delay_s(WATERING_DURATION);
   output_low(PIN_A2);
   output_low(LED);
   delay_ms(500);

   output_high(LED);
   output_high(PIN_A4);
   delay_s(WATERING_DURATION);
   output_low(PIN_A4);
   output_low(LED);
   delay_ms(500);

   output_high(LED);
   output_high(PIN_A5);
   delay_s(WATERING_DURATION);
   output_low(PIN_A5);
   output_low(LED);
   delay_ms(500);
}

void main()
{
   blink(1000, 3);
   
   while(TRUE)
   {
      delay_ms(3000);
      timeToWaterSec -= 3;
      
      if (timeToWaterSec < 0)
      {
         water();
         timeToWaterSec = 86400;
      }
      
      showTime();
   }

}

PICkit2-DEVICE POSITION