69f98eb3a1df

Mini 5: Pythagorean Theorem and Distance
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 22 Apr 2016 23:13:31 +0000
parents 01019d2d2be8
children 67072984548b
branches/tags (none)
files package.lisp src/main.lisp src/math.lisp src/utils.lisp src/vectors.lisp

Changes

--- a/package.lisp	Fri Apr 22 23:12:34 2016 +0000
+++ b/package.lisp	Fri Apr 22 23:13:31 2016 +0000
@@ -7,7 +7,8 @@
    #:a
    #:in-context
    #:mulf
-   #:dividesp))
+   #:dividesp
+   #:square))
 
 (defpackage #:coding-math.math
   (:use
@@ -16,6 +17,7 @@
    #:coding-math.utils)
   (:export
    #:tau
+   #:distance
    #:random-range
    #:random-around
    #:norm
@@ -30,6 +32,7 @@
 (defpackage #:coding-math.vectors
   (:use
    #:cl
+   #:coding-math.math
    #:coding-math.quickutils
    #:coding-math.utils)
   (:export
--- a/src/main.lisp	Fri Apr 22 23:12:34 2016 +0000
+++ b/src/main.lisp	Fri Apr 22 23:13:31 2016 +0000
@@ -40,11 +40,11 @@
   (background (gray 1))
   (incf frame)
   ;;
-  (when p
-    (particle-update! p)
-    (particle-wrap! p *width* *height*)
-    (with-pen (make-pen :stroke (gray 0.3) :fill (gray 0.8))
-      (draw-particle p)))
+  (with-pen (make-pen :stroke (gray 0.3)
+                      :fill (if (> (distance mx my *center-x* *center-y*) 100)
+                              (gray 1)
+                              (gray 0.5)))
+    (circle *center-x* *center-y* 100))
   ;;
   (when (zerop (mod frame 20))
     (calc-fps 20))
--- a/src/math.lisp	Fri Apr 22 23:12:34 2016 +0000
+++ b/src/math.lisp	Fri Apr 22 23:13:31 2016 +0000
@@ -3,6 +3,11 @@
 ;; Constants
 (defparameter tau (* pi 2))
 
+;; Geometry
+(defun distance (x0 y0 x1 y1)
+  (sqrt (+ (square (- x0 x1))
+           (square (- y0 y1)))))
+
 
 ;; Random
 (defun random-range (min max)
--- a/src/utils.lisp	Fri Apr 22 23:12:34 2016 +0000
+++ b/src/utils.lisp	Fri Apr 22 23:13:31 2016 +0000
@@ -4,6 +4,10 @@
   "Return whether `n` is evenly divisible by `divisor`."
   (zerop (mod n divisor)))
 
+(defun square (n)
+  "Return the square of `n`."
+  (* n n))
+
 
 (defmacro mulf (place n &environment env)
   "Multiply `place` by `n` in-place."
@@ -13,6 +17,7 @@
             (,(car stores) (* ,n ,access-expr)))
        ,store-expr)))
 
+
 (defun a (alist key) ; lol
   (cdr (assoc key alist)))
 
--- a/src/vectors.lisp	Fri Apr 22 23:12:34 2016 +0000
+++ b/src/vectors.lisp	Fri Apr 22 23:13:31 2016 +0000
@@ -84,3 +84,6 @@
   (format nil "[~A ~A]" (vec-x v) (vec-y v)))
 
 
+(defun vec-distance-between (v0 v1)
+  (distance (vec-x v0) (vec-y v0)
+            (vec-x v1) (vec-y v1)))