packages feed

diagrams-lib-1.0.1 / src / Diagrams / ThreeD / Vector.hs

{-# LANGUAGE FlexibleContexts           , TypeFamilies           , ViewPatterns  #-}------------------------------------------------------------------------------- |-- Module      :  Diagrams.ThreeD.Vector-- Copyright   :  (c) 2013 diagrams-lib team (see LICENSE)-- License     :  BSD-style (see LICENSE)-- Maintainer  :  diagrams-discuss@googlegroups.com---- Three-dimensional vectors.-------------------------------------------------------------------------------module Diagrams.ThreeD.Vector       ( -- * Special 2D vectors         unitX, unitY, unitZ, unit_X, unit_Y, unit_Z,         -- * Converting between vectors and angles         direction, fromDirection, angleBetween, angleBetweenDirs       ) whereimport Control.Lens (op)import Data.VectorSpaceimport Data.Crossimport Diagrams.ThreeD.Typesimport Diagrams.Coordinates-- | The unit vector in the positive X direction.unitX :: R3unitX = 1 ^& 0 ^& 0-- | The unit vector in the positive Y direction.unitY :: R3unitY = 0 ^& 1 ^& 0-- | The unit vector in the positive Z direction.unitZ :: R3unitZ = 0 ^& 0 ^& 1-- | The unit vector in the negative X direction.unit_X :: R3unit_X = (-1) ^& 0 ^& 0-- | The unit vector in the negative Y direction.unit_Y :: R3unit_Y = 0 ^& (-1) ^& 0-- | The unit vector in the negative Z direction.unit_Z :: R3unit_Z = 0 ^& 0 ^& (-1)-- | @direction v@ is the direction in which @v@ points.  Returns an--   unspecified value when given the zero vector as input.direction :: Direction d => R3 -> ddirection v  | r == 0 = fromSpherical $ Spherical zero zero  | otherwise = fromSpherical $ Spherical θ φ where  r = magnitude v  (x,y,z) = unr3 v  φ = Rad . asin $ z / r  θ = Rad . atan2 y $ x  zero = Rad $ 0-- | @fromDirection d@ is the unit vector in the direction @d@.fromDirection :: Direction d => d -> R3fromDirection (toSpherical -> (Spherical θ' φ')) = r3 (x,y,z) where  θ = op Rad $ θ'  φ = op Rad $ φ'  x = cos θ * cos φ  y = sin θ * cos φ  z = sin φ-- | compute the positive angle between the two vectors in their common planeangleBetween  :: (Angle a, Num a, Ord a) => R3 -> R3 -> aangleBetween v1 v2 = convertAngle . Rad $                     atan2 (magnitude $ cross3 v1 v2) (v1 <.> v2)-- | compute the positive angle between the two vectors in their common planeangleBetweenDirs  :: (Direction d, Angle a, Num a, Ord a) => d -> d -> aangleBetweenDirs d1 d2 = angleBetween (fromDirection d1) (fromDirection d2)