Skip to content

Instantly share code, notes, and snippets.

@baku89
Last active May 19, 2021 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baku89/1dcc016bb49af910507be47619f65013 to your computer and use it in GitHub Desktop.
Save baku89/1dcc016bb49af910507be47619f65013 to your computer and use it in GitHub Desktop.
future_sketches
(background "#E9EDF4")
(style (fill "royalblue")
(circle [50 50] 50)
(text "Code as Data" [150 50]))
(defn path/star
{:doc "Generates a star path"
;; Parameter annotation
:params
[{:label "Center" :type "vec2"}
{:label "# Vertices" :type "number"
:validator #(round (max 2 %))}
{:label "Radius 0" :type "number"
:validator #(max 0 %)}
{:label "Radius 1" :type "number"
:validator #(max 0 %)}]
;; Handles definition
:handles
{:draw
;; Returns a list of handles with ID
;; from the function's parameters
(fn {:params [c n rmin rmax]}
(let [rmin-angle (/ PI n)]
[{:id :center :type "point" :pos c}
{:id :rmin
:type "arrow"
:pos (vec2/+
c
(vec2/dir rmin-angle rmin))
:angle rmin-angle}
{:id :rmax
:type "arrow"
:pos (vec2/+ c [rmax 0])}]))
:drag
;; In turn, returns new parameters
;; from the handle's ID and position
(fn {:id id :pos pos
:params [c n rmin rmax]}
(case id
:center [pos n rmin rmax]
:rmin [c n (vec2/dist c pos) rmax]
:rmax [c n rmin (vec2/dist c pos)]))}}
;; Function body
[c n rmin rmax]
(apply polygon
(for [i (range (* n 2))]
(let [a (* (/ i n) PI)
r (if (mod i 2) rmin rmax)]
(vec2/+ c (vec2/dir a r))))))
:start-sketch
(background "#E9EDF4")
(style (fill "#F96463")
(path/star [0 0] 6 46 76))
(background "#E9EDF4")
(defvar gap 50)
(defvar r 30)
(style (fill "tomato")
(circle [(- gap) (const 0)] r))
(style (fill "lightgoldenrodyellow")
(circle (const [0 0]) r))
(style (fill "royalblue")
(circle [gap (const 0)] r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment