From 268a735c289a56f0159a7db6c5b30b8c86c3d39a Mon Sep 17 00:00:00 2001 From: Sam Ferrell Date: Thu, 1 Aug 2024 19:09:16 -0600 Subject: [PATCH] Refactor some SVG functions to new namespace. --- src/main/ogres/app/component/scene.cljs | 3 ++- src/main/ogres/app/geom.cljs | 26 +-------------------- src/main/ogres/app/svg.cljs | 31 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 src/main/ogres/app/svg.cljs diff --git a/src/main/ogres/app/component/scene.cljs b/src/main/ogres/app/component/scene.cljs index d935ed6..9494d78 100644 --- a/src/main/ogres/app/component/scene.cljs +++ b/src/main/ogres/app/component/scene.cljs @@ -7,8 +7,9 @@ [ogres.app.component.scene-draw :refer [draw]] [ogres.app.component.scene-pattern :refer [pattern]] [ogres.app.const :refer [grid-size]] - [ogres.app.geom :refer [bounding-rect shape-bounding-rect chebyshev-distance circle->path poly->path cone-points point-within-rect]] + [ogres.app.geom :refer [bounding-rect shape-bounding-rect chebyshev-distance cone-points point-within-rect]] [ogres.app.hooks :refer [create-portal use-subscribe use-dispatch use-portal use-query]] + [ogres.app.svg :refer [circle->path poly->path]] [ogres.app.util :refer [key-by round-grid]] [react-transition-group :refer [TransitionGroup CSSTransition Transition]] [uix.core :as uix :refer [defui $ create-ref use-callback use-effect use-memo use-state]] diff --git a/src/main/ogres/app/geom.cljs b/src/main/ogres/app/geom.cljs index 334b90a..f1d9c23 100644 --- a/src/main/ogres/app/geom.cljs +++ b/src/main/ogres/app/geom.cljs @@ -1,16 +1,4 @@ -(ns ogres.app.geom - (:require [clojure.string :refer [join]])) - -(def ^:private poly-path-xf - (comp (partition-all 2) (mapcat (fn [[x y]] [x y \L])))) - -(defn ^:private circle-path - [[x y r]] - (let [d (* r 2)] - [\M x y - \m r 0 - \a r r 0 1 0 (- d) 0 - \a r r 0 1 0 d 0 \z])) +(ns ogres.app.geom) (defn euclidean-distance "Returns the euclidean distance from [Ax Ay] to [Bx By]." @@ -54,18 +42,6 @@ (recur (rest (rest points)) (min min-x x) (min min-y y) (max max-x x) (max max-y y))) [min-x min-y max-x max-y]))) -(defn poly->path - ([] []) - ([path] (join " " path)) - ([path points] - (into path (conj (pop (into [\M] poly-path-xf points)) \z)))) - -(defn circle->path - ([] []) - ([path] (join " " path)) - ([path circle] - (into path (circle-path circle)))) - (defn ^:private clockwise? "Returns true if the given polygon has a clockwise winding order, false otherwise. Points must be given in the form of [Ax Ay Bx By [...]]." diff --git a/src/main/ogres/app/svg.cljs b/src/main/ogres/app/svg.cljs new file mode 100644 index 0000000..7f8d221 --- /dev/null +++ b/src/main/ogres/app/svg.cljs @@ -0,0 +1,31 @@ +(ns ogres.app.svg + (:require [clojure.string :refer [join]])) + +(def ^:private poly-path-xf + (comp (partition-all 2) (mapcat (fn [[x y]] [x y \L])))) + +(defn ^:private circle-path + [[x y r]] + (let [d (* r 2)] + [\M x y + \m r 0 + \a r r 0 1 0 (- d) 0 + \a r r 0 1 0 d 0 \z])) + +(defn poly->path + "Reducing function which returns an SVG path string from a collection + of polygons given as [Ax Ay Bx By Cx Cy ...]." + ([] []) + ([path] + (join " " path)) + ([path points] + (into path (conj (pop (into [\M] poly-path-xf points)) \z)))) + +(defn circle->path + "Reducing function which returns an SVG path string from a collection + of circles given as [Cx Cy Radius]." + ([] []) + ([path] + (join " " path)) + ([path circle] + (into path (circle-path circle))))