-
Notifications
You must be signed in to change notification settings - Fork 3
/
geom.clj
52 lines (39 loc) · 1.48 KB
/
geom.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns gdl.math.geom
"API wrapping com.badlogic.gdx.math.Intersector"
(:import (com.badlogic.gdx.math Rectangle Circle Intersector)))
(defmulti ^:private collides?* (fn [a b] [(class a) (class b)]))
(defmethod collides?* [Circle Circle]
[^Circle a ^Circle b]
(Intersector/overlaps a b))
(defmethod collides?* [Rectangle Rectangle]
[^Rectangle a ^Rectangle b]
(Intersector/overlaps a b))
(defmethod collides?* [Rectangle Circle]
[^Rectangle rect ^Circle circle]
(Intersector/overlaps circle rect))
(defmethod collides?* [Circle Rectangle]
[^Circle circle ^Rectangle rect]
(Intersector/overlaps circle rect))
(defn- rectangle? [{[x y] :left-bottom :keys [width height]}]
(and x y width height))
(defn- circle? [{[x y] :position :keys [radius]}]
(and x y radius))
(defn- m->shape [m]
(cond
(rectangle? m) (let [{[x y] :left-bottom :keys [width height]} m]
(Rectangle. x y width height))
(circle? m) (let [{[x y] :position :keys [radius]} m]
(Circle. x y radius))
:else (throw (Error. (str m)))))
(defn collides? [a b]
(collides?* (m->shape a) (m->shape b)))
(defn point-in-rect? [[x y] rectangle]
(.contains ^Rectangle (m->shape rectangle) x y))
(defn circle->outer-rectangle [{[x y] :position :keys [radius] :as circle}]
{:pre [(circle? circle)]}
(let [radius (float radius)
size (* radius 2)]
{:left-bottom [(- (float x) radius)
(- (float y) radius)]
:width size
:height size}))