Author Topic: Fall Foliage  (Read 5858 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Fall Foliage
« on: October 21, 2017, 01:57:45 pm »
Where were we? Oh Ashish was doing fractals and his last was a marvelous 3D Tree but I thought it a bit bare so I will show how to do leaves. :) maybe then he can make them 3D too!

Code: QB64: [Select]
  1. _TITLE "Fall Foliage 2017-10-21 by bplus"
  2. 'fall foliage.bas SmallBASIC 0.12.9 (B+=MGA) 2017-10-21
  3. 'test landscape and portrait views for Android
  4. 'xmx = min(xmax, 400) : ymx = min(700, ymax) 'portrait
  5. 'OK it's just plain better in landscape view
  6.  
  7. 'now for full viewing enjoyment
  8. 'xmx = xmax : ymx = ymax
  9.  
  10. CONST xmx = 1200
  11. CONST ymx = 700
  12. DEFSNG A-Z
  13.  
  14. rad = _PI(1 / 180)
  15. SCREEN _NEWIMAGE(xmx, ymx, 32)
  16. _SCREENMOVE 100, 20 'adjust as needed _MIDDLE needs a delay .5 or more for me
  17.  
  18.  
  19. n = 3
  20.     IF n < 15 THEN n = n + 3
  21.     horizon = rand%(.8 * ymx, .9 * ymx)
  22.     FOR i = 0 TO horizon
  23.         midInk 0, 0, 128, 10, 120, 128, i / horizon
  24.         lien 0, i, xmx, i
  25.     NEXT
  26.     FOR i = horizon TO ymx
  27.         midInk 70, 108, 30, 60, 10, 5, (i - horizon) / (ymx - horizon)
  28.         lien 0, i, xmx, i
  29.     NEXT
  30.     FOR i = 1 TO xmx * ymx * .00018
  31.         leaf rand%(0, xmx), rand%(horizon * 1.002, ymx)
  32.     NEXT
  33.     IF n < .01 * xmx THEN trees = n ELSE trees = rand%(.002 * xmx, .03 * xmx)
  34.     FOR i = 1 TO trees
  35.         y = horizon + .04 * ymx + i / trees * (ymx - horizon - .1 * ymx)
  36.         r = .01 * y: h = rand%(y * .15, y * .18)
  37.         branch rand%(10, xmx - 10), y, r, 90, h, 0
  38.     NEXT
  39.     fRect xmx, 0, xmax, ymax, 0
  40.     fRect 0, ymx, xmx, ymax, 0
  41.     _DISPLAY
  42.     SLEEP 2
  43.  
  44. SUB branch (xx, yy, startrr, angDD, lengthh, levv)
  45.     x = xx: y = yy
  46.     lev = levv
  47.     length = lengthh
  48.     angD = angDD
  49.     startr = startrr
  50.     x2 = x + COS(rad * (angD)) * length
  51.     y2 = y - SIN(rad * (angD)) * length
  52.     dx = (x2 - x) / length
  53.     dy = (y2 - y) / length
  54.     bc& = _RGB(30 + 6 * lev, 15 + 3 * lev, 5 + 2 * lev)
  55.     FOR i = 0 TO length
  56.         COLOR bc&
  57.         fCirc x + dx * i, y + dy * i, startr
  58.     NEXT
  59.     IF lev > 1 THEN leaf x2, y2
  60.     IF .8 * startr < .1 OR lev > 7 OR length < 3 THEN EXIT SUB
  61.     lev = lev + 1
  62.     branch x2, y2, .8 * startr, angD + 22 + rand%(-10, 19), rand%(.75 * length, .9 * length), lev
  63.     branch x2, y2, .8 * startr, angD - 22 - rand%(-10, 19), rand%(.75 * length, .9 * length), lev
  64.  
  65. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  66. SUB fCirc (CX AS LONG, CY AS LONG, R AS LONG)
  67.     DIM subRadius AS LONG, RadiusError AS LONG
  68.     DIM X AS LONG, Y AS LONG
  69.  
  70.     subRadius = ABS(R)
  71.     RadiusError = -subRadius
  72.     X = subRadius
  73.     Y = 0
  74.  
  75.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  76.  
  77.     ' Draw the middle span here so we don't draw it twice in the main loop,
  78.     ' which would be a problem with blending turned on.
  79.     LINE (CX - X, CY)-(CX + X, CY), , BF
  80.  
  81.     WHILE X > Y
  82.         RadiusError = RadiusError + Y * 2 + 1
  83.         IF RadiusError >= 0 THEN
  84.             IF X <> Y + 1 THEN
  85.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  86.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  87.             END IF
  88.             X = X - 1
  89.             RadiusError = RadiusError - X * 2
  90.         END IF
  91.         Y = Y + 1
  92.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  93.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  94.     WEND
  95.  
  96. SUB fRect (x1, y1, x2, y2, c&)
  97.     LINE (x1, y1)-(x2, y2), c&, BF
  98.  
  99. SUB fRectStep (x1, y1, x2, y2)
  100.     LINE (x1, y1)-STEP(x2, y2), , BF
  101.  
  102. SUB lien (x1, y1, x2, y2)
  103.     LINE (x1, y1)-(x2, y2)
  104.  
  105. SUB leaf (x, y)
  106.     sp = 15: leafs = rand%(xmx * ymx * .00001, xmx * ymx * .00002)
  107.     FOR n = 1 TO leafs
  108.         COLOR _RGB(rand%(50, 250), rand%(25, 255), rand%(0, 40))
  109.         xoff = x + RND * sp - RND * sp
  110.         yoff = y + RND * sp - RND * sp
  111.         woff = 3 + RND * 3
  112.         hoff = 3 + RND * 3
  113.         fRectStep xoff, yoff, woff, hoff
  114.     NEXT
  115.  
  116. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  117.     COLOR _RGB(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  118.  
  119. FUNCTION rand% (lo%, hi%)
  120.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  121.  
  122.  

Here is alternate code and screen shot:
http://www.thejoyfulprogrammer.com/qb64/forum/showthread.php?tid=1051&pid=4609&rndtime=1508574661435543493#pid4609
« Last Edit: October 21, 2017, 02:00:31 pm by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Fall Foliage
« Reply #1 on: October 22, 2017, 05:26:09 am »
Hi BPlus! Its very nice.  I have already written information Ashish about opening this forum, so here it may appear soon!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fall Foliage
« Reply #2 on: October 22, 2017, 08:54:29 am »
Wow! It's really awesome! :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