]> git.earman.xyz Git - lisp/workenv.git/commitdiff
Make average-of-points length agnostic
authorrearman <rearman@r717.net>
Mon, 13 Apr 2026 19:57:33 +0000 (15:57 -0400)
committerrearman <rearman@r717.net>
Mon, 13 Apr 2026 19:57:33 +0000 (15:57 -0400)
Drop cond for length of one, calculate average based on length of list of
points.

math.lisp

index 31f2ded6fa575a85bdc804c4c80d2bd8b735cd69..70099e8c486e90255ada577b2ab8921260f1a328 100644 (file)
--- a/math.lisp
+++ b/math.lisp
 If a 2d point is given, it is converted to 3d with z=0."
   (cond ((numberp (first points))
         points)
-       ((= (length points) 1)
-        (first points))
-       (t (mapcar #'/
-                  (list (apply #'+ (mapcar #'first points))
-                        (apply #'+ (mapcar #'second points))
-                        (apply #'+
-                               (mapcar #'(lambda (x)
-                                           (if (null (third x))
-                                               0
-                                               (third x)))
-                                       points)))
-                  '(2 2 2)))))
+       (t (let ((numpoints (length points)))
+            (mapcar #'/
+                    (list (apply #'+ (mapcar #'first points))
+                          (apply #'+ (mapcar #'second points))
+                          (apply #'+
+                                 (mapcar #'(lambda (x)
+                                             (if (null (third x))
+                                                 0
+                                                 (third x)))
+                                         points)))
+                    (list numpoints
+                          numpoints
+                          numpoints))))))
 
 (defun dot-product-points (apoint bpoint)
   "Return the dot-product of 3d vectors a and b.