math: grid: A couple more optimizations.
authorDavid Thompson <dthompson2@worcester.edu>
Sun, 2 Dec 2018 23:34:10 +0000 (18:34 -0500)
committerDavid Thompson <dthompson2@worcester.edu>
Sun, 2 Dec 2018 23:35:55 +0000 (18:35 -0500)
* chickadee/math/grid.scm (make-grid): Set a high minimum bucket size
to avoid rehashing.
(grid-move): Use memoized inexact->exact procedure. Fix return value
of check procedure being ignored.

chickadee/math/grid.scm

index 58232fc..9982f90 100644 (file)
@@ -114,7 +114,7 @@ needed to make the rectangles no longer overlap."
 (define* (make-grid #:optional (cell-size 64.0))
   "Create new grid partitioned by CELL-SIZE."
   (%make-grid (f32vector cell-size)
-              (make-hash-table)
+              (make-hash-table 1000)
               (make-hash-table)
               (make-rect 0.0 0.0 0.0 0.0)
               (make-array-list)
@@ -238,7 +238,7 @@ POSITION may be modified to resolve the colliding objects."
          (collisions (grid-buffer grid))
          (visited (grid-visited grid)))
     (define (to-cell n)
-      (inexact->exact (floor (/ n cell-size))))
+      (inexact->exact* (floor (/ n cell-size))))
     (define (collision? rect1 rect2 goal)
       (let ((goal-x (vec2-x goal))
             (goal-y (vec2-y goal)))
@@ -321,8 +321,7 @@ POSITION may be modified to resolve the colliding objects."
                 (when (<= cx maxx)
                   (let ((cell (row-column-ref row cx)))
                     (hash-for-each (lambda (other unused)
-                                     (check other (grid-rect-ref grid other))
-                                     #f)
+                                     (check other (grid-rect-ref grid other)))
                                    (cell-items cell)))
                   (xloop (+ cx 1)))))
             (yloop (+ cy 1))))