Author Topic: Chaos Game  (Read 5355 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Chaos Game
« on: February 19, 2019, 09:43:30 am »
Here are 8 beautiful fractals based on chaos game technique.

Code: QB64: [Select]
  1. 'All the fractal rendered are based on chaos game technique and are adapted from wikipedia.
  2. '19 Feb, 2019 Ashish
  3. 'https://en.wikipedia.com/wiki/Chaos_game
  4.  
  5.  
  6. _TITLE "Chaos Game"
  7. SCREEN _NEWIMAGE(700, 700, 32)
  8.  
  9.  
  10.  
  11.     i = i + 1
  12.     IF i > 8 THEN i = 1
  13.     drawFractal i, 300000 + i * 10000, 1 '1 for multiple colors, other value show fractal in only white color
  14.     _DELAY 0.5
  15.     CLS
  16.  
  17.  
  18.  
  19. SUB drawFractal (typ, num_of_iterations, colored AS _BYTE)
  20.     px = p5random(0, _WIDTH): py = p5random(0, _HEIGHT)
  21.     col~& = _RGBA(255, 255, 255, 80)
  22.     DIM clr~&(5)
  23.     clr~&(0) = _RGBA(255, 0, 0, 90)
  24.     clr~&(1) = _RGBA(0, 255, 0, 90)
  25.     clr~&(2) = _RGBA(0, 0, 255, 90)
  26.     clr~&(3) = _RGBA(255, 255, 0, 90)
  27.     clr~&(4) = _RGBA(0, 255, 255, 90)
  28.     clr~&(5) = _RGBA(255, 0, 255, 90)
  29.     SELECT CASE typ
  30.         CASE 1 'Seirpinshki Triangle
  31.             x1 = _WIDTH / 2: y1 = 0
  32.             x3 = 0: y3 = _HEIGHT - 1
  33.             x2 = _WIDTH - 1: y2 = y3
  34.  
  35.             IF colored = 1 THEN
  36.                 DIM vertColor(2)
  37.                 vertColor(0) = INT(p5random(0, 5))
  38.                 FOR i = 1 TO 2
  39.                     f1: kk = 0
  40.                     k = INT(p5random(0, 5))
  41.                     FOR j = 0 TO i - 1
  42.                         IF k = vertColor(j) THEN kk = 1
  43.                     NEXT
  44.                     IF kk = 1 THEN GOTO f1
  45.                     vertColor(i) = k
  46.                 NEXT
  47.             END IF
  48.             preVert = 1
  49.             DO
  50.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert - 1)) ELSE PSET (px, py), col~&
  51.                 i = i + 1
  52.                 p_e = RND
  53.                 IF p_e > 0 AND p_e < 0.33 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): preVert = 1
  54.                 IF p_e > 0.33 AND p_e < 0.66 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): preVert = 2
  55.                 IF p_e > 0.66 AND p_e < 0.99 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): preVert = 3
  56.  
  57.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  58.  
  59.             LOOP UNTIL i >= num_of_iterations
  60.         CASE 2 'I don't know the name of this fractal, but its nice. It is based on square vertices
  61.             x1 = 0: y1 = 0
  62.             x2 = _WIDTH - 1: y2 = 0
  63.             x3 = x2: y3 = _HEIGHT - 1
  64.             x4 = 0: y4 = y3
  65.  
  66.             IF colored = 1 THEN
  67.                 DIM vertColor(3)
  68.                 vertColor(0) = INT(p5random(0, 5))
  69.                 FOR i = 1 TO 3
  70.                     f2: kk = 0
  71.                     k = INT(p5random(0, 5))
  72.                     FOR j = 0 TO i - 1
  73.                         IF k = vertColor(j) THEN kk = 1
  74.                     NEXT
  75.                     IF kk = 1 THEN GOTO f2
  76.                     vertColor(i) = k
  77.                 NEXT
  78.             END IF
  79.             preVert = 1
  80.  
  81.             DO
  82.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert - 1)) ELSE PSET (px, py), col~&
  83.                 i = i + 1
  84.                 p_e = RND
  85.                 IF p_e > 0 AND p_e < 0.25 AND preVert <> 1 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): preVert = 1
  86.                 IF p_e > 0.25 AND p_e < 0.5 AND preVert <> 2 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): preVert = 2
  87.                 IF p_e > 0.5 AND p_e < 0.75 AND preVert <> 3 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): preVert = 3
  88.                 IF p_e > 0.75 AND p_e < 1 AND preVert <> 4 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): preVert = 4
  89.  
  90.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  91.  
  92.  
  93.             LOOP UNTIL i >= num_of_iterations
  94.         CASE 3
  95.             x1 = 0: y1 = 0
  96.             x2 = _WIDTH - 1: y2 = 0
  97.             x3 = x2: y3 = _HEIGHT - 1
  98.             x4 = 0: y4 = y3
  99.  
  100.             IF colored THEN
  101.                 DIM vertColor(3)
  102.                 vertColor(0) = INT(p5random(0, 5))
  103.                 FOR i = 1 TO 3
  104.                     f3: kk = 0
  105.                     k = INT(p5random(0, 5))
  106.                     FOR j = 0 TO i - 1
  107.                         IF k = vertColor(j) THEN kk = 1
  108.                     NEXT
  109.                     IF kk = 1 THEN GOTO f3
  110.                     vertColor(i) = k
  111.                 NEXT
  112.             END IF
  113.             preVert = 1
  114.  
  115.             DO
  116.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert - 1)) ELSE PSET (px, py), col~&
  117.                 i = i + 1
  118.                 p_e = RND
  119.                 IF p_e > 0 AND p_e < 0.25 AND preVert <> 4 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): preVert = 1
  120.                 IF p_e > 0.25 AND p_e < 0.5 AND preVert <> 1 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): preVert = 2
  121.                 IF p_e > 0.5 AND p_e < 0.75 AND preVert <> 2 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): preVert = 3
  122.                 IF p_e > 0.75 AND p_e < 1 AND preVert <> 3 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): preVert = 4
  123.  
  124.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  125.  
  126.  
  127.             LOOP UNTIL i >= num_of_iterations
  128.  
  129.         CASE 4
  130.             x1 = 0: y1 = 0
  131.             x2 = _WIDTH - 1: y2 = 0
  132.             x3 = x2: y3 = _HEIGHT - 1
  133.             x4 = 0: y4 = y3
  134.  
  135.             IF colored THEN
  136.                 DIM vertColor(3)
  137.                 vertColor(0) = INT(p5random(0, 5))
  138.                 FOR i = 1 TO 3
  139.                     f4: kk = 0
  140.                     k = INT(p5random(0, 5))
  141.                     FOR j = 0 TO i - 1
  142.                         IF k = vertColor(j) THEN kk = 1
  143.                     NEXT
  144.                     IF kk = 1 THEN GOTO f4
  145.                     vertColor(i) = k
  146.                 NEXT
  147.             END IF
  148.             preVert = 1
  149.  
  150.             DO
  151.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert - 1)) ELSE PSET (px, py), col~&
  152.                 i = i + 1
  153.                 p_e = RND
  154.                 IF p_e > 0 AND p_e < 0.25 AND preVert <> 3 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): preVert = 1
  155.                 IF p_e > 0.25 AND p_e < 0.5 AND preVert <> 4 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): preVert = 2
  156.                 IF p_e > 0.5 AND p_e < 0.75 AND preVert <> 1 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): preVert = 3
  157.                 IF p_e > 0.75 AND p_e < 1 AND preVert <> 2 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): preVert = 4
  158.  
  159.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  160.  
  161.  
  162.             LOOP UNTIL i >= num_of_iterations
  163.  
  164.         CASE 5
  165.             x1 = 0: y1 = 0
  166.             x2 = _WIDTH - 1: y2 = 0
  167.             x3 = x2: y3 = _HEIGHT - 1
  168.             x4 = 0: y4 = y3
  169.  
  170.             IF colored THEN
  171.                 DIM vertColor(3)
  172.                 vertColor(0) = INT(p5random(0, 5))
  173.                 FOR i = 1 TO 3
  174.                     f5: kk = 0
  175.                     k = INT(p5random(0, 5))
  176.                     FOR j = 0 TO i - 1
  177.                         IF k = vertColor(j) THEN kk = 1
  178.                     NEXT
  179.                     IF kk = 1 THEN GOTO f5
  180.                     vertColor(i) = k
  181.                 NEXT
  182.             END IF
  183.             preVert1 = 1
  184.  
  185.             DO
  186.                 _DEST 0
  187.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert1 - 1)) ELSE PSET (px, py), col~&
  188.                 i = i + 1
  189.                 p_e = RND
  190.  
  191.                 IF p_e > 0 AND p_e < 0.25 THEN
  192.                     IF preVert1 <> 4 AND prevert2 <> 2 OR preVert1 <> 2 AND prevert2 <> 4 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): prevert2 = preVert1: preVert1 = 1
  193.  
  194.                 END IF
  195.                 IF p_e > 0.25 AND p_e < 0.5 THEN
  196.                     IF preVert1 <> 1 AND prevert2 <> 3 OR preVert1 <> 3 AND prevert2 <> 1 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): prevert2 = preVert1: preVert1 = 2
  197.                 END IF
  198.                 IF p_e > 0.5 AND p_e < 0.75 THEN
  199.                     IF preVert1 <> 2 AND prevert2 <> 4 OR preVert1 <> 4 AND prevert2 <> 2 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): prevert2 = preVert1: preVert1 = 3
  200.                 END IF
  201.                 IF p_e > 0.75 AND p_e < 1 THEN
  202.                     IF preVert1 <> 1 AND prevert2 <> 3 OR preVert1 <> 3 AND prevert2 <> 1 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): prevert2 = preVert1: preVert1 = 4
  203.                 END IF
  204.  
  205.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  206.  
  207.  
  208.             LOOP UNTIL i >= num_of_iterations
  209.  
  210.         CASE 6
  211.  
  212.             x1 = 0: y1 = 0
  213.             x2 = _WIDTH - 1: y2 = 0
  214.             x3 = x2: y3 = _HEIGHT - 1
  215.             x4 = 0: y4 = y3
  216.  
  217.             IF colored THEN
  218.                 DIM vertColor(3)
  219.                 vertColor(0) = INT(p5random(0, 5))
  220.                 FOR i = 1 TO 3
  221.                     f6: kk = 0
  222.                     k = INT(p5random(0, 5))
  223.                     FOR j = 0 TO i - 1
  224.                         IF k = vertColor(j) THEN kk = 1
  225.                     NEXT
  226.                     IF kk = 1 THEN GOTO f6
  227.                     vertColor(i) = k
  228.                 NEXT
  229.             END IF
  230.             preVert1 = 1
  231.  
  232.             DO
  233.  
  234.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert1 - 1)) ELSE PSET (px, py), col~&
  235.                 i = i + 1
  236.                 p_e = RND
  237.  
  238.                 IF p_e > 0 AND p_e < 0.25 THEN
  239.                     IF preVert1 <> 2 AND prevert2 <> 2 OR preVert1 <> 4 AND prevert2 <> 4 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): prevert2 = preVert1: preVert1 = 1
  240.                 END IF
  241.                 IF p_e > 0.25 AND p_e < 0.5 THEN
  242.                     IF preVert1 <> 1 AND prevert2 <> 1 OR preVert1 <> 3 AND prevert2 <> 3 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): prevert2 = preVert1: preVert1 = 2
  243.                 END IF
  244.                 IF p_e > 0.5 AND p_e < 0.75 THEN
  245.                     IF preVert1 <> 2 AND prevert2 <> 2 OR preVert1 <> 4 AND prevert2 <> 4 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): prevert2 = preVert1: preVert1 = 3
  246.                 END IF
  247.                 IF p_e > 0.75 AND p_e < 1 THEN
  248.                     IF preVert1 <> 1 AND prevert2 <> 1 OR preVert1 <> 3 AND prevert2 <> 3 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): prevert2 = preVert1: preVert1 = 4
  249.                 END IF
  250.  
  251.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  252.  
  253.  
  254.             LOOP UNTIL i >= num_of_iterations
  255.  
  256.         CASE 7
  257.             rx = _WIDTH / 2
  258.             ry = _HEIGHT / 2
  259.             px = p5random(0, _WIDTH)
  260.             py = p5random(0, _HEIGHT)
  261.             FOR i = 5 TO 1 STEP -1
  262.  
  263.                 SELECT CASE i
  264.                     CASE 1
  265.                         x1 = rx + rx * SIN(_PI(0.4 * i)): y1 = ry + ry * COS(_PI(0.4 * i))
  266.                     CASE 2
  267.                         x2 = rx + rx * SIN(_PI(0.4 * i)): y2 = ry + ry * COS(_PI(0.4 * i))
  268.                     CASE 3
  269.                         x3 = rx + rx * SIN(_PI(0.4 * i)): y3 = ry + ry * COS(_PI(0.4 * i))
  270.                     CASE 4
  271.                         x4 = rx + rx * SIN(_PI(0.4 * i)): y4 = ry + ry * COS(_PI(0.4 * i))
  272.                     CASE 5
  273.                         x5 = rx + rx * SIN(_PI(0.4 * i)): y5 = ry + ry * COS(_PI(0.4 * i))
  274.                 END SELECT
  275.             NEXT
  276.  
  277.             IF colored THEN
  278.                 DIM vertColor(4)
  279.                 vertColor(0) = INT(p5random(0, 5))
  280.                 FOR i = 1 TO 4
  281.                     f7: kk = 0
  282.                     k = INT(p5random(0, 5))
  283.                     FOR j = 0 TO i - 1
  284.                         IF k = vertColor(j) THEN kk = 1
  285.                     NEXT
  286.                     IF kk = 1 THEN GOTO f7
  287.                     vertColor(i) = k
  288.                 NEXT
  289.             END IF
  290.  
  291.             preVert = 1
  292.             DO
  293.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert - 1)) ELSE PSET (px, py), col~&
  294.                 i = i + 1
  295.  
  296.                 p_e = RND
  297.                 IF p_e > 0 AND p_e < 0.2 AND preVert <> 1 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): preVert = 1
  298.                 IF p_e > 0.2 AND p_e < 0.4 AND preVert <> 2 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): preVert = 2
  299.                 IF p_e > 0.4 AND p_e < 0.6 AND preVert <> 3 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): preVert = 3
  300.                 IF p_e > 0.6 AND p_e < 0.8 AND preVert <> 4 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): preVert = 4
  301.                 IF p_e > 0.8 AND p_e < 1.0 AND preVert <> 5 THEN px = px + 0.5 * (x5 - px): py = py + 0.5 * (y5 - py): preVert = 5
  302.  
  303.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  304.  
  305.  
  306.             LOOP UNTIL i >= num_of_iterations
  307.         CASE 8
  308.             rx = _WIDTH / 2
  309.             ry = _HEIGHT / 2
  310.             px = p5random(0, _WIDTH)
  311.             py = p5random(0, _HEIGHT)
  312.             FOR i = 5 TO 1 STEP -1
  313.  
  314.                 SELECT CASE i
  315.                     CASE 1
  316.                         x1 = rx + rx * SIN(_PI(0.4 * i)): y1 = ry + ry * COS(_PI(0.4 * i))
  317.                     CASE 2
  318.                         x2 = rx + rx * SIN(_PI(0.4 * i)): y2 = ry + ry * COS(_PI(0.4 * i))
  319.                     CASE 3
  320.                         x3 = rx + rx * SIN(_PI(0.4 * i)): y3 = ry + ry * COS(_PI(0.4 * i))
  321.                     CASE 4
  322.                         x4 = rx + rx * SIN(_PI(0.4 * i)): y4 = ry + ry * COS(_PI(0.4 * i))
  323.                     CASE 5
  324.                         x5 = rx + rx * SIN(_PI(0.4 * i)): y5 = ry + ry * COS(_PI(0.4 * i))
  325.                 END SELECT
  326.             NEXT
  327.  
  328.             IF colored THEN
  329.                 DIM vertColor(4)
  330.                 vertColor(0) = INT(p5random(0, 5))
  331.                 FOR i = 1 TO 4
  332.                     f8: kk = 0
  333.                     k = INT(p5random(0, 5))
  334.                     FOR j = 0 TO i - 1
  335.                         IF k = vertColor(j) THEN kk = 1
  336.                     NEXT
  337.                     IF kk = 1 THEN GOTO f8
  338.                     vertColor(i) = k
  339.                 NEXT
  340.             END IF
  341.  
  342.             preVert1 = 1
  343.  
  344.             DO
  345.                 IF colored THEN PSET (px, py), clr~&(vertColor(preVert1 - 1)) ELSE PSET (px, py), col~&
  346.                 i = i + 1
  347.  
  348.                 p_e = RND
  349.                 IF p_e > 0 AND p_e < 0.2 THEN
  350.                     IF preVert1 <> 1 AND prevert2 <> 5 OR preVert1 <> 5 AND prevert2 <> 1 THEN px = px + 0.5 * (x1 - px): py = py + 0.5 * (y1 - py): prevert2 = preVert1: preVert1 = 1
  351.                 END IF
  352.                 IF p_e > 0.2 AND p_e < 0.4 THEN
  353.                     IF preVert1 <> 1 AND prevert2 <> 3 OR preVert1 <> 3 AND prevert2 <> 1 THEN px = px + 0.5 * (x2 - px): py = py + 0.5 * (y2 - py): prevert2 = preVert1: preVert1 = 2
  354.                 END IF
  355.                 IF p_e > 0.4 AND p_e < 0.6 THEN
  356.                     IF preVert1 <> 2 AND prevert2 <> 4 OR preVert1 <> 4 AND prevert2 <> 2 THEN px = px + 0.5 * (x3 - px): py = py + 0.5 * (y3 - py): prevert2 = preVert1: preVert1 = 3
  357.                 END IF
  358.                 IF p_e > 0.6 AND p_e < 0.8 THEN
  359.                     IF preVert1 <> 3 AND prevert2 <> 5 OR preVert1 <> 5 AND prevert2 <> 3 THEN px = px + 0.5 * (x4 - px): py = py + 0.5 * (y4 - py): prevert2 = preVert1: preVert1 = 4
  360.                 END IF
  361.                 IF p_e > 0.8 AND p_e < 1.0 THEN
  362.                     IF preVert1 <> 1 AND prevert2 <> 4 OR preVert1 <> 4 AND prevert2 <> 1 THEN px = px + 0.5 * (x5 - px): py = py + 0.5 * (y5 - py): prevert2 = preVert1: preVert1 = 5
  363.                 END IF
  364.  
  365.                 IF i MOD 1000 = 0 THEN _DISPLAY: _DELAY 0.01
  366.             LOOP UNTIL i >= num_of_iterations
  367.  
  368.         CASE ELSE: EXIT SUB
  369.     END SELECT
  370. 'function taken from p5js.bas
  371. 'https://bit.ly/p5jsbas
  372. FUNCTION p5random! (mn!, mx!)
  373.     IF mn! > mx! THEN
  374.         SWAP mn!, mx!
  375.     END IF
  376.     p5random! = RND * (mx! - mn!) + mn!
  377.  
  378.  


