Skip to content

Commit

Permalink
follow the leader leader leader
Browse files Browse the repository at this point in the history
  • Loading branch information
pishty1 committed Dec 18, 2023
1 parent 6c5cca7 commit dc859e5
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 87 deletions.
4 changes: 3 additions & 1 deletion src/sketches/entry.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

(defn chooser
([]
(euclid/start))
;; (euclid/start)
(tut5/start)
)
([x]
(case x
0 (flow/start)
Expand Down
38 changes: 18 additions & 20 deletions src/sketches/repo/euclid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,32 @@
(q/background 230 230 230)
(q/stroke 130, 0 0)
(q/stroke-weight 4)
{:mouse-pressed-position nil}
)
{:mouse-pressed-position nil})

(defn draw-state [state]

(q/background 190 130 30)
(q/stroke-weight 2)
(when (:mouse-pressed-position state)
(let [origin (:mouse-pressed-position state)
line {:fromx (first origin) :fromy (second origin)
:tox (q/mouse-x) :toy (q/mouse-y)}
anchor-circle {:x (first origin) :y (second origin) :w 20 :h 20}
current-mouse {:x (q/mouse-x) :y (q/mouse-y) :w 20 :h 20}
diff (v/sub [(:x current-mouse) (:y current-mouse)]
origin)
mag (* 2 (v/mag diff))
rcircle {:x (:x anchor-circle)
:y (:y anchor-circle)
:w mag
:h mag}]
(let [origin (:mouse-pressed-position state)
anchor-circle {:x (first origin) :y (second origin) :w 20 :h 20}
line {:fromx (first origin) :fromy (second origin)
:tox (q/mouse-x) :toy (q/mouse-y)}
current-mouse {:x (q/mouse-x) :y (q/mouse-y) :w 20 :h 20}
diff (v/sub [(:fromx line) (:fromy line)]
[(:tox line) (:toy line)])

(q/line (:fromx line) (:fromy line) (:tox line) (:toy line))
(q/fill 255 150)
(q/ellipse (:x anchor-circle) (:y anchor-circle) (:w anchor-circle) (:h anchor-circle))
(q/ellipse (:x current-mouse) (:y current-mouse) (:w current-mouse) (:h current-mouse))
(q/ellipse (:x rcircle) (:y rcircle) (:w rcircle) (:h rcircle))))
mag (* 2 (v/mag diff))
rcircle {:x (:x anchor-circle)
:y (:y anchor-circle)
:w mag
:h mag}]

)
(q/line (:fromx line) (:fromy line) (:tox line) (:toy line))
(q/fill 255 150)
(q/ellipse (:x anchor-circle) (:y anchor-circle) (:w anchor-circle) (:h anchor-circle))
(q/ellipse (:x current-mouse) (:y current-mouse) (:w current-mouse) (:h current-mouse))
(q/ellipse (:x rcircle) (:y rcircle) (:w rcircle) (:h rcircle)))))


(defn update-state [state]
Expand Down
127 changes: 74 additions & 53 deletions src/sketches/repo/tut5.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,67 @@
;; fill(255, 150);
;; ellipse(width/2, height/2, 50, 50);

(defn bounce [location speed w h]
[(if (or (> (first location) w)
(< (first location) 0))
(* (first speed) -1)
(first speed))
(if (or (> (second location) h)
(< (second location) 0))
(* (second speed) -1)
(second speed))])

(defn wrap [location w h]
[(if (> (first location) w)
0
(if (< (first location) 0)
w
(first location)))
(if (> (second location) h)
0
(if (< (second location) 0)
h
(second location)))])

(defn boundries [location speed w h type]
(case type
:bounce (bounce location speed w h)
:wrap (wrap location w h)
(wrap location w h)))

(defn setup []
(q/smooth)
(q/background 230 230 230)
(q/stroke 130, 0 0)
(q/stroke-weight 4)
(let [number-of-balls 4]
(let [number-of-balls 100]

{:balls (map (fn [_]
{:location {:x (rand-int (q/width)) :y (rand-int (q/height))}
:speed {:x (rand-int 3) :y (rand-int 5)}
:stroke (list (rand-int 255) (rand-int 255) (rand-int 255))
:fill (list (rand-int 255) (rand-int 255) (rand-int 255))
})
{:location [(rand-int (q/width)) (rand-int (q/height))]
:speed [0 0]
:acc [0 0]
:stroke (list 0 0 100)
:fill (list 0 0 255)})
(range number-of-balls))}))
(defn update-state [{:keys [balls] :as state}]
(loop [myballs balls
newballs []]
(if (seq myballs)
(let [ball (first myballs)
location (:location ball)
speed (:speed ball)
diff (v/sub [(q/mouse-x) (q/mouse-y)] location )
normalized-diff (v/norm diff)
scaled-diff (v/mult normalized-diff 0.1)
updated-speed (v/add speed scaled-diff)
limited-speed (v/limit updated-speed 5)
updated-location (v/add location limited-speed)]
(recur (rest myballs)
(conj newballs (assoc ball
:location updated-location
:speed updated-speed))))
(assoc state
:balls newballs))))

(defn draw-state [{:keys [balls]}]

Expand All @@ -46,58 +93,32 @@
(q/fill (first (:fill ball))
(second (:fill ball))
(last (:fill ball)))
(q/ellipse (:x (:location ball)) (:y (:location ball)) 26 26))
(q/ellipse (first (:location ball)) (second (:location ball)) 26 26))

