/examples/empathy/source.scm

http://github.com/dharmatech/abstracting · Scheme · 126 lines · 58 code · 56 blank · 12 comment · 0 complexity · fba14c042ebb47d4fea8177e9d005c62 MD5 · raw file

  1. ;; Original version by Kyle McDonald:
  2. ;;
  3. ;; http://www.openprocessing.org/visuals/?visualID=1182
  4. ;;
  5. ;; Ported to Abstracting by Ed Cavazos
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. (for-each require-lib '("list" "math" "tendrils/nodebox"))
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. (define *number-of-cells* 5000)
  10. (define *base-line-length* 37)
  11. (define *rotation-speed-step* 0.004)
  12. (define *slow-down-rate* 0.97)
  13. (set! *y-increases-up* #f)
  14. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  15. (define (det x1 y1 x2 y2 x3 y3)
  16. (- (* (- x2 x1)
  17. (- y3 y1))
  18. (* (- x3 x1)
  19. (- y2 y1))))
  20. (define (cell x y)
  21. (let ((spin-velocity 0)
  22. (current-angle 0))
  23. (let ((sense
  24. (lambda ()
  25. (if (or (not (= *p-mouse-x* 0))
  26. (not (= *p-mouse-y* 0)))
  27. (set! spin-velocity
  28. (+ spin-velocity
  29. (/ (* *rotation-speed-step*
  30. (det x y *p-mouse-x* *p-mouse-y* *mouse-x* *mouse-y*))
  31. (+ (dist x y *mouse-x* *mouse-y*) 1)))))
  32. (set! spin-velocity (* spin-velocity *slow-down-rate*))
  33. (set! current-angle (+ current-angle spin-velocity))
  34. (let ((d (+ 0.001 (* *base-line-length* spin-velocity))))
  35. (glVertex2d x y)
  36. (glVertex2d (+ x (* d (cos current-angle)))
  37. (+ y (* d (sin current-angle))))))))
  38. (vector 'cell sense))))
  39. (define (sense cell)
  40. ((vector-ref cell 1)))
  41. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  42. (set! *title* "Empathy by Kyle McDonald")
  43. (size 500 500)
  44. ;; (set! *frames-per-second* #t)
  45. (set! *frames-per-second* 30)
  46. (init-nodebox)
  47. (stroke 0.0 0.0 0.0 0.5)
  48. (define *cells*
  49. (list-tabulate *number-of-cells*
  50. (lambda (i)
  51. (let ((theta (+ i (random 0 (/ pi 9))))
  52. (dista (+ 3
  53. (random -3 3)
  54. (* (/ i *number-of-cells*)
  55. (/ *width* 2)
  56. (* (/ (- *number-of-cells* i) *number-of-cells*) 3.3)))))
  57. (cell (+ (/ *width* 2) (* dista (cos theta)))
  58. (+ (/ *height* 2) (* dista (sin theta))))))))
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. (set! *draw*
  61. (lambda ()
  62. (background 1.0)
  63. (glColor4d 0.0 0.0 0.0 0.5)
  64. (glBegin GL_LINES)
  65. (for-each sense *cells*)
  66. (glEnd)
  67. (set! *p-mouse-x* *mouse-x*)
  68. (set! *p-mouse-y* *mouse-y*)))
  69. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  70. (run-nodebox)