QB64.org Forum

Active Forums => Programs => Topic started by: Ashish on March 15, 2018, 12:33:02 pm

Title: Fractals! : Part 2
Post by: Ashish on March 15, 2018, 12:33:02 pm
Hi everyone! :)
I've again coded  some fractals... This time I've shown some L-System fractal. You can also see my previous
 fractal demos here - www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=14546

1. L-Systems Fractals (Contain 12 L-system fractals)  :)
Code: QB64: [Select]
  1. 'Coded By Ashish in QB64
  2. 'Twiter : @KingOfCoders
  3. _TITLE "Fractals [Part 2 : L-System]"
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6.  
  7. TYPE rule_type
  8.     token AS STRING * 1
  9.     replace AS STRING * 256
  10.  
  11.  
  12. DIM SHARED fractalsName(11) AS STRING
  13. DIM SHARED fractalRules(14) AS rule_type
  14.  
  15.  
  16. fractalsName(0) = "Board": fractalRules(0).token = "F": fractalRules(0).replace = "FF+F+F+F+FF"
  17. fractalsName(1) = "Crystal": fractalRules(1).token = "F": fractalRules(1).replace = "FF+F++F+F"
  18. fractalsName(2) = "Peano Curve": fractalRules(2).token = "X": fractalRules(2).replace = "XFYFX+F+YFXFY-F-XFYFX": fractalRules(3).token = "Y": fractalRules(3).replace = "YFXFY-F-XFYFX+F+YFXFY"
  19. fractalsName(3) = "Quadratic Gosper": fractalRules(4).token = "X": fractalRules(4).replace = "XFX-YF-YF+FX+FX-YF-YFFX+YF+FXFXYF-FX+YF+FXFX+YF-FXYF-YF-FX+FX+YFYF-": fractalRules(5).token = "Y": fractalRules(5).replace = "+FXFX-YF-YF+FX+FXYF+FX-YFYF-FX-YF+FXYFYF-FX-YFFX+FX+YF-YF-FX+FX+YFY"
  20. fractalsName(4) = "Quadratic Snowflake": fractalRules(6).token = "F": fractalRules(6).replace = "F-F+F+F-F"
  21. fractalsName(5) = "Quadratic Koch Island": fractalRules(7).token = "F": fractalRules(7).replace = "F-FF+FF+F+F-F-FF+F+F-F-FF-FF+F"
  22. fractalsName(6) = "Square Sierpinski": fractalRules(8).token = "X": fractalRules(8).replace = "XF-F+F-XF+F+XF-F+F-X"
  23. fractalsName(7) = "Triangle": fractalRules(9).token = "F": fractalRules(9).replace = "F-F+F"
  24. fractalsName(8) = "Von Koch Snowflake": fractalRules(10).token = "F": fractalRules(10).replace = "F-F++F-F"
  25. fractalsName(9) = "Hilbert": fractalRules(11).token = "X": fractalRules(11).replace = "-YF+XFX+FY-": fractalRules(12).token = "Y": fractalRules(12).replace = "+XF-YFY-FX+"
  26. fractalsName(10) = "Cross": fractalRules(13).token = "F": fractalRules(13).replace = "F+F-F+F+F"
  27. fractalsName(11) = "Pentaplexity": fractalRules(14).token = "F": fractalRules(14).replace = "F++F++F|F-F++F"
  28.  
  29.  
  30.     choice = getChoice
  31.     IF choice >= 0 AND choice <= UBOUND(fractalsName) THEN
  32.         _TITLE _TITLE$ + " " + fractalsName(choice)
  33.         SELECT CASE choice
  34.             CASE 0
  35.                 g$ = prepareGrammer("F+F+F+F", 0, 0, 5)
  36.                 angle! = 90
  37.             CASE 1
  38.                 g$ = prepareGrammer("F+F+F+F", 1, 1, 5)
  39.                 angle! = 90
  40.             CASE 2
  41.                 g$ = prepareGrammer("X", 2, 3, 4)
  42.                 angle! = 90
  43.             CASE 3
  44.                 g$ = prepareGrammer("-YF", 4, 5, 3)
  45.                 angle! = 90
  46.             CASE 4
  47.                 g$ = prepareGrammer("F", 6, 6, 5)
  48.                 angle! = 90
  49.             CASE 5
  50.                 g$ = prepareGrammer("F+F+F+F", 7, 7, 3)
  51.                 angle! = 90
  52.             CASE 6
  53.                 g$ = prepareGrammer("F+XF+F+XF", 8, 8, 5)
  54.                 angle! = 90
  55.             CASE 7
  56.                 g$ = prepareGrammer("F", 9, 9, 7)
  57.                 angle! = 120
  58.             CASE 8
  59.                 g$ = prepareGrammer("F++F++F", 10, 10, 5)
  60.                 angle! = 60
  61.             CASE 9
  62.                 g$ = prepareGrammer("X", 11, 12, 7)
  63.                 angle! = 90
  64.             CASE 10
  65.                 g$ = prepareGrammer("F+F+F+F", 13, 13, 6)
  66.                 angle! = 90
  67.             CASE 11
  68.                 g$ = prepareGrammer("F++F++F++F++F", 14, 14, 5)
  69.                 angle! = 36
  70.  
  71.         END SELECT
  72.         showControl
  73.         x = 100
  74.         y = 100
  75.         r = 3
  76.         DO
  77.             CLS
  78.  
  79.             IF _KEYDOWN(ASC("w")) THEN y = y - 2
  80.             IF _KEYDOWN(ASC("s")) THEN y = y + 2
  81.             IF _KEYDOWN(ASC("a")) THEN x = x - 2
  82.             IF _KEYDOWN(ASC("d")) THEN x = x + 2
  83.             IF _KEYDOWN(ASC("q")) THEN r = r + .1
  84.             IF _KEYDOWN(ASC("e")) AND r > 1 THEN r = r - .1
  85.             IF _KEYDOWN(27) THEN _AUTODISPLAY: EXIT DO
  86.             drawFractal g$, angle!, r, x, y
  87.  
  88.             _LIMIT 40
  89.             _DISPLAY
  90.  
  91.         LOOP
  92.     ELSE
  93.         PRINT "Wrong Choice"
  94.         _DELAY 1
  95.     END IF
  96.     _KEYCLEAR
  97.  
  98. FUNCTION prepareGrammer$ (axm$, s, e, N)
  99.     DIM fractalRule(e - s) AS rule_type
  100.     FOR i = s TO e
  101.         fractalRule(i - s).token = fractalRules(i).token
  102.         fractalRule(i - s).replace = RTRIM$(fractalRules(i).replace)
  103.     NEXT
  104.     prepareGrammer$ = generateGrammer$(axm$, fractalRule(), N)
  105.  
  106. FUNCTION getChoice ()
  107.     _TITLE "Fractals [Part 2 : L-System]"
  108.     CLS
  109.     FOR i = 0 TO UBOUND(fractalsName)
  110.         PRINT i + 1; ". "; fractalsName(i)
  111.     NEXT
  112.     INPUT "Enter the fractal No. : ", getChoice
  113.     getChoice = getChoice - 1
  114.  
  115. '######################################################
  116. 'Fractal related subroutine
  117. SUB showControl ()
  118.     CLS
  119.     PRINT "Use 'w' and 's' to move up and down"
  120.     PRINT "Use 'a' and 'd' to move left and rigth"
  121.     PRINT "Use 'q' and 'e' to zoom-in and zoom-out"
  122.     PRINT "Use 'Esc' to goto fractal menu"
  123.     PRINT "Hit a Key"
  124.     SLEEP
  125.  
  126. SUB drawFractal (grammer$, ang!, r, x, y)
  127.     ang2! = 0
  128.     px = x
  129.     py = y
  130.     FOR i = 1 TO LEN(grammer$)
  131.         ca$ = MID$(grammer$, i, 1)
  132.         SELECT CASE ca$
  133.             CASE "F"
  134.                 xx = COS(_D2R(ang2!)) * r + px
  135.                 yy = SIN(_D2R(ang2!)) * r + py
  136.                 LINE (px, py)-(xx, yy)
  137.                 px = xx
  138.                 py = yy
  139.             CASE "+"
  140.                 ang2! = ang2! + ang!
  141.             CASE "-"
  142.                 ang2! = ang2! - ang!
  143.             CASE "|"
  144.                 ang2! = ang2! + 180
  145.             CASE "f"
  146.                 xx = COS(_D2R(ang2!)) * r + px
  147.                 yy = SIN(_D2R(ang2!)) * r + py
  148.                 px = xx
  149.                 py = yy
  150.         END SELECT
  151.     NEXT
  152.  
  153. 'L-System Function
  154. FUNCTION generateGrammer$ (axiom$, rule() AS rule_type, n~&)
  155.     axiom2$ = axiom$
  156.     axiom3$ = ""
  157.     DO
  158.         FOR i = 1 TO LEN(axiom2$)
  159.             ca$ = MID$(axiom2$, i, 1)
  160.             nca$ = ca$
  161.             FOR j = 0 TO UBOUND(rule)
  162.                 IF rule(j).token = ca$ THEN
  163.                     nca$ = RTRIM$(rule(j).replace)
  164.                     EXIT FOR
  165.                 END IF
  166.             NEXT
  167.             axiom3$ = axiom3$ + nca$
  168.         NEXT
  169.         axiom2$ = axiom3$
  170.         axiom3$ = ""
  171.         count = count + 1
  172.         PRINT ".";
  173.     LOOP UNTIL count >= n~&
  174.     generateGrammer$ = axiom2$
  175.  
  176.  