Screenshot_1.png

« Last Edit: February 19, 2019, 09:44:50 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaos Game
« Reply #1 on: February 19, 2019, 09:59:08 am »
Nice! That 2nd one after Sierpinski is interesting. I wonder if I could duplicate with recursive sub.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Chaos Game
« Reply #2 on: February 19, 2019, 04:41:00 pm »
Very nice, Ashish. Could I use it as a texture for 3D?

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Chaos Game
« Reply #3 on: February 21, 2019, 08:49:55 am »
Nice! That 2nd one after Sierpinski is interesting. I wonder if I could duplicate with recursive sub.
Thank You, bplus! Good luck at that!

Very nice, Ashish. Could I use it as a texture for 3D?
Of course, Petr! Feel free to use it. I will be honourable on that.

wow... nice very beautiful!
i wish i had the knowledge to program like that!
congratulation Ashish!
You don't need very high knowledge to code like that. If you understand basic algebra and probability then you can code it.

I recommend you to watch following videos -
(Numberphile's video on chaos game, it is best to understand what is it).
(Daniel Shiffman video on coding this.)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaos Game
« Reply #4 on: February 23, 2019, 12:42:51 am »
OK I did manage to find a place to sneak in a little recursion in the 2nd fractal:
Code: QB64: [Select]
  1. _TITLE "Ashish Fractal #2 Chaos Game recursive mod by B+" '2019-02-23
  2. SCREEN _NEWIMAGE(1200, 720, 32)
  3. _SCREENMOVE 100, 20
  4.  
  5. scatter 0, 0, 600, 720, _RGB32(0, 0, 200)
  6. scatter 601, 0, 600, 720, _RGB32(130, 250, 0)
  7. PRINT "Done"
  8.  
  9. SUB scatter (x0, y0, w, h, c AS _UNSIGNED LONG)
  10.     x1 = x0: y1 = y0
  11.     x2 = x1 + w: y2 = y1
  12.     x3 = x2: y3 = y1 + h
  13.     x4 = x1: y4 = y3
  14.     'LINE (x1, y1)-(x2, y3), c, B
  15.     IF w < 100 THEN limit = w * h ELSE limit = w * h * .25
  16.     i = 0: v = 1: x = x1 + w / 2: y = y1 + h / 2
  17.     WHILE i < limit
  18.         r = INT(RND * 4) + 1
  19.         SELECT CASE r
  20.             CASE 1: IF v <> 1 THEN x = x + .5 * (x1 - x): y = y + .5 * (y1 - y): v = 1
  21.             CASE 2: IF v <> 2 THEN x = x + .5 * (x2 - x): y = y + .5 * (y2 - y): v = 2
  22.             CASE 3: IF v <> 3 THEN x = x + .5 * (x3 - x): y = y + .5 * (y3 - y): v = 3
  23.             CASE 4: IF v <> 4 THEN x = x + .5 * (x4 - x): y = y + .5 * (y4 - y): v = 4
  24.         END SELECT
  25.         PSET (x, y), c
  26.         i = i + 1
  27.         IF i MOD 1000 = 0 THEN
  28.             LINE (0, 0)-(50, 20), _RGB32(0, 0, 0), BF
  29.             LOCATE 1, 1: PRINT INT(100 * i / limit); "%"
  30.         END IF
  31.         _DISPLAY
  32.     WEND
  33.     IF w > 12 AND h > 12 THEN
  34.         w4 = w / 4: h4 = h / 4
  35.         xn = x1 + w / 2 - w4 / 2
  36.         yn = y1 + h / 2 - h4 / 2
  37.         r = _RED32(c): g = _GREEN32(c): b = _BLUE(c)
  38.         IF r > 127 THEN r = r - 64 ELSE r = r + 64
  39.         IF g > 127 THEN g = g - 64 ELSE g = g + 64
  40.         IF b > 127 THEN b = b - 64 ELSE r = b + 64
  41.         scatter xn, yn, w4, h4, _RGB32(r, g, b)
  42.     END IF
  43.  

 
Ashish Fractal #2 Chaos Game recursive mod B+.PNG

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Chaos Game
« Reply #5 on: March 03, 2019, 12:06:54 pm »
Cool bplus! You did it! :D
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials