Last active
May 22, 2017 11:08
-
-
Save kolja/2781c2eeac9880b2e25cbc7dc28a8af0 to your computer and use it in GitHub Desktop.
create a sphere from cubes
This file contains 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
#!/usr/local/bin/lumo | |
(ns sphere.core | |
(:require [lumo.core :refer [*command-line-args*]] | |
[clojure.string :refer [join]] | |
[cljs.reader :refer [read-string]])) | |
(defn sqr [n] (* n n)) | |
(defn sqrt [n] (.sqrt js/Math n)) | |
(defn cbrt [n] (.cbrt js/Math n)) | |
(defn abs [n] (.abs js/Math n)) | |
(defn solid? [[x y z] r-sqr] | |
(let [sum-of-sqrs (+ (sqr x) (sqr y) (sqr z)) | |
delta (- (sqrt r-sqr) (sqrt sum-of-sqrs))] | |
(<= -0.1 delta 1.2))) | |
(defn sphere [diameter] | |
(let [radius (/ (dec diameter) 2) | |
r-sqr (sqr radius) | |
dimension (range (- radius) (inc radius))] | |
(into {} (for [x dimension | |
y dimension | |
z dimension] | |
[[x y z] (solid? [x y z] r-sqr)])))) | |
(defn print-sphere [s] | |
(let [coordinates (sort (keys s)) | |
strings (map #(if (get s %) "[]" " ") coordinates) | |
d (cbrt (count coordinates)) | |
divider (join (repeat (* 2 d) "-"))] | |
(doseq [planes (partition d (partition d strings))] | |
(println divider) | |
(doseq [line planes] | |
(println (join line)))))) | |
(def diameter (read-string (first *command-line-args*))) | |
(print-sphere (sphere diameter)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment