Skip to content

Commit

Permalink
draw resistor connections
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtized committed Dec 12, 2024
1 parent 623043e commit ac5fce9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/shimmers/sketches/circuit_diagrams.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,36 @@
(bar pt3 (* 0.02 size) angle)])))
faces)))

(defn resistor [steps p q]
(let [a (tm/mix p q 0.2)
b (tm/mix p q 0.8)
perp (g/scale (g/rotate (tm/- b a) (* 0.25 eq/TAU)) (/ 1.0 steps))]
(csvg/group {:stroke-width 2.0}
(concat
[(gl/line2 p a)
(gl/line2 b q)]
(map gl/line2
(partition 2 1 (for [n (range (inc steps))]
(cond (zero? n) a
(= steps n) b
(odd? n) (tm/+ (tm/mix a b (* n (/ 1.0 steps))) perp)
(even? n) (tm/- (tm/mix a b (* n (/ 1.0 steps))) perp)))))))))

(defmethod draw-component :resistor
[{:keys [shape faces]}]
(let [center (g/centroid shape)]
(keep (fn [{conn :connected :as face}]
(when conn
(resistor (dr/random-int 6 11) (face-center face) center))) faces)))

(defn draw [{:keys [shape faces] :as component}]
(concat [shape]
(mapcat draw-face faces)
(draw-component component)))

(defn make-component [shape]
{:shape shape
:kind (dr/weighted {:wire 1 :orb 1 :ground 1})
:kind (dr/weighted {:wire 1 :orb 1 :ground 1 :resistor 1})
:faces (face-normals shape)})

(defn shapes []
Expand Down

0 comments on commit ac5fce9

Please sign in to comment.