Skip to content

Commit

Permalink
Triangle tasks: don't generate lines that almost overlap each other.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbeloglazov committed Jul 2, 2015
1 parent a060776 commit e4df8bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/project.clj
Expand Up @@ -13,6 +13,7 @@
[dk.brics.automaton/automaton "1.11.2"]]
:plugins [[lein-ring "0.8.10"]
[lein-immutant "1.2.0"]]
:profiles {:dev {:dependencies [[quil "2.2.6"]]}}
:ring {:handler com.geekalarm.server.server/handler
:init com.geekalarm.server.server/run-collector}
:uberjar-name "root.war")
Expand Down
39 changes: 38 additions & 1 deletion server/src/com/geekalarm/server/tasks/triangles_count.clj
Expand Up @@ -72,10 +72,25 @@
(- y2 y0))))
2)))

(defn lines-overlap? [[[x1 y1] [x2 y2] :as l1]
[[x3 y3] [x4 y4] :as l2]
inters]
(let [dx1 (- x1 x2)
dy1 (- y1 y2)
dx2 (- x3 x4)
dy2 (- y3 y4)
len1 (Math/hypot dx1 dy1)
len2 (Math/hypot dx2 dy2)
cos (/ (+ (* dx1 dx2) (* dy1 dy2))
len1 len2)]
(and (> (Math/abs cos) (Math/cos (Math/toRadians 2)))
(inside-offsets? inters (- offset)))))

(defn satisfy?
([line1 line2]
(if-let [inters (intersection line1 line2)]
(not (near-border? inters))
(and (not (near-border? inters))
(not (lines-overlap? line1 line2 inters)))
false))
([lines]
(and (every? true? (map #(apply satisfy? %) (combinations lines 2)))
Expand Down Expand Up @@ -138,3 +153,25 @@
:name "Triangles count"
:description (str "Calculate how many triangles are on screen.")
:generator #'generate})

(comment

(require '[quil.core :as q])

(defn overlapping-lines [n]
(let [lines (repeatedly n rand-line)]
(set (for [a lines
b lines
:when (and (not= a b)
(lines-overlap? a b))]
a))))

(defn q-draw-lines [lines]
(q/sketch
:size [size size]
:draw (fn []
(q/background 255)
(doseq [line lines]
(apply q/line line)))))

)

0 comments on commit e4df8bc

Please sign in to comment.