This commit is contained in:
Simon Brooke 2020-05-30 23:46:55 +01:00
parent 75899f8a4d
commit 989a8fe91d
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
2 changed files with 20 additions and 10 deletions

View file

@ -25,13 +25,12 @@
(defn path (defn path
"Return a path constructed from these `vertices`." "Return a path constructed from these `vertices`."
[& vertices] [& vertices]
(if (when-not (every? vertex? vertices)
(every? vertex? vertices)
{:vertices vertices :walkmap.id/id (keyword (gensym "path")) :kind :path}
(throw (IllegalArgumentException. (throw (IllegalArgumentException.
(str (str
"Each item on path must be a vertex: " "Each item on path must be a vertex: "
(s/join " " (map kind-type vertices))))))) (s/join " " (map kind-type (remove vertex? vertices)))))))
{:vertices vertices :walkmap.id/id (keyword (gensym "path")) :kind :path})
(defn polygon->path (defn polygon->path
"If `o` is a polygon, return an equivalent path. What's different about "If `o` is a polygon, return an equivalent path. What's different about
@ -41,10 +40,12 @@
If `o` is not a polygon, will throw an exception." If `o` is not a polygon, will throw an exception."
[o] [o]
(if (when-not (polygon? o)
(polygon? o) (throw (IllegalArgumentException. (str "Not a polygon: " (kind-type o)))))
(assoc (dissoc o :vertices) :kind :path :vertices (concat (:vertices o) (list (first (:vertices o))))) (assoc (dissoc o :vertices)
(throw (IllegalArgumentException. "Not a polygon!")))) :kind :path
;; `concat` rather than `conj` because order matters.
:vertices (concat (:vertices o) (list (first (:vertices o))))))
(defn path->edges (defn path->edges
"if `o` is a path, a polygon, or a sequence of vertices, return a sequence of "if `o` is a path, a polygon, or a sequence of vertices, return a sequence of

View file

@ -1,6 +1,8 @@
(ns walkmap.polygon (ns walkmap.polygon
"Essentially the specification for things we shall consider to be polygons." "Essentially the specification for things we shall consider to be polygons."
(:require [walkmap.vertex :refer [vertex?]])) (:require [clojure.string :as s]
[walkmap.utils :refer [kind-type]]
[walkmap.vertex :refer [vertex?]]))
(defn polygon? (defn polygon?
"True if `o` satisfies the conditions for a polygon. A polygon shall be a "True if `o` satisfies the conditions for a polygon. A polygon shall be a
@ -16,4 +18,11 @@
(:walkmap.id/id o) (:walkmap.id/id o)
(or (nil? (:kind o)) (= (:kind o) :polygon))))) (or (nil? (:kind o)) (= (:kind o) :polygon)))))
(defn polygon
[vertices]
(when-not (every? vertex? vertices)
(throw (IllegalArgumentException.
(str
"Each item on path must be a vertex: "
(s/join " " (map kind-type (remove vertex? vertices)))))))
{:vertices vertices :walkmap.id/id (keyword (gensym "poly")) :kind :polygon})