2. Christmas Tree  ;)
Code: QB64: [Select]
  1. 'Coded By Ashish
  2. _TITLE "Christmas Tree"
  3. SCREEN _NEWIMAGE(800, 600, 32)
  4. DIM SHARED sx, sy, ox, oy, z
  5. ox = _WIDTH * .2
  6. oy = _HEIGHT * .9
  7. z = 500
  8. COLOR _RGBA(255, 255, 255, 40)
  9. drawChristmasTree 0, 0, 0
  10. FOR i = 0 TO 50
  11.     a = sx: b = sy
  12.     drawChristmasTree a, b, 0
  13.  
  14. SUB drawChristmasTree (x!, y!, N AS _UNSIGNED LONG)
  15.         CASE 0 TO 1 / 3
  16.             xx! = -.5 * y! + .5
  17.             yy! = .5 * x
  18.         CASE 1 / 3 TO 2 / 3
  19.             xx! = .5 * y! + .5
  20.             yy! = -.5 * x + .5
  21.         CASE ELSE
  22.             xx! = .5 * x! + .25
  23.             yy! = .5 * y + .5
  24.     END SELECT
  25.     PSET (xx! * z + ox, -yy! * z + oy)
  26.     IF N < 10000 THEN
  27.         drawChristmasTree xx!, yy!, N + 1
  28.     ELSE sx = xx!: sy = yy!
  29.     END IF
  30.  
  31.  

3. Dragon  :D
Code: QB64: [Select]
  1. 'Coded By Ashish
  2. _TITLE "Dragon"
  3. SCREEN _NEWIMAGE(800, 700, 32)
  4. DIM SHARED sx, sy, ox, oy, z
  5.  
  6. z = 60
  7. ox = _WIDTH * .46
  8. oy = _HEIGHT * .09
  9. COLOR _RGBA(255, 180, 0, 30)
  10. drawDragon 0, 0, 0
  11. FOR i = 0 TO 100
  12.     a = sx: b = sy
  13.     drawDragon a, b, 0
  14. SUB drawDragon (x!, y!, N AS _UNSIGNED LONG)
  15.         CASE 0 TO .8
  16.             xx! = .824074 * x! + .281428 * y! - 1.88229
  17.             yy! = -.212346 * x! + .864198 * y! - .110607
  18.         CASE ELSE
  19.             xx! = .088272 * x! + .520988 * y! + .78536
  20.             yy! = -.463889 * x! - .377778 * y! + 8.095795
  21.     END SELECT
  22.     PSET (xx! * z + ox, yy! * z + oy)
  23.     IF N < 10000 THEN
  24.         drawDragon xx!, yy!, N + 1
  25.     ELSE sx = xx!: sy = yy!
  26.     END IF
  27.  
  28.  

4. Spiral  :)
Code: QB64: [Select]
  1. 'Coded By Ashish in QB64
  2. _TITLE "IFS Spiral"
  3.  
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5.  
  6. DIM SHARED gx, gy
  7. COLOR _RGBA(255, 255, 255, 10)
  8. drawSpiral _WIDTH * .75, _HEIGHT * .25, 1
  9.  
  10. FOR i = 0 TO 100
  11.     vx = gx
  12.     vy = gy
  13.     drawSpiral vx, vy, 1
  14.  
  15.  
  16. SUB drawSpiral (x!, y!, N~&)
  17.     SELECT CASE RND(1)
  18.         CASE 0 TO 0.05
  19.             xx! = -0.121212 * x! + 0.257576 * y! - 6.721654
  20.             yy! = 0.151515 * x! + 0.053030 * y! + 1.377236
  21.         CASE 0.05 TO 0.10
  22.             xx! = 0.181818 * x! - 0.136364 * y! + 6.086107
  23.             yy! = 0.090909 * x! + 0.181818 * y! + 1.568035
  24.         CASE ELSE
  25.             xx! = 0.787879 * x! - 0.424242 * y! + 1.758647
  26.             yy! = 0.242424 * x! + 0.859848 * y! + 1.408065
  27.     END SELECT
  28.     PSET (xx! * 60 + _WIDTH * .5, yy! * 60)
  29.     IF N~& < 10000 THEN
  30.         drawSpiral xx!, yy!, N~& + 1
  31.     ELSE
  32.         gx = xx!
  33.         gy = yy!
  34.     END IF
  35.  
  36.  

5. Old Tree  ;)
Code: QB64: [Select]
  1. 'Coded By Ashish
  2. _TITLE "Old Tree"
  3. SCREEN _NEWIMAGE(800, 600, 32)
  4. DIM SHARED sx, sy, ox, oy, z
  5. ox = _WIDTH * .11 'x-offset
  6. oy = _HEIGHT * 1.1 'y-offset
  7. z = 600 'zoom-value
  8. COLOR _RGBA(255, 255, 255, 40)
  9. drawOldTree 0, 0, 0
  10.  
  11. FOR i = 0 TO 100
  12.     a = sx: b = sy
  13.     drawOldTree a, b, 0
  14. SUB drawOldTree (x!, y!, N AS _UNSIGNED LONG)
  15.         CASE 0 TO .2
  16.             xx! = .195 * x! - .488 * y! + .4431
  17.             yy! = .344 * x! + .443 * y! + .2452
  18.         CASE .2 TO .4
  19.             xx! = .462 * x! + .414 * y! + .2511
  20.             yy! = -.252 * x! + .361 * y! + .5692
  21.         CASE .4 TO .6
  22.             xx! = -.637 * x! + .8562
  23.             yy! = .501 * y! + .2512
  24.         CASE .6 TO .8
  25.             xx! = -.035 * x! + .07 * y! + .4884
  26.             yy! = -.469 * x! + .022 * y! + .5069
  27.         CASE ELSE
  28.             xx! = -.058 * x! - .07 * y! + .5976
  29.             yy! = .453 * x! - .111 * y! + .0969
  30.     END SELECT
  31.     'IF N MOD 1000 = 0 THEN _DELAY .01
  32.     PSET (xx! * z + ox, oy - yy! * (z + z * .25))
  33.     IF N < 10000 THEN
  34.         drawOldTree xx!, yy!, N + 1
  35.     ELSE sx = xx!: sy = yy!
  36.     END IF
  37.  
  38.  

6. Leaf  :D
Code: QB64: [Select]
  1. 'Coded By Ashish in QB64
  2. _TITLE "Leaf"
  3. SCREEN _NEWIMAGE(800, 600, 32)
  4. DIM SHARED sx, sy, ox, oy, z
  5. ox = _WIDTH * .34
  6. oy = _HEIGHT
  7. z = 700
  8. COLOR _RGBA(0, 255, 0, 10)
  9. drawLeaf 0, 0, 0
  10.     p = sx: q = sy
  11.     drawLeaf p, q, 0
  12.     _LIMIT 30
  13.  
  14. SUB drawLeaf (x!, y!, N AS _UNSIGNED LONG)
  15.     DIM a(3) AS SINGLE, b(3) AS SINGLE, c(3) AS SINGLE, d(3) AS SINGLE, e(3) AS SINGLE, f(3) AS SINGLE
  16.     a(0) = 0: a(1) = .7248: a(2) = .1583: a = (3) = .3386
  17.     b(0) = .2439: b(1) = .0337: b(2) = -.1297: b(3) = .3694
  18.     c(0) = 0: c(1) = -.0253: c(2) = .355: c(3) = .2227
  19.     d(0) = .3053: d(1) = .7426: d(2) = .3676: d(3) = -.0756
  20.     e(0) = 0: e(1) = .206: e(2) = .1383: e(3) = .0679
  21.     f(0) = 0: f(1) = .2538: f(2) = .175: f(3) = .0826
  22.     k = INT(RND * 4)
  23.     xx! = a(k) * x! + b(k) * y! + e(k)
  24.     yy! = c(k) * x! + d(k) * y! + f(k)
  25.     PSET (xx! * z + ox, -yy! * z + oy)
  26.  
  27.     IF N < 10000 THEN
  28.         drawLeaf xx!, yy!, N + 1
  29.     ELSE sx = xx!: sy = yy!
  30.     END IF
  31.  
  32.  

7. Young Tree  :)
Code: QB64: [Select]
  1. 'Coded By Ashish in QB64
  2. DEFDBL A-Z
  3. _TITLE "Young Tree"
  4. SCREEN _NEWIMAGE(800, 700, 32)
  5. DIM SHARED sx, sy, ox, oy, z
  6. ox = _WIDTH / 2
  7. oy = _HEIGHT
  8. z = 300
  9. COLOR _RGBA(255, 255, 255, 40)
  10. drawYoungTree 0, 0, 0
  11. FOR i = 0 TO 200
  12.     a = sx: b = sy
  13.     drawYoungTree a, b, 0
  14. SUB drawYoungTree (x, y, N AS _UNSIGNED LONG)
  15.     DIM r(5), s(5), theta(5), phi(5), f(5)
  16.     r(0) = .05: r(1) = .05: r(2) = .6: r(3) = .5: r(4) = .5: r(5) = .55
  17.     s(0) = .6: s(1) = -.5: s(2) = .5: s(3) = .45: s(4) = .55: s(5) = .4
  18.     theta(0) = 0: theta(1) = 0: theta(2) = .698: theta(3) = .349: theta(4) = -.524: theta(5) = -.698
  19.     phi(0) = 0: phi(1) = 0: phi(2) = .698: phi(3) = .3492: phi(4) = -.524: phi(5) = -.698
  20.     f(0) = 0: f(1) = 1: f(2) = .6: f(3) = 1.1: f(4) = 1: f(5) = .7
  21.     k = INT(RND * 6)
  22.     xx = r(k) * COS(theta(k)) * x - s(k) * SIN(phi(k)) * y
  23.     yy = r(k) * SIN(theta(k)) * x + s(k) * COS(phi(k)) * y + f(k)
  24.     PSET (xx * (z + z * .25) + ox, -yy * z + oy)
  25.     IF N < 6000 THEN
  26.         drawYoungTree xx, yy, N + 1
  27.     ELSE sx = xx: sy = yy
  28.     END IF
  29.  
  30.  
Title: Re: Fractals! : Part 2
Post by: bplus on March 15, 2018, 10:32:46 pm
Very nice set of fractals!
Title: Re: Fractals! : Part 2
Post by: Ashish on March 16, 2018, 03:21:06 am
Very nice set of fractals!
Thanks bplus! :)
Title: Re: Fractals! : Part 2
Post by: _vince on March 16, 2018, 05:12:03 am
Nice use of recursion, very suitable to the nature of fractals.  Here's a program I found of a similar style

Code: QB64: [Select]
  1. pi = 4*atn(1)
  2. c = 2*sqr(3)/9
  3. cc = sqr(12) / 6
  4.  
  5. sw = 1024
  6. sh = 768
  7.  
  8. screen _newimage(sw, sh, 12)
  9. tri sw/2, sh/2, 600, 0
  10. tri sw/2, sh/2, 600, pi
  11. koch sw/2, sh/2, 600, 3
  12.  
  13. sub koch(x, y, s, i)
  14.         if i = 0 then exit sub
  15.         for a = pi/6 to 2*pi + pi/6 step pi/3
  16.                 xx = s*c*cos(a) + x
  17.                 yy = s*c*sin(a) + y
  18.        
  19.                 tri xx, yy, s/3, a+pi/6
  20.                 tri xx, yy, s/3, a+pi/6+pi
  21.  
  22.                 koch xx, yy, s/3, i - 1
  23.         next
  24.  
  25. sub tri(x, y, s, a)
  26.         line(x,y)-(x+s*cc*cos(pi/6 + a),y+s*cc*sin(pi/6 + a)),8
  27.         line(x,y)-(x+s*cc*cos(5*pi/6 + a),y+s*cc*sin(5*pi/6 + a)),8
  28.         line(x,y)-(x-s*cc*cos(pi/2 + a),y-s*cc*sin(pi/2 + a)),8
  29.  
  30.         line(x+s*cc*cos(pi/6 + a),y+s*cc*sin(pi/6 + a))-(x+s*cc*cos(5*pi/6 + a),y+s*cc*sin(5*pi/6 + a))
  31.         line-(x-s*cc*cos(pi/2 + a),y-s*cc*sin(pi/2 + a))
  32.         line-(x+s*cc*cos(pi/6 + a),y+s*cc*sin(pi/6 + a))
  33.  
Title: Re: Fractals! : Part 2
Post by: Ashish on March 16, 2018, 08:18:47 am
@v
That was cool!
Title: Re: Fractals! : Part 2
Post by: bplus on March 16, 2018, 09:06:52 am
Yes, like the new Matrix Effect added to library, you get lot's of bang for your byte.

With recursive methods, it is amazing how much can be done with so few code lines.
Title: Re: Fractals! : Part 2
Post by: Ashish on March 16, 2018, 12:00:12 pm
Some more......

8. Bushes [Type 1]

Code: QB64: [Select]
  1. DEFDBL A-Z
  2. _TITLE "Bushes"
  3.  
  4. SCREEN _NEWIMAGE(800, 700, 32)
  5.  
  6. TYPE rule_type
  7.     token AS STRING * 1
  8.     replace AS STRING * 256
  9. TYPE __state
  10.     x AS DOUBLE
  11.     y AS DOUBLE
  12.     ang AS DOUBLE
  13.  
  14.  
  15. DIM  fractalData$
  16. DIM fractalRule(1) AS rule_type
  17. fractalRule(0).token = "F"
  18. fractalRule(0).replace = "FF"
  19. fractalRule(1).token = "X"
  20. fractalRule(1).replace = "F-[[X]+X]+F[+FX]-X"
  21. PRINT "Generating"
  22. fractalData$ = generateGrammer("X", fractalRule(), 8)
  23.  
  24. COLOR _RGBA(0, 255, 0, 45)
  25. drawFractal fractalData$, 22.5, 1, _WIDTH / 2, _HEIGHT, .5, 0, .5
  26.  
  27. SUB drawFractal (grammer$, ang, r, x, y, w, r_f, w_f)
  28.  
  29.     REDIM stat(1) AS __state
  30.     curr_state = 1
  31.     ang2 = 0
  32.     px = x
  33.     py = y
  34.     FOR i = 1 TO LEN(grammer$)
  35.         ca$ = MID$(grammer$, i, 1)
  36.         SELECT CASE ca$
  37.             CASE "F"
  38.                 xx = COS(_D2R(ang2 - 90)) * r + px
  39.                 yy = SIN(_D2R(ang2 - 90)) * r + py
  40.  
  41.                 LINE (px, py)-(xx, yy)
  42.                 'thickLine px, py, xx, yy, w
  43.                 px = xx
  44.                 py = yy
  45.             CASE "f"
  46.                 xx = COS(_D2R(ang2)) * r + px
  47.                 yy = SIN(_D2R(ang2)) * r + py
  48.                 px = xx
  49.                 py = yy
  50.             CASE "+"
  51.                 ang2 = ang2 + ang
  52.             CASE "-"
  53.                 ang2 = ang2 - ang
  54.             CASE "["
  55.                 stat(curr_state).x = px
  56.                 stat(curr_state).y = py
  57.                 stat(curr_state).ang = ang2
  58.                 curr_state = curr_state + 1
  59.                 REDIM _PRESERVE stat(curr_state) AS __state
  60.             CASE "]"
  61.                 curr_state = curr_state - 1
  62.                 px = stat(curr_state).x
  63.                 py = stat(curr_state).y
  64.                 ang2 = stat(curr_state).ang
  65.                 REDIM _PRESERVE stat(curr_state) AS __state
  66.             CASE "|"
  67.                 ang2 = ang2 + 180
  68.             CASE ">"
  69.                 r = r * r_f
  70.             CASE "<"
  71.                 r = r / r_f
  72.             CASE "#"
  73.                 w = w + w_f
  74.             CASE "!"
  75.                 w = w - w_f
  76.         END SELECT
  77.     NEXT
  78.  
  79. 'L-System Function
  80. FUNCTION generateGrammer$ (axiom$, rule() AS rule_type, n~&)
  81.     axiom2$ = axiom$
  82.     axiom3$ = ""
  83.     DO
  84.         FOR i = 1 TO LEN(axiom2$)
  85.             ca$ = MID$(axiom2$, i, 1)
  86.             nca$ = ca$
  87.             FOR j = 0 TO UBOUND(rule)
  88.                 IF rule(j).token = ca$ THEN
  89.                     nca$ = RTRIM$(rule(j).replace)
  90.                     EXIT FOR
  91.                 END IF
  92.             NEXT
  93.             axiom3$ = axiom3$ + nca$
  94.         NEXT
  95.         axiom2$ = axiom3$
  96.         axiom3$ = ""
  97.         count = count + 1
  98.         PRINT ".";
  99.     LOOP UNTIL count >= n~&
  100.     generateGrammer$ = axiom2$
  101.  
  102.  
  103.  
  104.  

9. Bushes [Type 2]

Code: QB64: [Select]
  1. DEFDBL A-Z
  2. _TITLE "Bushes [Type 2]"
  3.  
  4. SCREEN _NEWIMAGE(800, 700, 32)
  5.  
  6. TYPE rule_type
  7.     token AS STRING * 1
  8.     replace AS STRING * 256
  9. TYPE __state
  10.     x AS DOUBLE
  11.     y AS DOUBLE
  12.     ang AS DOUBLE
  13.  
  14.  
  15. DIM  fractalData$
  16. DIM fractalRule(1) AS rule_type
  17. fractalRule(0).token = "X"
  18. fractalRule(0).replace = "X[-FFF][+FFF]FX"
  19. fractalRule(1).token = "Y"
  20. fractalRule(1).replace = "YFX[+Y][-Y]"
  21. PRINT "Generating"
  22. fractalData$ = generateGrammer("Y", fractalRule(), 7)
  23.  
  24. COLOR _RGBA(255, 255, 255, 145)
  25. drawFractal fractalData$, 25.7, 2, _WIDTH / 2, _HEIGHT, .5, 0, .5
  26.  
  27. SUB drawFractal (grammer$, ang, r, x, y, w, r_f, w_f)
  28.  
  29.     REDIM stat(1) AS __state
  30.     curr_state = 1
  31.     ang2 = 0
  32.     px = x
  33.     py = y
  34.     FOR i = 1 TO LEN(grammer$)
  35.         ca$ = MID$(grammer$, i, 1)
  36.         SELECT CASE ca$
  37.             CASE "F"
  38.                 xx = COS(_D2R(ang2 - 90)) * r + px
  39.                 yy = SIN(_D2R(ang2 - 90)) * r + py
  40.  
  41.                 LINE (px, py)-(xx, yy)
  42.                 'thickLine px, py, xx, yy, w
  43.                 px = xx
  44.                 py = yy
  45.             CASE "f"
  46.                 xx = COS(_D2R(ang2)) * r + px
  47.                 yy = SIN(_D2R(ang2)) * r + py
  48.                 px = xx
  49.                 py = yy
  50.             CASE "+"
  51.                 ang2 = ang2 + ang
  52.             CASE "-"
  53.                 ang2 = ang2 - ang
  54.             CASE "["
  55.                 stat(curr_state).x = px
  56.                 stat(curr_state).y = py
  57.                 stat(curr_state).ang = ang2
  58.                 curr_state = curr_state + 1
  59.                 REDIM _PRESERVE stat(curr_state) AS __state
  60.             CASE "]"
  61.                 curr_state = curr_state - 1
  62.                 px = stat(curr_state).x
  63.                 py = stat(curr_state).y
  64.                 ang2 = stat(curr_state).ang
  65.                 REDIM _PRESERVE stat(curr_state) AS __state
  66.             CASE "|"
  67.                 ang2 = ang2 + 180
  68.             CASE ">"
  69.                 r = r * r_f
  70.             CASE "<"
  71.                 r = r / r_f
  72.             CASE "#"
  73.                 w = w + w_f
  74.             CASE "!"
  75.                 w = w - w_f
  76.         END SELECT
  77.     NEXT
  78.  
  79. 'L-System Function
  80. FUNCTION generateGrammer$ (axiom$, rule() AS rule_type, n~&)
  81.     axiom2$ = axiom$
  82.     axiom3$ = ""
  83.     DO
  84.         FOR i = 1 TO LEN(axiom2$)
  85.             ca$ = MID$(axiom2$, i, 1)
  86.             nca$ = ca$
  87.             FOR j = 0 TO UBOUND(rule)
  88.                 IF rule(j).token = ca$ THEN
  89.                     nca$ = RTRIM$(rule(j).replace)
  90.                     EXIT FOR
  91.                 END IF
  92.             NEXT
  93.             axiom3$ = axiom3$ + nca$
  94.         NEXT
  95.         axiom2$ = axiom3$
  96.         axiom3$ = ""
  97.         count = count + 1
  98.         PRINT ".";
  99.     LOOP UNTIL count >= n~&
  100.     generateGrammer$ = axiom2$
  101.  
  102.  
  103.  
  104.  

10. Bushes [Type 3]

Code: QB64: [Select]
  1. DEFDBL A-Z
  2. _TITLE "Bushes [Type 3]"
  3.  
  4. SCREEN _NEWIMAGE(800, 700, 32)
  5.  
  6. TYPE rule_type
  7.     token AS STRING * 1
  8.     replace AS STRING * 256
  9. TYPE __state
  10.     x AS DOUBLE
  11.     y AS DOUBLE
  12.     ang AS DOUBLE
  13.  
  14.  
  15. DIM  fractalData$
  16. DIM fractalRule(0) AS rule_type
  17.  
  18. fractalRule(0).token = "F"
  19. fractalRule(0).replace = "FF+[+F-F-F]-[-F+F+F]"
  20.  
  21. PRINT "Generating"
  22. fractalData$ = generateGrammer("F", fractalRule(), 6)
  23.  
  24. COLOR _RGBA(255, 255, 255, 40)
  25. drawFractal fractalData$, 22.5, 3, _WIDTH / 2, _HEIGHT, 0, 0, 0
  26.  
  27. SUB drawFractal (grammer$, ang, r, x, y, w, r_f, w_f)
  28.  
  29.     REDIM stat(1) AS __state
  30.     curr_state = 1
  31.     ang2 = 0
  32.     px = x
  33.     py = y
  34.     FOR i = 1 TO LEN(grammer$)
  35.         ca$ = MID$(grammer$, i, 1)
  36.         SELECT CASE ca$
  37.             CASE "F"
  38.                 xx = COS(_D2R(ang2 - 90)) * r + px
  39.                 yy = SIN(_D2R(ang2 - 90)) * r + py
  40.  
  41.                 LINE (px, py)-(xx, yy)
  42.                 'thickLine px, py, xx, yy, w
  43.                 px = xx
  44.                 py = yy
  45.             CASE "f"
  46.                 xx = COS(_D2R(ang2)) * r + px
  47.                 yy = SIN(_D2R(ang2)) * r + py
  48.                 px = xx
  49.                 py = yy
  50.             CASE "+"
  51.                 ang2 = ang2 + ang
  52.             CASE "-"
  53.                 ang2 = ang2 - ang
  54.             CASE "["
  55.                 stat(curr_state).x = px
  56.                 stat(curr_state).y = py
  57.                 stat(curr_state).ang = ang2
  58.                 curr_state = curr_state + 1
  59.                 REDIM _PRESERVE stat(curr_state) AS __state
  60.             CASE "]"
  61.                 curr_state = curr_state - 1
  62.                 px = stat(curr_state).x
  63.                 py = stat(curr_state).y
  64.                 ang2 = stat(curr_state).ang
  65.                 REDIM _PRESERVE stat(curr_state) AS __state
  66.             CASE "|"
  67.                 ang2 = ang2 + 180
  68.             CASE ">"
  69.                 r = r * r_f
  70.             CASE "<"
  71.                 r = r / r_f
  72.             CASE "#"
  73.                 w = w + w_f
  74.             CASE "!"
  75.                 w = w - w_f
  76.         END SELECT
  77.     NEXT
  78.  
  79. 'L-System Function
  80. FUNCTION generateGrammer$ (axiom$, rule() AS rule_type, n~&)
  81.     axiom2$ = axiom$
  82.     axiom3$ = ""
  83.     DO
  84.         FOR i = 1 TO LEN(axiom2$)
  85.             ca$ = MID$(axiom2$, i, 1)
  86.             nca$ = ca$
  87.             FOR j = 0 TO UBOUND(rule)
  88.                 IF rule(j).token = ca$ THEN
  89.                     nca$ = RTRIM$(rule(j).replace)
  90.                     EXIT FOR
  91.                 END IF
  92.             NEXT
  93.             axiom3$ = axiom3$ + nca$
  94.         NEXT
  95.         axiom2$ = axiom3$
  96.         axiom3$ = ""
  97.         count = count + 1
  98.         PRINT ".";
  99.     LOOP UNTIL count >= n~&
  100.     generateGrammer$ = axiom2$
  101.  
  102.  
  103.  
Title: Re: Fractals! : Part 2
Post by: bplus on August 07, 2020, 12:54:33 pm
@Ashish I don't know if I tried all these when you posted originally but Leaf #6 is currently not working.

Looking it over it seems all the arrays could be set once and for all instead of setting over and over in the recursive part of the app.
Title: Re: Fractals! : Part 2
Post by: Ashish on August 07, 2020, 01:01:32 pm
@bplus
Hm... It works for me.
Why is it not working for you is very surprising...

 
Title: Re: Fractals! : Part 2
Post by: Ashish on August 07, 2020, 01:11:48 pm
Try this one -
Code: QB64: [Select]
  1. 'Coded By Ashish in QB64
  2. _TITLE "Leaf"
  3. SCREEN _NEWIMAGE(800, 600, 32)
  4. DIM SHARED sx, sy, ox, oy, z
  5. ox = _WIDTH * .34
  6. oy = _HEIGHT
  7. z = 700
  8. COLOR _RGBA(0, 255, 0, 10)
  9. drawLeaf 0, 0, 0
  10.     p = sx: q = sy
  11.     drawLeaf p, q, 0
  12.     _LIMIT 30
  13.  
  14. SUB drawLeaf (x!, y!, N AS _UNSIGNED LONG)
  15.     STATIC a(3) AS SINGLE, b(3) AS SINGLE, c(3) AS SINGLE, d(3) AS SINGLE, e(3) AS SINGLE, f(3) AS SINGLE
  16.     IF N = 0 THEN
  17.         a(0) = 0: a(1) = .7248: a(2) = .1583: a = (3) = .3386
  18.         b(0) = .2439: b(1) = .0337: b(2) = -.1297: b(3) = .3694
  19.         c(0) = 0: c(1) = -.0253: c(2) = .355: c(3) = .2227
  20.         d(0) = .3053: d(1) = .7426: d(2) = .3676: d(3) = -.0756
  21.         e(0) = 0: e(1) = .206: e(2) = .1383: e(3) = .0679
  22.         f(0) = 0: f(1) = .2538: f(2) = .175: f(3) = .0826
  23.     END IF
  24.     k = INT(RND * 4)
  25.     xx! = a(k) * x! + b(k) * y! + e(k)
  26.     yy! = c(k) * x! + d(k) * y! + f(k)
  27.     PSET (xx! * z + ox, -yy! * z + oy)
  28.  
  29.     IF N < 10000 THEN
  30.         drawLeaf xx!, yy!, N + 1
  31.     ELSE sx = xx!: sy = yy!
  32.     END IF
  33.  
  34.  
Title: Re: Fractals! : Part 2
Post by: bplus on August 07, 2020, 01:26:30 pm
Got it working with this, simplified version:
Code: QB64: [Select]
  1. 'Coded By Ashish in QB64  B+ start mod fix 2020-08-07
  2. 'ref   https://www.qb64.org/forum/index.php?topic=182.msg955#msg955
  3. _TITLE "Leaf"
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5. _DELAY .25
  6.  
  7. ' >>>>>>>>>>> moved this section out of drawLeaf
  8. DIM SHARED a(3) AS SINGLE, b(3) AS SINGLE, c(3) AS SINGLE, d(3) AS SINGLE, e(3) AS SINGLE, f(3) AS SINGLE
  9. a(0) = 0: a(1) = .7248: a(2) = .1583: a = (3) = .3386
  10. b(0) = .2439: b(1) = .0337: b(2) = -.1297: b(3) = .3694
  11. c(0) = 0: c(1) = -.0253: c(2) = .355: c(3) = .2227
  12. d(0) = .3053: d(1) = .7426: d(2) = .3676: d(3) = -.0756
  13. e(0) = 0: e(1) = .206: e(2) = .1383: e(3) = .0679
  14. f(0) = 0: f(1) = .2538: f(2) = .175: f(3) = .0826
  15.  
  16.  
  17. DIM SHARED sx, sy, ox, oy, z
  18. ox = _WIDTH * .34
  19. oy = _HEIGHT
  20. z = 700
  21. COLOR _RGB32(0, 255, 0, 10) '<<< added 32 version RGB
  22.  
  23.     p = sx: q = sy
  24.     drawLeaf p, q
  25. LOOP UNTIL _KEYDOWN(27) ' from INKEY$ <> ""
  26.  
  27. SUB drawLeaf (x!, y!) ' changed N from _unsigned nope remove completely!
  28.     k = INT(RND * 4)
  29.     xx! = a(k) * x! + b(k) * y! + e(k)
  30.     yy! = c(k) * x! + d(k) * y! + f(k)
  31.     LINE (xx! * z + ox, -yy! * z + oy)-STEP(2, 0), , BF
  32.     sx = xx!: sy = yy!
  33.     '....................... removed recursive calls because you do that in main loop
  34.  
  35.  
  36.  
  37.  
Title: Re: Fractals! : Part 2
Post by: bplus on August 07, 2020, 01:28:17 pm
@Ashish yes, your 2nd version works!


BTW these are very nice, glad to be reminded of them again!
Title: Re: Fractals! : Part 2
Post by: SierraKen on August 11, 2020, 05:10:14 pm
Awesome, your second Leaf works on my computer now too Ashish, thank you! I just put everyone's fractals in one folder so I don't lose any of them. :) I am also backing up all my QB64 apps.
Title: Re: Fractals! : Part 2
Post by: Aurel on August 13, 2020, 04:18:14 pm
Ashish
your fractals are simply great !
Title: Re: Fractals! : Part 2
Post by: Bolee on February 17, 2021, 05:48:36 am
Cant get to Fractals part 1.


I have made some fractals in VB6 which I will try to convert to QB64. (Made fern, circle fractal, pythagoras tree, binary tree)
Title: Re: Fractals! : Part 2
Post by: bplus on February 17, 2021, 12:01:45 pm
Cant get to Fractals part 1.


I have made some fractals in VB6 which I will try to convert to QB64. (Made fern, circle fractal, pythagoras tree, binary tree)

Sure! Here is the Platinum Edition:
Code: QB64: [Select]
  1. _Title "Ashish Fractals Plus! mods by bplus 2017-10-19, press space to return to menu"
  2. Dim Shared lowX, highX, lowY, highY
  3.  
  4.     Screen 0
  5.     Cls
  6.     cp 2, "Fractal Menu:"
  7.     lp 4, 25, " 1 Circle"
  8.     lp 5, 25, " 2 Arc"
  9.     lp 6, 25, " 3 Tree"
  10.     lp 7, 25, " 4 Quad"
  11.     lp 8, 25, " 5 Quad & Circle"
  12.     lp 9, 25, " 6 Squares forming triangle"
  13.     lp 10, 25, " 7 Sierpinski Carpet"
  14.     lp 11, 25, " 8 Kite"
  15.     lp 12, 25, " 9 Dragon"
  16.     lp 13, 25, "10 Triangle of Circles"
  17.     lp 14, 25, "11 Vicsek"
  18.     lp 15, 25, "12 Circle Illusion?"
  19.     lp 16, 25, "13 Sierpinski Triangle"
  20.     lp 17, 25, "14 Plus, another variation"
  21.     lp 18, 25, "15 Sierpinski Fies a Kite"
  22.     lp 20, 25, "16 Exit"
  23.     Locate 22, 20
  24.     Input "Enter the menu number of your choice "; menu
  25.  
  26.     Select Case menu
  27.         Case 1: CircleFrac
  28.         Case 2: arcFrac
  29.         Case 3: treeFrac
  30.         Case 4: quadFrac
  31.         Case 5: qncFrac
  32.         Case 6: tnsFrac
  33.         Case 7: sierCarFrac
  34.         Case 8: KiteFrac
  35.         Case 9: dragonFrac
  36.         Case 10: TofCsFrac
  37.         Case 11: VicsekFrac
  38.         Case 12: cIllusFrac
  39.         Case 13: SierTriFrac
  40.         Case 14: PlusFrac
  41.         Case 15: SierFliesKiteFrac
  42.         Case 16: End
  43.     End Select
  44.  
  45. Sub arcFrac
  46.     Screen _NewImage(800, 600, 32)
  47.     r = 40
  48.     needUpdate = 1
  49.     Do
  50.         If _KeyDown(32) Then Exit Sub
  51.         If _KeyDown(Asc("w")) Then r = r + s: needUpdate = 1
  52.         If _KeyDown(Asc("s")) And r > 2 Then r = r - s: needUpdate = 1
  53.         If needUpdate = 1 Then
  54.             needUpdate = 0
  55.             ttl "Arc Fractal, Press 'w' and 's' to zoom-in and zoom-out, space to return to Menu"
  56.             drawArc 400, 300, r, 1
  57.             _Display
  58.             Cls
  59.         End If
  60.         _Limit 60
  61.         s = map(r, 1, 10000, 1, 300)
  62.     Loop
  63.  
  64. Sub CF (x, y, r)
  65.     Circle (x, y), r
  66.     If r > 2 Then
  67.         CF x + r, y, r / 2
  68.         CF x - r, y, r / 2
  69.     End If
  70.  
  71. Sub cIll (x, y, r)
  72.     Line (x, y)-(x - r, y)
  73.     Line (x, y)-(x + r, y)
  74.     Line (x, y)-(x, y - r)
  75.     Line (x, y)-(x, y + r)
  76.     If r > 21 Then
  77.         cIll (x - r) + r / 3, y, r / 2
  78.         cIll (x + r) - r / 3, y, r / 2
  79.         cIll x, (y + r) - r / 3, r / 2
  80.         cIll x, (y - r) + r / 3, r / 2
  81.     End If
  82.  
  83. Sub cIllusFrac
  84.     'Coded By Ashish with <3
  85.     'Can you observe circle in this fractal!? (no, sorry)
  86.     'I was able to do so... :D
  87.     Screen _NewImage(800, 600, 32)
  88.     cIll 400, 300, 200
  89.     ttl "Circle with 4 line variation, Illusion? press any..."
  90.     _Display
  91.     Sleep
  92.  
  93. Sub CircleFrac
  94.     'Idea from  [youtube]https://youtu.be/jPsZwrV9ld0[/youtube]
  95.     Screen _NewImage(800, 600, 32)
  96.     r = 50
  97.     needUpdate = 1
  98.     Do
  99.         If _KeyDown(32) Then Exit Sub
  100.         If _KeyDown(Asc("w")) Then r = r + s: needUpdate = 1
  101.         If _KeyDown(Asc("s")) And r > 2 Then r = r - s: needUpdate = 1
  102.         If needUpdate = 1 Then
  103.             needupadte = 0
  104.             ttl "Circle Fractal, Press 'w' and 's' to zoom-in and zoom-out, space to return to Menu"
  105.             CF 400, 300, r
  106.             _Display
  107.             Cls
  108.         End If
  109.         s = map(r, 1, 10000, 1, 300)
  110.         _Limit 60
  111.     Loop
  112.  
  113. Sub cp (r%, txt$)
  114.     Locate r%, (80 - Len(txt$)) / 2: Print txt$
  115.  
  116. 'Calculate the distance between two points.
  117. Function dist! (x1!, y1!, x2!, y2!)
  118.     dist! = Sqr((x2! - x1!) ^ 2 + (y2! - y1!) ^ 2)
  119.  
  120. Sub dragonFrac
  121.     Screen _NewImage(800, 600, 32)
  122.     drawDragon 400, 300, 400, 300, 60, Rnd * _Pi(2), Rnd * 255, Rnd * 255, Rnd * 255
  123.     ttl "Dragon Curve Fractal, press any except space for more..."
  124.     _Display
  125.     Sleep
  126.     Do
  127.         If _KeyDown(32) Then Exit Sub
  128.         xx = Rnd * _Width
  129.         yy = Rnd * _Height
  130.         drawDragon xx, yy, xx, yy, 60, Rnd * _Pi(2), Rnd * 255, Rnd * 255, Rnd * 255 ': f = 0
  131.         Line (0, 0)-(_Width, _Height), _RGBA(0, 0, 0, 30), BF
  132.         f = f + 1
  133.         ttl "Dragon Curve Fractal, press space to exit"
  134.         _Display
  135.         _Limit 60
  136.     Loop
  137.  
  138. Sub drawArc (x, y, r, f)
  139.     If f = 1 Then
  140.         Circle (x, y), r, , 0, _Pi
  141.     Else
  142.         Circle (x, y), r, , _Pi, _Pi(2)
  143.     End If
  144.     If r > 2 Then
  145.         If f = 1 Then e = 0 Else e = 1
  146.         drawArc x + r, y, r / 2, e
  147.         drawArc x - r, y, r / 2, e
  148.     End If
  149.  
  150. Sub drawDragon (cx, cy, x, y, r, a, mR, mG, mB)
  151.     d = dist(x, y, cx, cy)
  152.     Color _RGB(map(d, 0, 200, mR, 0), map(d + y, y, d + y, 0, mG), map(d, 0, 200, 0, mB))
  153.     fcirc x, y, r
  154.     If r > 1 Then
  155.         drawDragon cx, cy, x + r * 1.75 * Cos(a), y + r * 1.75 * Sin(a), r * 0.75, a - 0.62, mR, mG, mB
  156.         drawDragon cx, cy, x + r * 1.75 * Cos(a + _Pi), y + r * 1.75 * Sin(a + _Pi), r * 0.75, (a + _Pi) - 0.62, mR, mG, mB ')+_PI
  157.     End If
  158.  
  159. Sub drawKite (x, y, s, a)
  160.     Line (x, y)-(x + s * Cos(_Pi(2) - a), (y - s) + s * Sin(_Pi(2) - a))
  161.     Line (x, y)-(x + s * Cos(_Pi + a), (y - s) + s * Sin(_Pi + a))
  162.     If s > 1 Then
  163.         drawKite x + s * Cos(_Pi(2) - a), (y - s) + s * Sin(_Pi(2) - a), s / 2, a
  164.         drawKite x + s * Cos(_Pi + a), (y - s) + s * Sin(_Pi + a), s / 2, a
  165.     End If
  166.  
  167. Sub drawKite2 (xx, yy, s, a)
  168.     x = xx: y = yy
  169.     x2 = x + 3 * s * Cos(_Pi(1 / 2) - a / 2): y2 = y + 3 * s * Sin(_Pi(1 / 2) - a / 2)
  170.     x3 = x + 3 * s * Cos(_Pi(1 / 2) + a / 2): y3 = y + 3 * s * Sin(_Pi(1 / 2) + a / 2)
  171.     SierLineTri x, y, x2, y2, x3, y3, 0
  172.     If s > 10 Then
  173.         drawKite2 x + 1 * s * Cos(_Pi(2) - a), (y - s) + 1 * s * Sin(_Pi(2) - a), s / 2, a
  174.         drawKite2 x + 1 * s * Cos(_Pi + a), (y - s) + 1 * s * Sin(_Pi + a), s / 2, a
  175.     End If
  176.  
  177. Sub drawTree (x, y, r, a, s)
  178.     If r < 14 Then c~& = _RGB(10, 200, 10) Else c~& = _RGB(160, 10, 10)
  179.     internalp5line x, y, x + r * Cos(a - s), y + r * Sin(a - s), r / 10, c~&
  180.     internalp5line x, y, x + r * Cos(a + s * 3), y + r * Sin(a + s * 3), r / 10, c~&
  181.     If r > 2 Then
  182.         drawTree x + r * Cos(a - s), y + r * Sin(a - s), r * 0.67, a - s, s
  183.         drawTree x + r * Cos(a + s * 3), y + r * Sin(a + s * 3), r * 0.67, a + s * 3, s
  184.     End If
  185.  
  186. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  187. Sub fcirc (CX As Long, CY As Long, R As Long)
  188.     Dim subRadius As Long, RadiusError As Long
  189.     Dim X As Long, Y As Long
  190.     subRadius = Abs(R)
  191.     RadiusError = -subRadius
  192.     X = subRadius
  193.     Y = 0
  194.     If subRadius = 0 Then PSet (CX, CY): Exit Sub
  195.     ' Draw the middle span here so we don't draw it twice in the main loop,
  196.     ' which would be a problem with blending turned on.
  197.     Line (CX - X, CY)-(CX + X, CY), , BF
  198.     While X > Y
  199.         RadiusError = RadiusError + Y * 2 + 1
  200.         If RadiusError >= 0 Then
  201.             If X <> Y + 1 Then
  202.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  203.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  204.             End If
  205.             X = X - 1
  206.             RadiusError = RadiusError - X * 2
  207.         End If
  208.         Y = Y + 1
  209.         Line (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  210.         Line (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  211.     Wend
  212.  
  213. 'taken from QB64's p5.js
  214. 'http://bit.ly/p5jsbas
  215. Sub internalp5line (x0!, y0!, x1!, y1!, s!, col~&)
  216.     dx! = x1! - x0!
  217.     dy! = y1! - y0!
  218.     d! = Sqr(dx! * dx! + dy! * dy!)
  219.     For i = 0 To d!
  220.         Color col~&
  221.         fcirc x0! + dxx!, y0! + dyy!, s!
  222.         dxx! = dxx! + dx! / d!
  223.         dyy! = dyy! + dy! / d!
  224.     Next
  225.  
  226. Sub KiteFrac
  227.     Screen _NewImage(800, 600, 32)
  228.     ttl "Kite Fractal, press any"
  229.     drawKite 400, 500, 140, .5
  230.     _Display
  231.     Sleep
  232.  
  233. Sub lp (r%, c%, txt$)
  234.     Locate r%, c%: Print txt$
  235.  
  236. 'taken from QB64's p5.js
  237. 'http://bit.ly/p5jsbas
  238. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  239.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  240.  
  241. Sub Plus (x, y, r)
  242.     'variation BF, B, B, BF
  243.     'variation #2 BF, BF, BF, BF
  244.     'variation #3 B, B, B, B
  245.     'How many plus can you find?
  246.     Color _RGB(r, 255 - r, 255 - r)
  247.     Line (x, y)-(x - r, y - r), , BF
  248.     Line (x, y)-(x + r, y - r), , B
  249.     Line (x, y)-(x - r, y + r), , B
  250.     Line (x, y)-(x + r, y + r), , BF
  251.     If r > 6 Then
  252.         Plus x - r / 2, y - r / 2, r / 2.3
  253.         Plus x + r / 2, y - r / 2, r / 2.3
  254.         Plus x - r / 2, y + r / 2, r / 2.3
  255.         Plus x + r / 2, y + r / 2, r / 2.3
  256.     End If
  257.  
  258. Sub PlusFrac
  259.     'playing with Ashish circle with 4 line by bplus 2017-10-16
  260.     Screen _NewImage(800, 600, 32)
  261.     Plus 400, 300, 290
  262.     ttl "Plus, another variation "
  263.     _Display
  264.     Sleep
  265.  
  266. Sub qncFrac
  267.     Screen _NewImage(800, 600, 32)
  268.     r = 100
  269.     needupdate = 1
  270.     Do
  271.         If _KeyDown(32) Then Exit Sub
  272.         If _KeyDown(Asc("w")) Then r = r + s: needupdate = 1
  273.         If _KeyDown(Asc("s")) And r > 2 Then r = r - s: needupdate = 1
  274.         If needupdate = 1 Then
  275.             needupdate = 0
  276.             ttl "Quad Inside Circle Inside Quad, press w to widen, s to shrink, space to exit"
  277.             quad_circle 400, 300, 0, 0, r, 0
  278.             _Display
  279.             Cls
  280.             s = map(r, 1, 10000, 1, 300)
  281.         End If
  282.         _Limit 40
  283.     Loop
  284.  
  285. Sub quad_circle (x, y, x2, y2, r, e)
  286.     If e = 1 Then
  287.         Line (x, y)-(x2, y2), , B
  288.     Else
  289.         Circle (x, y), r
  290.     End If
  291.     If r > 2 Then
  292.         If e = 1 Then
  293.             If x2 > x Then newR = x2 - x Else newR = x - x2
  294.             quad_circle (x + x2) / 2, (y + y2) / 2, 0, 0, newR / 2, 0
  295.         Else
  296.             tx1 = x + r * Cos(_Pi - .7)
  297.             ty1 = y + r * Sin(_Pi - .7)
  298.             tx2 = x + r * Cos(_Pi(2) - .7)
  299.             ty2 = y + r * Sin(_Pi(2) - .7)
  300.             quad_circle tx1, ty1, tx2, ty2, r / 2, 1
  301.         End If
  302.     End If
  303.  
  304. Sub quad_fractal (x, y, r, e)
  305.     If _KeyDown(32) Then Exit Sub
  306.     Line (x - r, y - r)-(x + r, y - r)
  307.     Line (x + r, y - r)-(x + r, y + r)
  308.     Line (x + r, y + r)-(x - r, y + r)
  309.     Line (x - r, y + r)-(x - r, y - r)
  310.     If r > e Then
  311.         quad_fractal x - r, y - r, r / 2, e
  312.         quad_fractal x + r, y - r, r / 2, e
  313.         quad_fractal x + r, y + r, r / 2, e
  314.         quad_fractal x - r, y + r, r / 2, e
  315.     End If
  316.  
  317. Sub quadFrac
  318.     Screen _NewImage(800, 600, 32)
  319.     k = 100: dir = .5
  320.     Do
  321.         If _KeyDown(32) Then Exit Sub
  322.         Cls
  323.         ttl "Quads!!, press space to exit"
  324.         quad_fractal 400, 300, 100, k
  325.         _Display
  326.         _Limit 2
  327.         k = k * dir
  328.         If k < 2 Then dir = 2
  329.         If k > 100 Then dir = .5
  330.     Loop
  331.  
  332. Sub SC (x, y, r)
  333.     Line (x - r, y - r)-(x + r, y + r), _RGB(map(x, lowX, highX, 0, 255), map(y, lowY, highY, 255, 0), map(x + y, lowX + lowY, highX + highY, 255, 0)), BF
  334.     If r > 3 Then
  335.         v = r * 2
  336.         SC x, y - v, r / 3
  337.         SC x, y + v, r / 3
  338.         SC x + v, y, r / 3
  339.         SC x - v, y, r / 3
  340.         SC x - v, y - v, r / 3
  341.         SC x + v, y + v, r / 3
  342.         SC x - v, y + v, r / 3
  343.         SC x + v, y - v, r / 3
  344.     End If
  345.  
  346. Sub SC0 (x, y, r)
  347.     Line (x - r, y - r)-(x + r, y + r), _RGB(0, 0, 0), BF
  348.     If x - r < lowX Then lowX = x - r
  349.     If x + r > highX Then highX = x + r
  350.     If y - r < lowY Then lowY = y - r
  351.     If y + r > highY Then highY = y + r
  352.     If r > 3 Then
  353.         v = r * 2
  354.         SC0 x, y - v, r / 3
  355.         SC0 x, y + v, r / 3
  356.         SC0 x + v, y, r / 3
  357.         SC0 x - v, y, r / 3
  358.         SC0 x - v, y - v, r / 3
  359.         SC0 x + v, y + v, r / 3
  360.         SC0 x - v, y + v, r / 3
  361.         SC0 x + v, y - v, r / 3
  362.     End If
  363.  
  364. Sub sierCarFrac
  365.     Screen _NewImage(1000, 700, 32)
  366.     lowX = 500
  367.     highX = 500
  368.     highY = 500
  369.     lowY = 500
  370.     Cls , _RGB(255, 255, 255)
  371.     SC0 500, 350, 120
  372.     Line (0, 0)-(lowX, _Height), _RGB(0, 0, 0), BF
  373.     Line (_Width - 1, 0)-(highX, _Height), _RGB(0, 0, 0), BF
  374.     Line (0, 0)-(_Width, lowY), _RGB(0, 0, 0), BF
  375.     Line (0, _Height)-(_Width, highY), _RGB(0, 0, 0), BF
  376.     ttl "Sierpinski_Carpet, press any"
  377.     _Display
  378.     Sleep
  379.     SC 500, 350, 120
  380.     ttl "Sierpinski_Carpet, press any"
  381.     _Display
  382.     Sleep
  383.  
  384. Sub SierFliesKiteFrac
  385.     ' after playing with Ashish Kite Fractal,  by bplus 2017-10-16
  386.     Screen _NewImage(1200, 700, 32)
  387.     While 1
  388.         Cls
  389.         If _KeyDown(32) Then Exit Sub
  390.         drawKite2 600, 540, 200, a
  391.         ttl "Sierpinski flies a Kite, press space to exit"
  392.         _Display
  393.         _Limit 20
  394.         a = a + _Pi(2 / 360)
  395.     Wend
  396.  
  397. Sub SierLineTri (x1, y1, x2, y2, x3, y3, depth)
  398.     If depth = 0 Then 'draw out triangle if level 0
  399.         Line (x1, y1)-(x2, y2)
  400.         Line (x2, y2)-(x3, y3)
  401.         Line (x1, y1)-(x3, y3)
  402.     End If
  403.     'find midpoints
  404.     If x2 < x1 Then mx1 = (x1 - x2) / 2 + x2 Else mx1 = (x2 - x1) / 2 + x1
  405.     If y2 < y1 Then my1 = (y1 - y2) / 2 + y2 Else my1 = (y2 - y1) / 2 + y1
  406.     If x3 < x2 Then mx2 = (x2 - x3) / 2 + x3 Else mx2 = (x3 - x2) / 2 + x2
  407.     If y3 < y2 Then my2 = (y2 - y3) / 2 + y3 Else my2 = (y3 - y2) / 2 + y2
  408.     If x3 < x1 Then mx3 = (x1 - x3) / 2 + x3 Else mx3 = (x3 - x1) / 2 + x1
  409.     If y3 < y1 Then my3 = (y1 - y3) / 2 + y3 Else my3 = (y3 - y1) / 2 + y1
  410.     Line (mx1, my1)-(mx2, my2) '  'draw all inner triangles
  411.     Line (mx2, my2)-(mx3, my3)
  412.     Line (mx1, my1)-(mx3, my3)
  413.     If depth < 4 Then 'not done so call me again
  414.         SierLineTri x1, y1, mx1, my1, mx3, my3, depth + 1
  415.         SierLineTri x2, y2, mx1, my1, mx2, my2, depth + 1
  416.         SierLineTri x3, y3, mx3, my3, mx2, my2, depth + 1
  417.     End If
  418.  
  419. Sub SierTri (x, y, r)
  420.     Line (x + r * Cos(_D2R(330)), y + r * Sin(_D2R(330)))-(x + r * Cos(_D2R(90)), y + r * Sin(_D2R(90)))
  421.     Line (x + r * Cos(_D2R(90)), y + r * Sin(_D2R(90)))-(x + r * Cos(_D2R(210)), y + r * Sin(_D2R(210)))
  422.     Line (x + r * Cos(_D2R(210)), y + r * Sin(_D2R(210)))-(x + r * Cos(_D2R(330)), y + r * Sin(_D2R(330)))
  423.     If r > 4 Then
  424.         SierTri x + r * Cos(_D2R(30)), y + r * Sin(_D2R(30)), r / 2
  425.         SierTri x + r * Cos(_D2R(150)), y + r * Sin(_D2R(150)), r / 2
  426.         SierTri x + r * Cos(_D2R(270)), y + r * Sin(_D2R(270)), r / 2
  427.     End If
  428.  
  429. Sub SierTriFrac
  430.     Screen _NewImage(800, 600, 32)
  431.     SierTri 400, 400, 160
  432.     ttl "Sierpinski Triangle, press any..."
  433.     _Display
  434.     Sleep
  435.  
  436. Sub TofCs (x, y, r, t)
  437.     Color _RGB(Rnd * 255, Rnd * 255, Rnd * 255)
  438.     fcirc x, y, r
  439.     If r > 2 Then
  440.         TofCs x + r * 1.75 * Cos(t), y + r * 1.75 * Sin(t), r * 0.75, t
  441.         TofCs x + r * 1.75 * Cos(_Pi + t), y + r * 1.75 * Sin(_Pi - t), r * 0.75, t
  442.     End If
  443.  
  444. Sub TofCsFrac 'modified for speed as Petr had shown
  445.     Screen _NewImage(1000, 600, 32)
  446.     TofCs 500, 200, 80, .5
  447.     ttl "Triangle Formed By Circle, press any..."
  448.     _Display
  449.     Sleep
  450.  
  451. Sub tns (x, y, r)
  452.     Line (x - r, y - r)-(x + r, y + r), _RGB(Rnd * 255, Rnd * 255, Rnd * 255), BF
  453.     If r > 1 Then
  454.         v = r * 2
  455.         tns x, y - v, r / 2
  456.         tns x + v, y - r * 2, r / 2
  457.         tns x + v, y, r / 2
  458.     End If
  459.  
  460. Sub tnsFrac
  461.     ttl "Square_formed_triangle, press any..."
  462.     Screen _NewImage(800, 600, 32)
  463.     tns 250, 450, 100
  464.     _Display
  465.     Sleep
  466.  
  467. Sub treeFrac
  468.     Screen _NewImage(800, 600, 32)
  469.     radius = 130
  470.     Do
  471.         Cls
  472.         ttl "Fractal_Trees, press space to return to Menu"
  473.         If _KeyDown(32) Then Exit Sub
  474.         drawTree 400, 400, radius, _Pi(3 / 2), s
  475.         internalp5line 400, 600, 400, 400, radius / 10, _RGB(160, 10, 10)
  476.         _Display
  477.         _Limit 40
  478.         s = Abs(Sin(v#)) * 0.25 + 0.2
  479.         v# = v# + 0.01
  480.     Loop
  481.  
  482. Sub ttl (txt$)
  483.     Color _RGB(0, 200, 200)
  484.     lp 2, 5, txt$
  485.     Color _RGB(255, 255, 255)
  486.  
  487. Sub vicsek (x, y, r)
  488.     Line (x, y)-(x - r, y)
  489.     Line (x, y)-(x + r, y)
  490.     Line (x, y)-(x, y - r)
  491.     Line (x, y)-(x, y + r)
  492.     If r > 2 Then
  493.         vicsek x - r, y, r / 3
  494.         vicsek x + r, y, r / 3
  495.         vicsek x, y + r, r / 3
  496.         vicsek x, y - r, r / 3
  497.         vicsek x, y, r / 3
  498.     End If
  499.  
  500. Sub VicsekFrac
  501.     Screen _NewImage(800, 600, 32)
  502.     vicsek 400, 300, 180
  503.     ttl "Vicsek Fractal, press any..."
  504.     _Display
  505.     Sleep
  506.  

Platinum because this Edition has Sierpinsky Flies a Kite a mod of Ashish Kite: