0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-04 13:29:14 -05:00

Add the ability to invert matrix.

This commit is contained in:
Andrey Antukh 2016-08-11 18:12:37 +03:00
parent 0dff98801c
commit d9a5c06106
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

View file

@ -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)))))