Mini 9: Random Distribution
author |
Steve Losh <steve@stevelosh.com> |
date |
Wed, 04 May 2016 20:05:07 +0000 |
parents |
7e868ce7595b
|
children |
7a40282385de
|
branches/tags |
(none) |
files |
package.lisp src/main.lisp src/math.lisp |
Changes
--- a/package.lisp Tue May 03 23:59:09 2016 +0000
+++ b/package.lisp Wed May 04 20:05:07 2016 +0000
@@ -24,6 +24,7 @@
#:distance
#:random-range
#:random-around
+ #:random-dist
#:norm
#:lerp
#:precise-lerp
--- a/src/main.lisp Tue May 03 23:59:09 2016 +0000
+++ b/src/main.lisp Wed May 04 20:05:07 2016 +0000
@@ -14,43 +14,39 @@
(circle (particle-x p) (particle-y p) (particle-radius p))))
+(defun add-result (results)
+ (incf (aref results (floor (random-dist 0 100 4)))))
+
(defsketch cm (:width *width*
:height *height*
:debug :scancode-d)
((ready)
(mouse)
- (sp)
- (p)
- (earth)
- (sun)
- (emitter)
- (ps)
- (particle-pen (make-pen :stroke (gray 0.5) :fill (gray 0.8)))
- (sp-pen (make-pen :stroke (gray 0.0) :fill (gray 0.0)))
- (rope-pen (make-pen :stroke (gray 0.0)))
- (sun-pen (make-pen :fill (rgb 1.0 1.0 0.0) :stroke (gray 0)))
- (earth-pen (make-pen :fill (rgb 0.0 1.0 0.0) :stroke (gray 0)))
- (p-pen (make-pen :fill (rgb 1.0 0.0 0.0)))
+ (graph-pen (make-pen :fill (gray 0.8)))
+ (dot-pen (make-pen :fill (gray 0.8)))
+ (results)
+ (dots)
)
(with-fps
(background (gray 1))
;;
(when ready
- (particle-update! p)
- (mapcar #'particle-update! ps)
- (draw-particle sun sun-pen)
- (draw-particle earth earth-pen)
- (draw-particle emitter sp-pen)
- (loop :for p :in ps :do (draw-particle p p-pen))
-
- (draw-particle p particle-pen)
- (with-pen rope-pen
- (line (particle-x p) (particle-y p) (vec-x sp) (vec-y sp))
- (line (particle-x p) (particle-y p) (vec-x mouse) (vec-y mouse)))
- (with-pen sp-pen
- (circle (vec-x sp) (vec-y sp) 3)
- (circle (vec-x mouse) (vec-y mouse) 3))
+ (with-pen dot-pen
+ (loop :for (x . y) :in dots
+ :do (circle x y 2)))
+ (add-result results)
+ (with-pen graph-pen
+ (loop :for r :across results
+ :for i :from 0
+ :do (rect (map-range 0 100
+ 0 *width*
+ i)
+ 0
+ (- (/ *width* 100) 1)
+ (map-range 0 200
+ 0 *height*
+ r))))
)
@@ -64,41 +60,12 @@
(defun reset (game)
(setf (slot-value game 'ready) nil)
- (setf (slot-value game 'sp)
- (make-vec (random *width*)
- (random *height*))
- (slot-value game 'p)
- (make-particle (random *width*)
- (random *height*)
- :radius 10
- :friction (random-range 0.1 0.2)
- :gravity (random-range 0.1 0.5))
- (slot-value game 'sun) (make-particle *center-x* *center-y*
- :mass 500.0
- :radius 20)
- (slot-value game 'earth) (make-particle 320 80
- :mass 100.0
- :radius 8)
- (slot-value game 'emitter) (make-particle 100 100 :radius 5)
- (slot-value game 'ps) nil
- )
- (particle-spring-add! (slot-value game 'p)
- (slot-value game 'mouse)
- 0.05
- 80)
- (particle-spring-add! (slot-value game 'p)
- (slot-value game 'sp)
- 0.05
- 80)
- (loop :repeat 300
- :for p = (make-particle 100 100
- :speed (random 3.0)
- :direction (random tau)
- :radius 3)
- :do
- (push p (slot-value game 'ps))
- (particle-gravitate-add! p (slot-value game 'sun))
- (particle-gravitate-add! p (slot-value game 'earth)))
+ (setf (slot-value game 'results)
+ (make-array 100 :initial-element 0)
+ (slot-value game 'dots)
+ (loop :repeat 500
+ :collect (cons (random-dist 0 *width* 4)
+ (random-dist 0 *height* 4))))
(setf (slot-value game 'ready) t))
--- a/src/math.lisp Tue May 03 23:59:09 2016 +0000
+++ b/src/math.lisp Wed May 04 20:05:07 2016 +0000
@@ -21,6 +21,11 @@
(random-range (- val range)
(+ val range)))
+(defun random-dist (min max iterations)
+ (loop :repeat iterations
+ :summing (random-range min max) :into total
+ :finally (return (/ total iterations))))
+
;;;; Number range mapping
(defun norm (min max val)