Skip to content

Commit

Permalink
extend random-points to kinda cover ellipse object
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtized committed Jun 6, 2024
1 parent f3d4efa commit 4c38ee7
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/shimmers/algorithm/random_points.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
[thi.ng.geom.utils :as gu]
[thi.ng.geom.vector :as gv]
#?(:clj [thi.ng.geom.types]
:cljs [thi.ng.geom.types :refer [Circle2 Line2 LineStrip2 Polygon2 Rect2 Triangle2]])
:cljs [thi.ng.geom.types
:refer [Circle2 Ellipse2 Line2 LineStrip2 Polygon2 Rect2 Triangle2]])
[thi.ng.math.core :as tm])
#?(:clj (:import [thi.ng.geom.types Circle2 Line2 LineStrip2 Polygon2 Rect2 Triangle2])))
#?(:clj (:import [thi.ng.geom.types
Circle2 Ellipse2 Line2 LineStrip2 Polygon2 Rect2 Triangle2])))


(defn cell-fit [{[w h] :size} n]
Expand Down Expand Up @@ -155,6 +157,28 @@
(sample-point-bounds [{:keys [p r]}]
(v/+polar p r (dr/random-tau))))

(extend-type Ellipse2
ISamplePoint
(sample-point-at
([{:keys [p rx ry]} t]
(let [theta (* t eq/TAU)]
;; FIXME: this is biased, need to use arc length somehow?
;; see: shimmers/math/geometry/ellipse.cljc#L82
;; possibly by using gu/point-at over vertices?
(tm/+ (gv/vec2 (* rx (math/cos theta))
(* ry (math/sin theta)))
p)))
([{:keys [p rx ry]} u v]
(let [theta (* v eq/TAU)
w (math/sqrt u)]
(tm/+ (gv/vec2 (* w rx (math/cos theta))
(* w ry (math/sin theta)))
p))))
(sample-point-inside [_]
(sample-point-at _ (dr/random) (dr/random)))
(sample-point-bounds [_]
(sample-point-at _ (dr/random))))

(extend-type Rect2
ISamplePoint
(sample-point-at
Expand Down

0 comments on commit 4c38ee7

Please sign in to comment.