/examples/random-art/source.scm

http://github.com/dharmatech/abstracting · Scheme · 163 lines · 78 code · 71 blank · 14 comment · 0 complexity · 38ec5f9fdfb43deb8a77d4d1ee3d8523 MD5 · raw file

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. (random-source-randomize! default-random-source)
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4. (define (vector-ref-random v)
  5. (vector-ref v (random-integer (vector-length v))))
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. (define (sin-pi* c) (sin (* pi c)))
  8. (define (cos-pi* c) (cos (* pi c)))
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10. (define (random-product-procedure n)
  11. (display "(* ")
  12. (let ((proc-1 (random-procedure (- n 1)))
  13. (proc-2 (random-procedure (- n 1))))
  14. (display ")")
  15. (lambda (a b)
  16. (* (proc-1 a b)
  17. (proc-2 a b)))))
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. (define (random-average-procedure n)
  20. (display "(avg ")
  21. (let ((proc-1 (random-procedure (- n 1)))
  22. (proc-2 (random-procedure (- n 1))))
  23. (display ")")
  24. (lambda (a b)
  25. (/ (+ (proc-1 a b)
  26. (proc-2 a b))
  27. 2.0))))
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. (define (random-sin-pi*-procedure n)
  30. (display "(sin (* pi ")
  31. (let ((proc (random-procedure (- n 1))))
  32. (display "))")
  33. (lambda (a b)
  34. (sin-pi* (proc a b)))))
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. (define (random-cos-pi*-procedure n)
  37. (display "(cos (* pi ")
  38. (let ((proc (random-procedure (- n 1))))
  39. (display "))")
  40. (lambda (a b)
  41. (cos-pi* (proc a b)))))
  42. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  43. (define (random-x-y-procedure n)
  44. (let ((arg (random-integer 2)))
  45. (display (if (= arg 0) "x " "y "))
  46. (lambda (a b)
  47. (vector-ref (vector a b) arg))))
  48. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  49. (define (random-procedure n)
  50. (cond ((= n 1)
  51. (random-x-y-procedure #f))
  52. (else
  53. (let ((procedure (vector-ref-random (vector random-product-procedure
  54. random-average-procedure
  55. random-sin-pi*-procedure
  56. random-cos-pi*-procedure))))
  57. (procedure n)))))
  58. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  59. (display "(lambda (x y) ")
  60. (define fun (random-procedure 7))
  61. (display ")\n")
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63. (glutInit (vector 0) (vector ""))
  64. (glutInitWindowPosition 100 100)
  65. (glutInitWindowSize 500 500)
  66. (glutCreateWindow "Random Art")
  67. (glutReshapeFunc (ortho-2d -1.0 1.0 -1.0 1.0))
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69. (glutDisplayFunc
  70. (lambda ()
  71. (glMatrixMode GL_MODELVIEW)
  72. (glLoadIdentity)
  73. (glClearColor 0.0 0.0 0.0 0.0)
  74. (glClear GL_COLOR_BUFFER_BIT)
  75. (glBegin GL_POINTS)
  76. (do ((y -1.0 (+ y 0.01)))
  77. ((> y 1.0))
  78. (do ((x -1.0 (+ x 0.01)))
  79. ((> x 1.0))
  80. (let ((val (fun x y)))
  81. (let ((grey (/ (+ val 1.0) 2.0)))
  82. (glColor4d grey grey grey 1.0)
  83. (glVertex2d x y)))))
  84. (glEnd)
  85. (glFlush)
  86. ))
  87. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  88. (glutKeyboardFunc
  89. (lambda (key x y)
  90. (exit)))
  91. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  92. (glutMainLoop)