1 {-# LANGUAGE FlexibleInstances #-}
9 import Test.Tasty.QuickCheck ( Arbitrary(..) )
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.
16 Point !Double !Double !Double
20 instance Arbitrary Point where
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)
33 Point coord coord coord
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)
43 -- | Returns the dot product of two points (taken as three-vectors).
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)