Created
March 30, 2011 19:06
-
-
Save osoleve/895059 to your computer and use it in GitHub Desktop.
Small 3D Vector Math Library
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
-- file: Vectors.hs | |
-- | |
-- Contains vector functions | |
data Vector a = Vector a a a | |
deriving (Show) | |
vecPlus3D :: (Num t) => Vector t -> Vector t -> Vector t | |
(Vector i j k) `vecPlus3D` (Vector l m n) = Vector (i+l) (j+m) (k+n) | |
vecMult3D :: (Num t) => Vector t -> t -> Vector t | |
(Vector i j k) `vecMult3D` c = Vector (i*c) (j*c) (k*c) | |
scalMult3D :: (Num t) => Vector t -> Vector t -> t | |
(Vector i j k) `scalMult3D` (Vector l m n) = i*l + j*m + k*n | |
magnitude3D :: (Floating t) => Vector t -> t | |
magnitude3D (Vector i j k) = sqrt (i**2 + j**2 + k**2) | |
dot3D :: (Floating t) => Vector t -> Vector t -> t | |
(Vector i j k) `dot3D` (Vector l m n) = (i*l + j*m + k*n) | |
theta3D :: (Floating t) => Vector t -> Vector t -> t | |
theta3D (Vector i j k) (Vector l m n) = (asin . magnitude3D $ crossProduct) * | |
(180/pi) | |
where crossProduct = (Vector i j k) `cross` (Vector l m n) | |
-- cross does not follow the [..]3D naming convention | |
-- because cross products only exist for 3D vectors | |
cross :: (Num t) => Vector t -> Vector t -> Vector t | |
(Vector i j k) `cross` (Vector l m n) = Vector (j*n - m*k) | |
(k*l - n*i) | |
(i*m - l*j) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment