Add aphorism thirty-five: middle-point of a linestring

This commit is contained in:
arne 2023-08-05 15:55:20 +02:00
commit 35b70dcbb6

View file

@ -0,0 +1,60 @@
(ns aphorisms.thirty-five
(:require [quil.core :as q]
[quil.middleware :as qm]
[thi.ng.geom.core :as g]
[thi.ng.geom.line :as l]
[thi.ng.geom.rect :as r]
[thi.ng.geom.vector :as v]))
;; Small experiment to find the central point along a line string;
;; Turns out this is as easy as calling (g/point-at l 0.5) on a non-empty line :)
(def bounds (r/rect 500 500))
(defn setup []
(q/ellipse-mode :center)
(q/rect-mode :corners)
(q/color-mode :hsb 255)
{:current-line (l/linestrip2 [])})
(defn mouse-pressed [state ev]
(prn state)
(if (= (:button ev) :right)
;; clear on right-click
(assoc state :current-line (l/linestrip2 []))
(update-in state [:current-line :points] conj (v/vec2 (:x ev) (:y ev)))))
(defn draw-state [state]
(q/background 255 0 255)
(q/no-fill)
(q/stroke 255 120 220)
(q/stroke-weight 1)
;; draw lines between points
(doseq [[[x1 y1] [x2 y2]] (->>
(interleave (-> state :current-line :points) (rest (-> state :current-line :points)))
(partition 2))]
(q/line x1 y1 x2 y2))
;; draw points
(q/stroke 255 0 255)
(q/stroke-weight 2)
(q/fill 255 255 220)
(doseq [[x y] (-> state :current-line :points)]
(q/ellipse x y 5 5))
;; draw center point
(when (seq (-> state :current-line :points))
(let [[cx cy] (g/point-at (:current-line state) 0.5)]
(q/ellipse cx cy 10 10))))
#_:clj-kondo/ignore
(q/defsketch thirty-five
:title "Thirty-Five"
:size (:size bounds)
:settings #(q/pixel-density (q/display-density))
:features [:keep-on-top]
:setup setup
:mouse-pressed mouse-pressed
:update identity
:draw draw-state
:middleware [qm/pause-on-error #_(screenshottable) qm/fun-mode])
(defn -main [& _args])