5c1a3615e9fc
Mini 3: Map(-range)
author | Steve Losh <steve@stevelosh.com> |
---|---|
date | Tue, 12 Apr 2016 22:05:57 +0000 |
parents | 2f82e9ecb18e |
children | 4e226f02861b |
branches/tags | (none) |
files | package.lisp src/main.lisp src/math.lisp |
Changes
--- a/package.lisp Sun Apr 10 23:13:35 2016 +0000 +++ b/package.lisp Tue Apr 12 22:05:57 2016 +0000 @@ -7,4 +7,6 @@ (:use #:cl #:sketch #:coding-math.quickutils - #:coding-math.utils)) + #:coding-math.utils) + (:shadow + #:normalize))
--- a/src/main.lisp Sun Apr 10 23:13:35 2016 +0000 +++ b/src/main.lisp Tue Apr 12 22:05:57 2016 +0000 @@ -47,7 +47,7 @@ (rotate (degrees angle)) (when thrustingp (with-pen (make-pen :fill (rgb 1.0 0.0 0.0)) - (ngon 3 -15 0 10 6))) ; fire + (ngon 3 -15 0 10 6))) ; fire (with-pen (make-pen :stroke (gray 0) :fill (gray 0.5)) (rect -10 -3 10 6) ; engine (ngon 3 0 0 10 10) ; hull @@ -60,22 +60,13 @@ ((mx 0) (my 0) (frame 1) - (sun (make-particle center-x center-y - :mass 2000.0)) - (planet (make-particle (+ center-x 200) center-y - :speed 3.0 - :direction (- (/ tau 4)) - )) ) (background (gray 1)) (incf frame) ;; - (particle-gravitate-to! planet sun) - (particle-update! planet) - (with-pen (make-pen :stroke (gray 0) :fill (rgb 1.0 1.0 0.0)) - (circle (particle-x sun) (particle-y sun) 50)) (with-pen (make-pen :stroke (gray 0) :fill (rgb 0.0 1.0 0.0)) - (circle (particle-x planet) (particle-y planet) 10)) + (circle center-x center-y + (map-range 0 *height* 5 100 my))) ;; (when (zerop (mod frame 20)) (calc-fps 20)) @@ -83,7 +74,7 @@ ;;;; Mouse -(defmethod mousemotion-event ((window cm) ts b x y xrel yrel) +(defmethod kit.sdl2:mousemotion-event ((window cm) ts b x y xrel yrel) (declare (ignore ts b xrel yrel)) (with-slots (mx my) window (setf mx x)
--- a/src/math.lisp Sun Apr 10 23:13:35 2016 +0000 +++ b/src/math.lisp Tue Apr 12 22:05:57 2016 +0000 @@ -1,14 +1,35 @@ (in-package #:coding-math) +;; Constants (defconstant tau (* pi 2)) -(defun normalize (n min max) - (/ (- n min) +;; Number range mapping +(defun normalize (min max val) + (/ (- val min) (- max min))) +(defun lerp (from to n) + "Lerp together `from` and `to` by factor `n`. -(defun lerp (from to n) + Note that you might want `precise-lerp` instead. + + " (+ from (* n (- to from)))) +(defun precise-lerp (from to n) + "Lerp together `from` and `to` by factor `n`, precisely. + + Vanilla lerp does not guarantee `(lerp from to 0.0)` will return exactly + `from` due to floating-point errors. This version will return exactly `from` + when given a `n` of `0.0`, at the cost of an extra multiplication. + + " + (+ (* (- 1 n) from) + (* n to))) + +(defun map-range (source-from source-to dest-from dest-to source-val) + "Map `source-val` from the source range to the destination range." + (lerp dest-from dest-to + (normalize source-from source-to source-val)))