Added: triangle?, gradient, vertex (vector) multiply.
This commit is contained in:
parent
989a8fe91d
commit
87177051da
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -16,3 +16,5 @@ pom.xml.asc
|
|||
resources/isle_of_man.svg
|
||||
|
||||
resources/small_hill.svg
|
||||
|
||||
s.edn
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
An edge is a line segment having just a start and an end, with no intervening
|
||||
nodes."
|
||||
(:require [clojure.math.numeric-tower :as m]
|
||||
[walkmap.polygon :refer [polygon?]]
|
||||
[walkmap.vertex :refer [ensure2d ensure3d vertex vertex= vertex?]]))
|
||||
|
||||
(defn edge
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
(ns walkmap.polygon
|
||||
"Essentially the specification for things we shall consider to be polygons."
|
||||
(:require [clojure.string :as s]
|
||||
[walkmap.edge :as e]
|
||||
[walkmap.utils :refer [kind-type]]
|
||||
[walkmap.vertex :refer [vertex?]]))
|
||||
|
||||
|
@ -18,6 +19,12 @@
|
|||
(:walkmap.id/id o)
|
||||
(or (nil? (:kind o)) (= (:kind o) :polygon)))))
|
||||
|
||||
(defn triangle?
|
||||
[o]
|
||||
(and
|
||||
(coll? o)
|
||||
(= (count (:vertices o)) 3)))
|
||||
|
||||
(defn polygon
|
||||
[vertices]
|
||||
(when-not (every? vertex? vertices)
|
||||
|
@ -26,3 +33,15 @@
|
|||
"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})
|
||||
|
||||
(defn gradient
|
||||
"Return a unit vector representing the gradient across `triangle`."
|
||||
[triangle]
|
||||
(when-not (triangle? triangle)
|
||||
(throw (IllegalArgumentException.
|
||||
(s/join " " ["Must be a triangle:" (kind-type triangle)]))))
|
||||
(let [order (sort #(max (:z %1) (:z %2)) (:vertices triangle))
|
||||
highest (first order)
|
||||
lowest (last order)]
|
||||
(e/unit-vector (e/edge lowest highest))))
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
two vertices, create an edge from them and use `walkmap.edge/length`."
|
||||
(:require [clojure.math.numeric-tower :as m]
|
||||
[clojure.string :as s]
|
||||
[taoensso.timbre :as l]
|
||||
[walkmap.geometry :refer [=ish]]
|
||||
[walkmap.utils :refer [truncate]]))
|
||||
|
||||
|
@ -55,6 +56,25 @@
|
|||
#(=ish (% v1) (% v2))
|
||||
[:x :y :z]))
|
||||
|
||||
(defn vertex*
|
||||
"Return a vertex like `v1`, but with each of its coordinates multiplied
|
||||
by the equivalent vertex in `v2`."
|
||||
[v1 v2]
|
||||
(if
|
||||
(and (vertex? v1) (vertex? v2))
|
||||
(let [f (fn [v1 v2 coord]
|
||||
(* (or (coord v1) 0)
|
||||
;; one here is deliberate!
|
||||
(or (coord v2) 1)))]
|
||||
(assoc v1 :x (f v1 v2 :x)
|
||||
:y (f v1 v2 :y)
|
||||
:z (f v1 v2 :z)))
|
||||
(do (l/warn
|
||||
(s/join
|
||||
" "
|
||||
["in `vertex-multiply`, both must be vectors. v1:" v1 "v2:" v2]))
|
||||
v1)))
|
||||
|
||||
(defn vertex
|
||||
"Make a vertex with this `x`, `y` and (if provided) `z` values. Returns a map
|
||||
with those values, plus a unique `:walkmap.id/id` value, and `:kind` set to `:vertex`.
|
||||
|
|
Loading…
Reference in a new issue