Skip to content

Commit 9781e05

Browse files
committed
Fix bugs in ex49 and set radius to oscillate
* Render smoothly * Replace Vec2. with vector * Refactor * Add a factor of sin(theta) to the radius
1 parent 943047a commit 9781e05

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ In Emacs/Cider: C-c C-k
5858
* ex45 - scalar fields from vector fields
5959
* ex46 - distributions
6060
* ex47 - nebula (https://generateme.wordpress.com/2018/10/24/smooth-rendering-log-density-mapping/)
61-
* ex48 - visualization of reconstruction filters used in log density renderer
61+
* ex48 - visualization of reconstruction filters used in log density renderer
62+
* ex49 - archimedian spiral with an oscillating radius
6263
* NOC - [Nature of Code](https://github.com/shiffman/The-Nature-of-Code-Examples/) Clojure2d version. Most interesting:
6364
* ch02/attraction-many-2-7.clj
6465
* ch02/fluidresistance-2-5.clj

src/ex49_archimedean_spiral.clj

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
(ns ex49-archimedean_spiral
2-
"Generates an archimedean spiral"
2+
"Generates an archimedean spiral with an oscillating radius"
33
(:require [clojure2d.core :refer :all]
4-
[fastmath.core :as m])
5-
(:import [fastmath.vector Vec2]))
4+
[fastmath.core :as m]
5+
[fastmath.vector :as v]))
66

77
;; be sure everything is fast as possible
88
(set! *warn-on-reflection* true)
99
(set! *unchecked-math* :warn-on-boxed)
1010
(m/use-primitive-operators)
1111

12-
(def ^:const ^long fps 60)
13-
1412
(def ^:const ^long w 800)
1513
(def ^:const ^long h 800)
1614

1715
(def ^:const ^double x0 400)
1816
(def ^:const ^double y0 400)
1917

20-
(def ^:const ^double tStep 0.5)
21-
22-
(def ^:const ^double tAngleScale (/ m/PI 2))
23-
(def ^:const ^double tRadiusScale 10)
24-
25-
(defn draw [canvas window ^long frame _]
26-
(let [field (get-state window)
27-
t ^double (/ frame fps)]
28-
(-> canvas
29-
(set-background :black 50)
30-
(set-color :white 80))
31-
(let [p (for [^double theta (range 0 t tStep)] (Vec2.
32-
(+ x0 (* (m/cos (* theta ^double tAngleScale)) (* theta tRadiusScale)))
33-
(+ y0 (* (m/sin (* theta ^double tAngleScale)) (* theta tRadiusScale)))))]
34-
(path-bezier canvas p false true))))
35-
36-
(def window (show-window {:canvas (canvas w h)
37-
:window-name "Archimedean spiral"
38-
:draw-fn draw
39-
:fps fps}))
18+
(def ^:const ^double tStep 0.01)
19+
20+
(def ^:const ^double tAngleScale m/HALF_PI)
21+
(def ^:const ^double tRadiusScale 10.0)
22+
23+
(def ^:const ^double ampFactor 0.3)
24+
(def ^:const ^double periodFactor 16)
25+
26+
(defn draw [canvas window ^double frame _]
27+
(let [t1 ^double (/ frame 60.0)]
28+
(-> canvas
29+
(set-background :black 50)
30+
(set-color :white 80))
31+
(let [p (for [^double t (range 0.0 t1 tStep)]
32+
(let [theta (* t tAngleScale)
33+
r (* (+ 1 (* ampFactor (m/sin (* periodFactor theta)))) (* t tRadiusScale))]
34+
(vector (+ x0 (* (m/cos theta) r))
35+
(+ y0 (* (m/sin theta) r)))
36+
))]
37+
(path canvas p))))
38+
39+
(def window (show-window {:canvas (canvas w h :highest)
40+
:window-name "Archimedean spiral with an oscillating radius"
41+
:draw-fn draw}))

0 commit comments

Comments
 (0)