Replace command->point with segment->point helper

This commit is contained in:
Andrey Antukh 2025-04-08 22:09:33 +02:00
parent db73c2eea0
commit 9ba0ae5532
4 changed files with 23 additions and 43 deletions

View File

@ -52,7 +52,7 @@
(assoc :prev first)
(some? prev)
(assoc :prev (helpers/command->point prev))))))))
(assoc :prev (helpers/segment->point prev))))))))
(defn close-paths
"Removes the :close-path commands and replace them for line-to so we can calculate
@ -65,7 +65,7 @@
last-point nil]
(if-let [segment (first segments)]
(let [point
(helpers/command->point segment)
(helpers/segment->point segment)
segment
(cond
@ -259,7 +259,7 @@
result)]
(recur (first content)
(rest content)
(helpers/command->point current)
(helpers/segment->point current)
(conj result (dissoc current :prev)))))))
(defn remove-duplicated-segments

View File

@ -95,26 +95,6 @@
(gpt/angle->point from-point (mth/radians to-angle) distance))
point))
;; FIXME: looks very similar to get-point
(defn command->point
([command]
(command->point command nil))
([command coord]
(let [params (:params command)
xkey (case coord
:c1 :c1x
:c2 :c2x
:x)
ykey (case coord
:c1 :c1y
:c2 :c2y
:y)
x (get params xkey)
y (get params ykey)]
(when (and (some? x) (some? y))
(gpt/point x y)))))
(defn segment->point
([segment] (segment->point segment :x))
([segment coord]
@ -131,14 +111,14 @@
([segment]
(command->line segment (:prev segment)))
([segment prev]
[prev (command->point segment)]))
[prev (segment->point segment)]))
(defn command->bezier
([segment]
(command->bezier segment (:prev segment)))
([segment prev]
[prev
(command->point segment)
(segment->point segment)
(gpt/point (-> segment :params :c1x) (-> segment :params :c1y))
(gpt/point (-> segment :params :c2x) (-> segment :params :c2y))]))
@ -151,17 +131,17 @@
([command prev-point]
(let [points (case (:command command)
:move-to [(command->point command)]
:move-to [(segment->point command)]
;; If it's a line we add the beginning point and endpoint
:line-to [prev-point (command->point command)]
:line-to [prev-point (segment->point command)]
;; We return the bezier extremities
:curve-to (into [prev-point (command->point command)]
:curve-to (into [prev-point (segment->point command)]
(let [curve [prev-point
(command->point command)
(command->point command :c1)
(command->point command :c2)]]
(segment->point command)
(segment->point command :c1)
(segment->point command :c2)]]
(->> (curve-extremities curve)
(mapv #(curve-values curve %)))))
[])]
@ -448,7 +428,7 @@
"Given a point and a line-to command will create a two new line-to commands
that will split the original line into two given a value between 0-1"
[from-p segment t-val]
(let [to-p (command->point segment)
(let [to-p (segment->point segment)
sp (gpt/lerp from-p to-p t-val)]
[(make-line-to sp) segment]))
@ -488,7 +468,7 @@
(let [values (->> values (filter #(and (> % 0) (< % 1))))]
(if (empty? values)
[segment]
(let [to-p (command->point segment)
(let [to-p (segment->point segment)
values-set (->> (conj values 1) (into (sorted-set)))]
(->> values-set
(mapv (fn [val]
@ -505,7 +485,7 @@
(let [values (->> values (filter #(and (> % 0) (< % 1))))]
(if (empty? values)
[segment]
(let [to-p (command->point segment)
(let [to-p (segment->point segment)
params (:params segment)
h1 (gpt/point (:c1x params) (:c1y params))
h2 (gpt/point (:c2x params) (:c2y params))

View File

@ -295,8 +295,8 @@
(let [point+distance
(fn [[cur-cmd prev-cmd]]
(let [from-p (helpers/command->point prev-cmd)
to-p (helpers/command->point cur-cmd)
(let [from-p (helpers/segment->point prev-cmd)
to-p (helpers/segment->point cur-cmd)
h1 (gpt/point (get-in cur-cmd [:params :c1x])
(get-in cur-cmd [:params :c1y]))
h2 (gpt/point (get-in cur-cmd [:params :c2x])
@ -332,8 +332,8 @@
(let [point+distance
(fn [[cur-cmd prev-cmd]]
(let [from-p (helpers/command->point prev-cmd)
to-p (helpers/command->point cur-cmd)
(let [from-p (helpers/segment->point prev-cmd)
to-p (helpers/segment->point cur-cmd)
h1 (gpt/point (get-in cur-cmd [:params :c1x])
(get-in cur-cmd [:params :c1y]))
h2 (gpt/point (get-in cur-cmd [:params :c2x])
@ -898,7 +898,7 @@
;; We haven't found any extremes so we turn the commands to points
extremities
(if (empty? extremities)
(->> content (keep helpers/command->point))
(->> content (keep helpers/segment->point))
extremities)]
;; If no points are returned we return an empty rect.

View File

@ -18,7 +18,7 @@
(defn make-subpath
"Creates a subpath either from a single command or with all the data"
([command]
(let [p (helpers/command->point command)]
(let [p (helpers/segment->point command)]
(make-subpath p p [command])))
([from to data]
{:from from
@ -31,7 +31,7 @@
(let [command (if (= :close-path (:command command))
(helpers/make-line-to (:from subpath))
command)
p (helpers/command->point command)]
p (helpers/segment->point command)]
(-> subpath
(assoc :to p)
(update :data conj command))))
@ -181,10 +181,10 @@
(if (nil? current)
(> signed-area 0)
(let [{x1 :x y1 :y :as p} (helpers/command->point current)
(let [{x1 :x y1 :y :as p} (helpers/segment->point current)
last? (nil? (first subpath))
first-point (if (nil? first-point) p first-point)
{x2 :x y2 :y} (if last? first-point (helpers/command->point (first subpath)))
{x2 :x y2 :y} (if last? first-point (helpers/segment->point (first subpath)))
signed-area (+ signed-area (- (* x1 y2) (* x2 y1)))]
(recur (first subpath)