Implement naive rect packing :)

This commit is contained in:
heyarne 2021-12-30 16:55:14 +01:00
commit 081809d4cd

View file

@ -24,13 +24,27 @@
starting at the top left." starting at the top left."
[frame rects] [frame rects]
(let [rects (sort-by (comp - geom/height) rects)] (let [rects (sort-by (comp - geom/height) rects)]
(reduce (fn [result rect] (-> (reduce (fn [acc rect]
(let [last-placed (last result)] ;; a word on "top" vs "bottom": the thi.ng coordinate system
(conj result (geom/translate rect [(if last-placed ;; works like the ones you know from scool, i.e. [0 1] is
(rect/right last-placed) ;; above [0 0]. the svg coordinate system uses screen
0) ;; coordinates, where [0 0] is at the top left. if you see
0])))) ;; rect/bottom we're actually looking at the top edge that is
[] rects))) ;; drawn in the svg.
(let [last-placed (or (last (:result acc)) (rect/rect 0 0))
moved-right (geom/translate rect [(rect/right last-placed) (rect/bottom (:row-start acc))])]
;; do we still have enough space to the right?
(if (<= (rect/right moved-right) (rect/right frame))
;; if yes, everything is bon
(update acc :result conj moved-right)
;; if no, move to the bottom
(let [moved-bottom (geom/translate rect [0 (rect/top (:row-start acc))])]
(-> (assoc acc :row-start moved-bottom)
(update :result conj moved-bottom))))))
{:row-start (first rects)
:result []}
rects)
:result)))
#_(pack-rects-naive (:frame @state) (:rects @state)) #_(pack-rects-naive (:frame @state) (:rects @state))