/examples/tendrils/source.scm

http://github.com/dharmatech/abstracting · Scheme · 91 lines · 53 code · 30 blank · 8 comment · 0 complexity · c9cf1f931406e77af8af9875c2f4155d MD5 · raw file

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; Original version in NodeBox:
  3. ;;
  4. ;; http://www.nodebox.net/code/index.php/Tendrils
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6. (define (tendril pos width)
  7. (let ((angle (- (random (* 2 pi)) pi))
  8. (v 0)
  9. (segments '()))
  10. (let ((grow
  11. (lambda (distance curl step)
  12. (set! pos (pt+ pos (pt (* (cos angle) distance)
  13. (* (sin angle) distance))))
  14. (set! v (+ v (random (- step) step)))
  15. (set! v (* v (+ 0.9 (* curl 0.1))))
  16. (set! angle (+ angle v))
  17. (set! segments (cons pos segments))))
  18. (draw
  19. (let ((draw-segment
  20. (lambda (fraction position)
  21. (let ((diameter (inexact (* fraction width))))
  22. (circle position diameter)))))
  23. (lambda ()
  24. (for-each-with-fraction draw-segment segments))))
  25. )
  26. (vector 'tendril grow draw))))
  27. (define (tendril-grow tendril distance curl step)
  28. ((vector-ref tendril 1) distance curl step))
  29. (define (tendril-draw tendril)
  30. ((vector-ref tendril 2)))
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32. (define (plant pos tendrils width)
  33. (let ((tendrils (map (lambda (i)
  34. (tendril pos width))
  35. (iota tendrils))))
  36. (let ((grow
  37. (lambda (distance curl step)
  38. (for-each (cut tendril-grow <> distance curl step) tendrils)))
  39. (draw
  40. (lambda ()
  41. (for-each tendril-draw tendrils))))
  42. (vector 'plant grow draw))))
  43. (define (plant-grow plant distance curl step)
  44. ((vector-ref plant 1) distance curl step))
  45. (define (plant-draw plant)
  46. ((vector-ref plant 2)))
  47. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  48. (size 600 600)
  49. (init-nodebox)
  50. (set! *draw*
  51. (lambda ()
  52. (background 0.12 0.12 0.06)
  53. (no-fill)
  54. (stroke 1 0.5)
  55. (set! *stroke-width* 0.5)
  56. (let ((plant (plant (pt (/ *width* 2) (/ *height* 2)) 20 15)))
  57. (do-times 200 (lambda (i)
  58. (plant-grow plant 3.0 1.0 0.02)))
  59. (plant-draw plant))))
  60. (run-nodebox)
  61. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;