Skip to content

Commit

Permalink
Added txtplot for drawing points in terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
mogenslund committed Jan 27, 2021
1 parent 14bf881 commit 593c4f5
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/liq/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
[clojure.java.io :as io]
[clojure.pprint :as pprint]
[liq.extras.mindmap :refer [mindmap]]
[liq.extras.txtplot :refer [txtbitmap txtplot]]
[liq.util :as util]
[liq.buffer :as buffer]
[liq.editor :as editor]))
Expand Down
67 changes: 67 additions & 0 deletions src/liq/extras/txtplot.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
(ns liq.extras.txtplot
(:require [clojure.string :as str]))

(defn txtbitmap
[points]
(let [maxx (quot (+ (apply max (map first points)) 2) 2)
maxy (quot (+ (apply max (map second points)) 2) 2)
v0 (apply vector (map #(or [0 0 0 0] %) (range maxx)))
bitmap0 (apply vector (map #(or v0 %) (range maxy)))
;; p -> p0, v0 : [2 1] -> [1 0], [0 0 1 0] (quot 2 2)
csplit (fn [p] [[(quot (first p) 2) (quot (second p) 2)]
(cond (and (even? (first p)) (even? (second p))) [1 0 0 0]
(and (odd? (first p)) (even? (second p))) [0 1 0 0]
(and (even? (first p)) (odd? (second p))) [0 0 1 0]
(and (odd? (first p)) (odd? (second p))) [0 0 0 1])])
;; [0 0 1 1] [0 1 1 0] -> [0 1 1 1]
ccombine (fn [p1 p2] (apply vector (map max p1 p2)))
chararray [" " "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""]
tochar (fn [v] (chararray (+ (* (v 0) 8) (* (v 1) 4) (* (v 2) 2) (* (v 3) 1))))]
(str/join "\n"
(map #(str/join (map tochar %))
(loop [bitmap bitmap0 remaining points]
(if (empty? remaining)
bitmap
(let [[p0 v0] (csplit (first remaining))
bitmap1 (update-in bitmap [(second p0) (first p0)] #(ccombine % v0))]
(recur bitmap1 (rest remaining)))))))))

(defn txtplot
[xres yres points]
(let [xmin (apply min (map first points))
xmax (apply max (map first points))
ymin (apply min (map second points))
ymax (apply max (map second points))
mapx (fn [x] (Math/round (+ 0.0 (/ (* (- x xmin) xres) (- xmax xmin)))))
mapy (fn [y] (Math/round (+ 0.0 (- yres (/ (* (- y ymin) yres) (- ymax ymin))))))
mapxy (fn [p] [(mapx (first p)) (mapy (second p))])]
(txtbitmap (apply vector (map mapxy points)))))

;; Esamples
(comment

;; Basic samples
(txtbitmap [[0 0] [10 1] [10 2] [4 5] [6 5]])
(txtplot 100 200 [[-100 2] [-5 3] [0 0] [3 -15]])

;; Sinus curve
(let [xs (range -10 10 0.04)
points (map #(vector % (Math/sin %)) xs)]
(txtplot 200 50 points))

;; Sierpinski fractal
(let [sierpinski [[0.5 0 0 0.5 0 0 0.33]
[0.5 0 0 0.5 0 0.5 0.33]
[0.5 0 0 0.5 0.5 0 0.34]]
nextpoint (fn [data [x0 y0]]
(let [r (rand)
index (loop [i 0 accum ((data 0) 6)]
(if (> accum r)
i
(recur (+ i 1) (+ accum ((data (+ i 1)) 6)))))
x (+ (* ((data index) 0) x0) (* ((data index) 1) y0) ((data index) 4))
y (+ (* ((data index) 2) x0) (* ((data index) 3) y0) ((data index) 5))]
[x y]))
points (take 25000 (iterate #(nextpoint sierpinski %) [0 0]))]
(txtplot 108 54 points)))

39 changes: 39 additions & 0 deletions test/liq/extras/freemove_mode_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(ns liq.extras.freemove-mode-test
(:require [clojure.test :refer :all]
[liq.buffer :as buffer]
[liq.extras.freemove-mode :refer :all]))

(deftest trim-line-test
""
(is (= (-> (buffer/buffer "aaa bbb") (trim-line 1) buffer/text) "aaa bbb"))
(is (= (-> (buffer/buffer "aaa bbb ") (trim-line 1) buffer/text) "aaa bbb"))
(is (= (-> (buffer/buffer " ") (trim-line 1) buffer/text) ""))
(is (= (-> (buffer/buffer "") (trim-line 2) buffer/text) "")))

(deftest trim-buffer-test
""
(is (= (-> (buffer/buffer "aaa bbb") trim-buffer buffer/text) "aaa bbb"))
(is (= (-> (buffer/buffer " aaa bbb ") trim-buffer buffer/text) " aaa bbb"))
(is (= (-> (buffer/buffer " aaa bbb \n \naa ") trim-buffer buffer/text) " aaa bbb\n\naa"))
(is (= (-> (buffer/buffer " aaa bbb \n \naa ") trim-buffer buffer/end-of-buffer buffer/text) " aaa bbb\n\naa"))
(is (= (-> (buffer/buffer " aaa bbb \n \naa ") buffer/end-of-buffer trim-buffer buffer/text) " aaa bbb\n\naa"))
(is (= (-> (buffer/buffer " aaa bbb \n \naa ") buffer/end-of-buffer trim-buffer buffer/right buffer/text) " aaa bbb\n\naa")))

(deftest beginning-of-boundry-test
""
(is (= (-> (buffer/buffer "aa bbbb ccc dd") (buffer/right 6) beginning-of-boundry ::buffer/col) 5))
(is (= (-> (buffer/buffer "aa bbbb ccc dd") (buffer/right 6) beginning-of-boundry ::buffer/col) 1)))

(deftest move-region-tmp-test
""
(is (= (buffer/text
(move-region-tmp (-> (buffer/buffer "abc") (buffer/right))
[{::buffer/row 1 ::buffer/col 1} {::buffer/row 1 ::buffer/col 3}] 0 1))
" abc")))

(deftest move-right-test
""
(is (= (-> (buffer/buffer "abc") (buffer/right) move-right ::buffer/cursor ::buffer/col) 3))
(is (= (-> (buffer/buffer "abc") (buffer/right) move-right buffer/text) " abc"))
(is (= (-> (buffer/buffer "abc") (buffer/right) move-right move-right ::buffer/cursor ::buffer/col) 4))
(is (= (-> (buffer/buffer "abc") (buffer/right) move-right move-right buffer/text) " abc")))

0 comments on commit 593c4f5

Please sign in to comment.