Advertisement
Guest User

WorldGenerator

a guest
Feb 3rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. /*МИРОГЕНЕРАТОР*/
  2. //density, size, probability(плотность, размер, вероятность)
  3. function drawWorld(density, size, probability)
  4. {  
  5.     nsize = size;
  6.     ctx.fillStyle = '#aaa'
  7.     for(x = 0; x < cvs.width; x+=density)
  8.     {
  9.         for(y = 0; y < cvs.height; y+=density)
  10.         {
  11.             if(randomizer(x, y, 100) > probability)
  12.             {
  13.                 osize = nsize;
  14.                 nsize = randomizer(x, y, size);
  15.                 s = (x + size - x) + (y + size - y);
  16.                 p = x + y;
  17.                 q = p / s;
  18.                 csize = interpolate(osize, nsize, q);
  19.                 ctx.fillRect(x, y, csize, csize);
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. function randomizer(x, y, max)
  26. {
  27.     WorldCounter++;
  28.     if(WorldCounter == WorldConstant) WorldCounter = 0;
  29.     return (x ^ y * WorldCounter) % max;
  30. }
  31.  
  32. //Интерполяция косинусоидальная
  33. function interpolate(a, b, x)
  34. {
  35.     return a * (1 - ((1 - Math.cos(x * Math.PI)) / 2)) + b * ((1 - Math.cos(x * 3.1415927)) / 2);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement