Skip to content

Commit

Permalink
Refactor some SVG functions to new namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
samcf committed Aug 2, 2024
1 parent ec61390 commit 268a735
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/main/ogres/app/component/scene.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
26 changes: 1 addition & 25 deletions src/main/ogres/app/geom.cljs
Original file line number Diff line number Diff line change
@@ -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]."
Expand Down Expand Up @@ -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 [...]]."
Expand Down
31 changes: 31 additions & 0 deletions src/main/ogres/app/svg.cljs
Original file line number Diff line number Diff line change
@@ -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))))

0 comments on commit 268a735

Please sign in to comment.