   (let [[slope1 off1] (slope-offset seg1)
         [slope2 off2] (slope-offset seg2)]
     (cond
+     (and (not slope1) (not slope2))
+     nil
+
      (and slope1 slope2)
      (let [x (/ (- off2 off1) (- slope1 slope2))
            y (+ (* x slope1) off1)]

  ;; intersects in 2D (facing y)
  )

;;(face-intersect [[0 0 0] [1 1 0]] [[0.5 0.5 0.5] [0.5 0.5 -0.5]])
(defn slope [line]
  (let [[p1 p2] line
        [p1x p1y] p1

         i-xy
         [(i-xz 0) (i-xy 1) (i-xz 1)])))

(let [arm [[0.5 0.5 0.5] [0.5 0.5 -0.5]]
      face [[0.0 0.0 0.0] [1.0 1.0 0.0]]]
  (arm-face-intersect arm face))

(defn faces [pos half-width]
  "Location of faces for a given block"
  (let [[x y z] pos
        hw half-width]
      [;; back -z
       [[(- x hw) (- y hw) (- z hw)] [(+ x hw) (+ y hw) (- z hw)]]
       ;; bottom
       [[(- x hw) (- y hw) (- z hw)] [(+ x hw) (- y hw) (+ z hw)]]
       ;; side -x
       [[(- x hw) (- y hw) (- z hw)] [(- x hw) (+ y hw) (+ z hw)]]

       ;; front +z
       [[(- x hw) (- y hw) (+ z hw)] [(+ x hw) (+ y hw) (+ z hw)]]
       ;; top
       [[(- x hw) (+ y hw) (- z hw)] [(+ x hw) (+ y hw) (+ z hw)]]
       ;; side +x
       [[(+ x hw) (- y hw) (- z hw)] [(+ x hw) (+ y hw) (+ z hw)]]]))

(defn pythagorean-dist [a b]
  (Math/sqrt (+ a b)))

(defn distance-2d [p1 p2]
  (let [[p1x p1y] p1
        [p2x p2y] p2]
    (pythagorean-dist (Math/abs (float (- p2x p1x)))
                      (Math/abs (float (- p2y p1y))))))

(defn distance-3d [p1 p2]
  (let [[p1x p1y p1z] p1
        [p2x p2y p2z] p2
        xy-dist (distance-2d [p1x p1y] [p2x p2y])
        xz-dist (distance-2d [p1x p1z] [p2x p2z])]
    (pythagorean-dist xy-dist xz-dist)))

(defn closer-point-3d [point a b]
  (let [dist-a (distance-3d point a)
        dist-b (distance-3d point b)
        min-dist (min dist-a dist-b)]
    (if (= min-dist dist-a) a b)))

(defn closest-intersecting-face [pos half-width arm]
  (let [[p1 p2] arm
        faces-intersects (map (fn [face] {:face face
                                         :intersect (arm-face-intersect [p1 p2] face)})
                              (faces pos half-width))
        matching-faces-intersects (filter :intersect faces-intersects)
        closer (fn [fia fib] (let [a (:intersect fia)
                                  b (:intersect fib)
                                  cp (closer-point-3d p1 a b)]
                              (if (= cp a) fia fib)))
        closest-fi (reduce closer matching-faces-intersects)]
    (:face closest-fi)))

;; (closest-intersecting-face [0.5 0.5 0.5]
;;                            0.5
;;                            [[0.5 0.5 1.5] [0.5 0.5 -0.5]])

(defn graph-position [block]
  (let [pos (position block)
        [x y z] pos
        width (:width block)
        offset (/ width 2.0)]
    [(- x offset) (- y offset) (- z offset)]))

(defn draw-face
  ([block face] (draw-face (graph-position block) nil nil))
  ([graph-pos translate rotations]
     (let [[x y z] graph-pos]
       (shader/with-program
         (util/with-pushed-matrix
           (GL11/glTranslatef 0 0 0)
           (GL11/glTranslatef x y z)

           ;; White
           (GL11/glColor3f 0.8 0.8 0.8)
           (util/do-shape GL11/GL_LINES
                          ;; bottom +x
                          (GL11/glVertex3f 0 0 0)
                          (GL11/glVertex3f 1 0 0)
                          ;; up +y
                          (GL11/glVertex3f 1 0 0)
                          (GL11/glVertex3f 1 1 0)
                          ;; top -x
                          (GL11/glVertex3f 1 1 0)
                          (GL11/glVertex3f 0 1 0)
                          ;; down -y
                          (GL11/glVertex3f 0 1 0)
                          (GL11/glVertex3f 0 0 0)))))))

(defn draw [block]
  (let [graph-pos (graph-position block)
        [x y z] graph-pos]
    (util/with-pushed-matrix
      (GL11/glTranslatef x y z)
      (wood-block/draw))
    (draw-face block [[0 0 0] [1 1 0]])))

(defn draw-many [blocks]
  (doseq [block blocks]
    (draw block)))
