mirror of
https://github.com/penpot/penpot.git
synced 2025-06-13 03:01:39 +02:00
⚡ Performance improvements.
This commit is contained in:
parent
3ab5e11d5f
commit
76e19a4b41
9 changed files with 134 additions and 349 deletions
|
@ -2,8 +2,10 @@
|
|||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
|
||||
;; Copyright (c) 2015-2017 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
;; defined by the Mozilla Public License, v. 2.0.
|
||||
;;
|
||||
;; Copyright (c) 2015-2020 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.util.geom.point
|
||||
(:refer-clojure :exclude [divide])
|
||||
|
@ -27,13 +29,6 @@
|
|||
(point? v)
|
||||
v
|
||||
|
||||
(or (vector? v)
|
||||
(seq? v))
|
||||
(Point. (first v) (second v))
|
||||
|
||||
(map? v)
|
||||
(Point. (:x v) (:y v))
|
||||
|
||||
(number? v)
|
||||
(Point. v v)
|
||||
|
||||
|
@ -41,105 +36,87 @@
|
|||
(throw (ex-info "Invalid arguments" {:v v}))))
|
||||
([x y] (Point. x y)))
|
||||
|
||||
(defn rotate
|
||||
"Apply rotation transformation to the point."
|
||||
[p angle]
|
||||
{:pre [(point? p)]}
|
||||
(let [angle (mth/radians angle)
|
||||
sin (mth/sin angle)
|
||||
cos (mth/cos angle)]
|
||||
(Point.
|
||||
(-> (- (* (:x p) cos) (* (:y p) sin))
|
||||
(mth/precision 6))
|
||||
(-> (+ (* (:x p) sin) (* (:y p) cos))
|
||||
(mth/precision 6)))))
|
||||
|
||||
(defn add
|
||||
"Returns the addition of the supplied value to both
|
||||
coordinates of the point as a new point."
|
||||
[p other]
|
||||
{:pre [(point? p)]}
|
||||
(let [other (point other)]
|
||||
(Point. (+ (:x p) (:x other))
|
||||
(+ (:y p) (:y other)))))
|
||||
[{x :x y :y :as p} {ox :x oy :y :as other}]
|
||||
(assert (point? p))
|
||||
(assert (point? other))
|
||||
(Point. (+ x ox) (+ y oy)))
|
||||
|
||||
(defn subtract
|
||||
"Returns the subtraction of the supplied value to both
|
||||
coordinates of the point as a new point."
|
||||
[p other]
|
||||
{:pre [(point? p)]}
|
||||
(let [other (point other)]
|
||||
(Point. (- (:x p) (:x other))
|
||||
(- (:y p) (:y other)))))
|
||||
|
||||
[{x :x y :y :as p} {ox :x oy :y :as other}]
|
||||
(assert (point? p))
|
||||
(assert (point? other))
|
||||
(Point. (- x ox) (- y oy)))
|
||||
|
||||
(defn multiply
|
||||
"Returns the subtraction of the supplied value to both
|
||||
coordinates of the point as a new point."
|
||||
[p other]
|
||||
{:pre [(point? p)]}
|
||||
(let [other (point other)]
|
||||
(Point. (* (:x p) (:x other))
|
||||
(* (:y p) (:y other)))))
|
||||
[{x :x y :y :as p} {ox :x oy :y :as other}]
|
||||
(assert (point? p))
|
||||
(assert (point? other))
|
||||
(Point. (* x ox) (* y oy)))
|
||||
|
||||
(defn divide
|
||||
[p other]
|
||||
{:pre [(point? p)]}
|
||||
(let [other (point other)]
|
||||
(Point. (/ (:x p) (:x other))
|
||||
(/ (:y p) (:y other)))))
|
||||
[{x :x y :y :as p} {ox :x oy :y :as other}]
|
||||
(assert (point? p))
|
||||
(assert (point? other))
|
||||
(Point. (/ x ox) (/ y oy)))
|
||||
|
||||
(defn negate
|
||||
[p]
|
||||
{:pre [(point? p)]}
|
||||
(let [{:keys [x y]} (point p)]
|
||||
(Point. (- x) (- y))))
|
||||
[{x :x y :y :as p}]
|
||||
(assert (point? p))
|
||||
(Point. (- x) (- y)))
|
||||
|
||||
(defn distance
|
||||
"Calculate the distance between two points."
|
||||
[p other]
|
||||
(let [other (point other)
|
||||
dx (- (:x p) (:x other))
|
||||
dy (- (:y p) (:y other))]
|
||||
[{x :x y :y :as p} {ox :x oy :y :as other}]
|
||||
(assert (point? p))
|
||||
(assert (point? other))
|
||||
(let [dx (- x ox)
|
||||
dy (- y oy)]
|
||||
(-> (mth/sqrt (+ (mth/pow dx 2)
|
||||
(mth/pow dy 2)))
|
||||
(mth/precision 6))))
|
||||
|
||||
(defn length
|
||||
[p]
|
||||
{:pre [(point? p)]}
|
||||
(mth/sqrt (+ (mth/pow (:x p) 2)
|
||||
(mth/pow (:y p) 2))))
|
||||
[{x :x y :y :as p}]
|
||||
(assert (point? p))
|
||||
(mth/sqrt (+ (mth/pow x 2)
|
||||
(mth/pow y 2))))
|
||||
|
||||
(defn angle
|
||||
"Returns the smaller angle between two vectors.
|
||||
If the second vector is not provided, the angle
|
||||
will be measured from x-axis."
|
||||
([p]
|
||||
(-> (mth/atan2 (:y p) (:x p))
|
||||
([{x :x y :y :as p}]
|
||||
(-> (mth/atan2 y x)
|
||||
(mth/degrees)))
|
||||
([p center]
|
||||
(let [center (point center)]
|
||||
(angle (subtract p center)))))
|
||||
(angle (subtract p center))))
|
||||
|
||||
(defn angle-with-other
|
||||
"Consider point as vector and calculate
|
||||
the angle between two vectors."
|
||||
[p other]
|
||||
{:pre [(point? p)]}
|
||||
(let [other (point other)
|
||||
a (/ (+ (* (:x p) (:x other))
|
||||
(* (:y p) (:y other)))
|
||||
(* (length p) (length other)))
|
||||
a (mth/acos (if (< a -1)
|
||||
-1
|
||||
(if (> a 1) 1 a)))]
|
||||
[{x :x y :y :as p} {ox :x oy :y :as other}]
|
||||
(assert (point? p))
|
||||
(assert (point? other))
|
||||
(let [a (/ (+ (* x ox)
|
||||
(* y oy))
|
||||
(* (length p)
|
||||
(length other)))
|
||||
a (mth/acos (if (< a -1) -1 (if (> a 1) 1 a)))]
|
||||
(-> (mth/degrees a)
|
||||
(mth/precision 6))))
|
||||
|
||||
(defn update-angle
|
||||
"Update the angle of the point."
|
||||
[p angle]
|
||||
(assert (point? p))
|
||||
(assert (number? angle))
|
||||
(let [len (length p)
|
||||
angle (mth/radians angle)]
|
||||
(Point. (* (mth/cos angle) len)
|
||||
|
@ -148,24 +125,25 @@
|
|||
(defn quadrant
|
||||
"Return the quadrant of the angle of the point."
|
||||
[{:keys [x y] :as p}]
|
||||
{:pre [(point? p)]}
|
||||
(assert (point? p))
|
||||
(if (>= x 0)
|
||||
(if (>= y 0) 1 4)
|
||||
(if (>= y 0) 2 3)))
|
||||
|
||||
(defn round
|
||||
"Change the precision of the point coordinates."
|
||||
[{:keys [x y]} decimanls]
|
||||
[{:keys [x y] :as p} decimanls]
|
||||
(assert (point? p))
|
||||
(assert (number? decimanls))
|
||||
(Point. (mth/precision x decimanls)
|
||||
(mth/precision y decimanls)))
|
||||
|
||||
(defn transform
|
||||
"Transform a point applying a matrix transfomation."
|
||||
[pt {:keys [a b c d e f] :as m}]
|
||||
(let [{:keys [x y]} (point pt)]
|
||||
(Point. (+ (* x a) (* y c) e)
|
||||
(+ (* x b) (* y d) f))))
|
||||
|
||||
[{:keys [x y] :as p} {:keys [a b c d e f] :as m}]
|
||||
(assert (point? p))
|
||||
(Point. (+ (* x a) (* y c) e)
|
||||
(+ (* x b) (* y d) f)))
|
||||
|
||||
;; --- Transit Adapter
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue