Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
update to 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ztellman committed Jun 27, 2010
1 parent 54dc2fa commit 333a467
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
4 changes: 2 additions & 2 deletions project.clj
@@ -1,7 +1,7 @@
(defproject cantor "0.2.0-SNAPSHOT"
(defproject cantor "0.2.0"
:description "primitive math for clojure"
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]
:dev-dependencies [[lein-clojars "0.5.0-SNAPSHOT"]
[leiningen/lein-swank "1.2.0-SNAPSHOT"]
[autodoc "0.7.1"]])
[autodoc "0.7.1"]])
4 changes: 2 additions & 2 deletions src/cantor.clj
Expand Up @@ -102,8 +102,8 @@
(import-fn range/interval)
(import-fn range/box2)
(import-fn range/box3)
(import-fn #'range/ul)
(import-fn #'range/lr)
(import-fn #'range/upper)
(import-fn #'range/lower)
(import-fn range/intersection)
(import-fn range/union)
(import-fn range/inside?)
Expand Down
59 changes: 38 additions & 21 deletions src/cantor/range.clj
Expand Up @@ -6,70 +6,87 @@
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns ^{:skip-wiki true} cantor.range
(ns
^{:skip-wiki true}
cantor.range
(:use [cantor.utils])
(:require [cantor.vector :as vec])
(:import [cantor.vector Vec2 Vec3]))

;;;

(defprotocol Range
(#^vec/Tuple ul [r] "Returns the minima of the range.")
(#^vec/Tuple lr [r] "Returns the maxima of the range.")
(#^vec/Tuple upper [r] "Returns the minima of the range.")
(#^vec/Tuple lower [r] "Returns the maxima of the range.")
(clone [r a b] "Returns a range of the same type with endpoints set to a and b"))

(defn overlap?
"Returns true if the two ranges overlap."
[a b]
(and
(vec/all? <= (ul a) (lr b))
(vec/all? >= (lr a) (ul b))))
(vec/all? <= (upper a) (lower b))
(vec/all? >= (lower a) (upper b))))

(defn intersection
"Returns the intersection of the two ranges, or nil if they don't intersect."
[a b]
(when (overlap? a b)
(clone a
(vec/map* max (ul a) (ul b))
(vec/map* min (lr a) (lr b)))))
(vec/map* max (upper a) (upper b))
(vec/map* min (lower a) (lower b)))))

(defn union
"Returns the union of the two ranges."
[a b]
(clone a
(vec/map* min (ul a) (ul b))
(vec/map* max (lr a) (lr b))))
(vec/map* min (upper a) (upper b))
(vec/map* max (lower a) (lower b))))

(defn inside?
"Returns true if vector 'p' is inside range 'r'."
[r p]
(and (vec/all? <= (ul r) p)
(vec/all? >= (lr r) p)))
(and (vec/all? <= (upper r) p)
(vec/all? >= (lower r) p)))

(defn size
"Returns the difference between the two extremes of the range."
[r]
(vec/sub (lr r) (ul r)))
(vec/sub (lower r) (upper r)))

;;;

(defrecord Interval [#^double a #^double b]
Range
(ul [r] a)
(lr [r] b)
(clone [_ a b] (Interval. a b)))
(upper [r] a)
(lower [r] b)
(clone [_ a b] (Interval. a b))
clojure.lang.IFn
(invoke [_ n]
(condp = n
0 a
1 b)))

(defrecord Box2 [#^Vec2 a #^Vec2 b]
Range
(ul [r] a)
(lr [r] b)
(clone [_ a b] (Box2. a b)))
(upper [r] a)
(lower [r] b)
(clone [_ a b] (Box2. a b))
clojure.lang.IFn
(invoke [_ n]
(condp = n
0 a
1 b)))

(defrecord Box3 [#^Vec3 a #^Vec3 b]
Range
(ul [r] a)
(lr [r] b)
(clone [_ a b] (Box3. a b)))
(upper [r] a)
(lower [r] b)
(clone [_ a b] (Box3. a b))
clojure.lang.IFn
(invoke [_ n]
(condp = n
0 a
1 b)))

;;;

Expand Down
6 changes: 4 additions & 2 deletions src/cantor/vector.clj
Expand Up @@ -6,7 +6,9 @@
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns ^{:skip-wiki true} cantor.vector
(ns
^{:skip-wiki true}
cantor.vector
(:use [clojure.contrib.def :only (defmacro-)]
[cantor.misc :only (radians degrees)]
[cantor.utils :only (with-tags)]))
Expand Down Expand Up @@ -233,4 +235,4 @@
(.write writer (str "[ x=" (.x #^Vec2 v) ", y=" (.y #^Vec2 v) " ]")))

(defmethod print-method cantor.vector.Polar2 [p writer]
(.write writer (str "[ theta=" (.theta #^Polar2 p) ", r=" (.r #^Polar2 p) " ]")))
(.write writer (str "[ theta=" (.theta #^Polar2 p) ", r=" (.r #^Polar2 p) " ]")))

0 comments on commit 333a467

Please sign in to comment.