From d9a5c06106555f9ff83fc7e4d06b54aca1fe0493 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 11 Aug 2016 18:12:37 +0300 Subject: [PATCH] Add the ability to invert matrix. --- src/uxbox/main/geom/matrix.cljs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/uxbox/main/geom/matrix.cljs b/src/uxbox/main/geom/matrix.cljs index d77945568..14175b6c4 100644 --- a/src/uxbox/main/geom/matrix.cljs +++ b/src/uxbox/main/geom/matrix.cljs @@ -123,3 +123,21 @@ (+ ty1 (* tx2 c1) (* ty2 d1))))) ([m om & others] (reduce append (append m om) others))) + +(defn ^boolean invertible? + [{:keys [a b c d tx ty] :as m}] + (let [det (- (* a d) (* c b))] + (and (not (mth/nan? det)) + (mth/finite? tx) + (mth/finite? ty)))) + +(defn invert + [{:keys [a b c d tx ty] :as m}] + (when (invertible? m) + (let [det (- (* a d) (* c b))] + (Matrix. (/ d det) + (/ (- b) det) + (/ (- c) det) + (/ a det) + (/ (- (* c ty) (* d tx)) det) + (/ (- (* b tx) (* a ty)) det)))))