]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Point.hs
Whitespace cleanup in Point.
[spline3.git] / src / Point.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
3 module Point (
4 Point(..),
5 dot,
6 scale )
7 where
8
9 import Test.QuickCheck ( Arbitrary(..) )
10
11
12 -- | Represents a point in three dimensions. We use a custom type (as
13 -- opposed to a 3-tuple) so that we can make the coordinates strict.
14 --
15 data Point =
16 Point !Double !Double !Double
17 deriving (Eq, Show)
18
19
20 instance Arbitrary Point where
21 arbitrary = do
22 (x,y,z) <- arbitrary
23 return $ Point x y z
24
25
26 instance Num Point where
27 (Point x1 y1 z1) + (Point x2 y2 z2) = Point (x1+x2) (y1+y2) (z1+z2)
28 (Point x1 y1 z1) - (Point x2 y2 z2) = Point (x1-x2) (y1-y2) (z1-z2)
29 (Point x1 y1 z1) * (Point x2 y2 z2) = Point (x1*x2) (y1*y2) (z1*z2)
30 abs (Point x y z) = Point (abs x) (abs y) (abs z)
31 signum (Point x y z) = Point (signum x) (signum y) (signum z)
32 fromInteger n =
33 Point coord coord coord
34 where
35 coord = fromInteger n
36
37
38 -- | Scale a point by a constant.
39 scale :: Point -> Double -> Point
40 scale (Point x y z) d = Point (x*d) (y*d) (z*d)
41
42
43 -- | Returns the dot product of two points (taken as three-vectors).
44 {-# INLINE dot #-}
45 dot :: Point -> Point -> Double
46 dot (Point x1 y1 z1) (Point x2 y2 z2) =
47 (x2 - x1)^(2::Int) + (y2 - y1)^(2::Int) + (z2 - z1)^(2::Int)