-
Notifications
You must be signed in to change notification settings - Fork 20
/
geometry.clj
48 lines (44 loc) · 1.85 KB
/
geometry.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
(ns genartlib.geometry
(:require
[genartlib.algebra :refer [angle avg interpolate]]
[quil.core :refer [dist cos sin]])
(:import
[genartlib PolyUtils]))
(defn rotate-polygon
"Rotates a polygon clockwise about its centroid. The theta argument determines
how the polygon is rotated. `points` is a sequence of [x y] pairs that
define the polygon"
[theta points]
(if (zero? theta)
points
(let [xs (map first points)
ys (map second points)
x-centroid (/ (apply + xs) (count xs))
y-centroid (/ (apply + ys) (count ys))
points (map vector xs ys)]
(map (fn [[x y]]
(let [current-angle (angle x-centroid y-centroid x y)
new-angle (+ current-angle theta)
hypot (dist x y x-centroid y-centroid)
x-offset (* hypot (cos new-angle))
y-offset (* hypot (sin new-angle))]
[(+ x-offset x-centroid) (+ y-offset y-centroid)]))
points))))
(defn polygon-contains-point?
"Returns true if a polygon contains the given point. The polygon
is defined by the first two arguments: a sequence of x values
and a sequence of y values that define the polygon."
[x-verts y-verts test-x test-y]
(PolyUtils/polygonContainsPoint (double-array x-verts) (double-array y-verts) (double test-x) (double test-y)))
(defn shrink-polygon
"Pulls all points in a polygon towards the centroid. The provided
ratio controls how far the points are pulled, with 0 producing no
change and 1.0 pulling all the way to the centroid."
[points ratio]
(let [x-points (map first points)
y-points (map second points)
x-centroid (apply avg x-points)
y-centroid (apply avg y-points)]
(for [[x y] points]
[(interpolate x x-centroid (- 1.0 ratio))
(interpolate y y-centroid (- 1.0 ratio))])))