Last active
June 30, 2017 08:49
-
-
Save mmzsource/833ddfb2cf87c54fe4fc26996466cd1d to your computer and use it in GitHub Desktop.
Code belonging to and explained in my "Bézier in Clojure" blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; Assume leiningen + lein-try plugin installed and working | |
;; | |
;; Fire up a repl with: | |
;; | |
;; lein try incanter "1.5.7" | |
;; | |
(require '[incanter.core :as incanter]) | |
(require '[incanter.charts :as charts]) | |
(defn pow [base exponent] | |
(reduce *' (repeat exponent base))) | |
(defn bezier-3 [P0 P1 P2] | |
(fn [t] | |
(+ | |
(* (pow (- 1 t) 2) P0) | |
(* 2 (- 1 t) t P1) | |
(* (pow t 2) P2)))) | |
(defn view-bezier-plot [[x1 y1] [x2 y2] [x3 y3] plot-title] | |
(let [b3x (bezier-3 x1 x2 x3) | |
xs (map b3x (range 0 1.0 0.01)) | |
b3y (bezier-3 y1 y2 y3) | |
ys (map b3y (range 0 1.0 0.01)) | |
dataset (incanter/conj-cols xs ys) | |
xy-plot (charts/xy-plot "col-0" "col-1" :data dataset :points true :title plot-title)] | |
(incanter/view xy-plot))) | |
;; Example bezier curves: | |
(view-bezier-plot [0 0] [1 0] [1 1] "increasing ascending") | |
(view-bezier-plot [0 0] [0 -1] [1 1] "swoosh") | |
(view-bezier-plot [-2 4] [0 -4] [2 4] "y = x^2 ?") | |
(view-bezier-plot [-1 0] [0 0] [1 0] "my pulse after a useless meeting") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment