Author Topic: Fractals! : Part 2  (Read 24516 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Fractals! : Part 2
« 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.  
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: Fractals! : Part 2
« Reply #1 on: March 15, 2018, 10:32:46 pm »
Very nice set of fractals!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractals! : Part 2
« Reply #2 on: March 16, 2018, 03:21:06 am »
Very nice set of fractals!
Thanks bplus! :)
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 _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Fractals! : Part 2
« Reply #3 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.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractals! : Part 2
« Reply #4 on: March 16, 2018, 08:18:47 am »
@v
That was cool!
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: Fractals! : Part 2
« Reply #5 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.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractals! : Part 2
« Reply #6 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.  
« Last Edit: March 17, 2018, 06:57:22 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: Fractals! : Part 2
« Reply #7 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.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractals! : Part 2
« Reply #8 on: August 07, 2020, 01:01:32 pm »
@bplus
Hm... It works for me.
Why is it not working for you is very surprising...

 
Screenshot_1_.png
« Last Edit: August 07, 2020, 01:07:29 pm 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 Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractals! : Part 2
« Reply #9 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.  
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: Fractals! : Part 2
« Reply #10 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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fractals! : Part 2
« Reply #11 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!
« Last Edit: August 07, 2020, 01:31:24 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fractals! : Part 2
« Reply #12 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.
« Last Edit: August 11, 2020, 06:07:01 pm by SierraKen »

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Fractals! : Part 2
« Reply #13 on: August 13, 2020, 04:18:14 pm »
Ashish
your fractals are simply great !
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline Bolee

  • Newbie
  • Posts: 4
    • View Profile
Re: Fractals! : Part 2
« Reply #14 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)