Created
April 2, 2010 13:17
-
-
Save swannodette/353121 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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