(let [center [(/ (q/width) 2)
(/ (q/height) 2)]
mouse [(q/mouse-x)
(q/mouse-y)]
diff (v/sub mouse center)
;; scaled-diff (v/mult diff 0.8)
scaled-diff-div (v/div diff 0.5)
scaled-diff-div (v/div diff 1)
mag (v/mag scaled-diff-div)
[finalx finaly] scaled-diff-div]
(q/line (first center) (last center)
(+ (first center) finalx) (+ (last center) finaly))

(q/fill 0)
(q/rect 0 0 mag, 10)
))


(defn update-state [{:keys [balls w h] :as state}]
(loop [myballs balls
newballs []]
(if (seq myballs)
(let [ball (first myballs)
location (:location ball)
speed (:speed ball)
newlocation {:x (+ (:x location) (:x speed))
:y (+ (:y location) (:y speed))}
newspeed {:x (if (or (> (:x newlocation) w)
(< (:x newlocation) 0))
(* (:x speed) -1)
(:x speed))
:y (if (or (> (:y newlocation) h)
(< (:y newlocation) 0))
(* (:y speed) -1)
(:y speed))}]
(recur (rest myballs)
(conj newballs (assoc ball
:location newlocation
:speed newspeed))))
(assoc state
:balls newballs))))

(defn start []
(q/defsketch Something2
:host "sketch"
:title "Cross with circle"
:setup setup
:update update-state
:draw draw-state
:renderer :p2d
:mouse-clicked menu/when-mouse-pressed
:size [menu/w menu/h]
:middleware [menu/show-frame-rate
m/fun-mode]))
[finalx finaly] scaled-diff-div]))






(defn start []
(q/defsketch Something2
:host "sketch"
:title "Cross with circle"
:setup setup
:update update-state
:draw draw-state
:renderer :p2d
:mouse-clicked menu/when-mouse-pressed
:size [menu/w menu/h]
:middleware [menu/show-frame-rate
m/fun-mode]))
42 changes: 29 additions & 13 deletions src/sketches/vectorop.cljs
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
(ns sketches.vectorop)

(defn sub [[x1 y1] [x2 y2]]
[(- x1 x2)
(- y1 y2)])
(defn add [[x1 y1] [x2 y2]]
[(+ x1 x2)
(+ y1 y2)])

(defn mult [[x y] scalar]
[(* x scalar)
(* y scalar)])
(defn sub [[x1 y1] [x2 y2]]
[(- x1 x2)
(- y1 y2)])

(defn div [[x y] scalar]
[(/ x scalar)
(/ y scalar)])
(defn mult [[x y] scalar]
[(* x scalar)
(* y scalar)])

(defn mag [[x y]]
(Math/sqrt (+ (* x x) (* y y))))
(defn div [[x y] scalar]
[(/ x scalar)
(/ y scalar)])

(defn normalize [[x y]]
(div [x y] (mag [x y])))
(defn mag [[x y]]
(Math/sqrt (+ (* x x) (* y y))))

(defn norm [[x y]]
(div [x y]
(mag [x y])))

(defn rand2d []
(let [randx (- (* 2 (rand)) 1)
randy (- (* 2 (rand)) 1)]
[randx randy]))

(defn limit [[x y] limit]
(let [mag (mag [x y])]
(if (> mag limit)
(mult (norm [x y]) limit)
[x y])))

0 comments on commit dc859e5

Please sign in to comment.