Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created April 2, 2010 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/353121 to your computer and use it in GitHub Desktop.
Save swannodette/353121 to your computer and use it in GitHub Desktop.
(ns vector-math.core
(:import [javax.vecmath Vector2d]))
(def epsilon 1e-6)
(defprotocol Vec2Math
(add [this other])
(sub [this other])
(mul [this scalar])
(div [this scalar])
(clean [this])
(unit [this])
(length-squared [this])
(dot-product [this]))
(deftype vec2
[#^float x #^float y] :as this
Vec2Math
(add [other] (vec2. (+ x (.x #^vec2 other))
(+ y (.y #^vec2 other))))
(sub [other] (vec2. (- x (.x #^vec2 other))
(- y (.y #^vec2 other))))
(mul [scalar] (let [scalar (float scalar)]
(vec2. (* x scalar)
(* y scalar))))
(div [scalar] (let [scalar (float scalar)]
(vec2. (/ x scalar)
(/ y scalar))))
(length-squared [] (+ (* x x) (* y y))))
(def v (vec2 5.0 2.0))
(length-squared v)
;; ^ the line above throws the following exception:
;; vector-math.core.vec2__3954.length_squared()Ljava/lang/Object;
;; [Thrown class java.lang.AbstractMethodError]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment