1 {-# LANGUAGE FlexibleInstances #-}
12 import Comparisons ((~=))
15 type Point = (Double, Double, Double)
17 instance Num Point where
18 (x1,y1,z1) + (x2,y2,z2) = (x1+x2, y1+y2, z1+z2)
19 (x1,y1,z1) - (x2,y2,z2) = (x1-x2, y1-y2, z1-z2)
20 (x1,y1,z1) * (x2,y2,z2) = (x1*x2, y1*y2, z1*z2)
21 abs (x, y, z) = (abs x, abs y, abs z)
22 signum (x, y, z) = (signum x, signum y, signum z)
23 fromInteger n = (fromInteger n, fromInteger n, fromInteger n)
26 -- | Scale a point by a constant.
27 scale :: Point -> Double -> Point
28 scale (x, y, z) d = (x*d, y*d, z*d)
31 -- | Returns the distance between p1 and p2.
32 distance :: Point -> Point -> Double
37 -- | Returns the dot product of two points (taken as three-vectors).
38 dot :: Point -> Point -> Double
39 dot (x1, y1, z1) (x2, y2, z2) =
40 (x2 - x1)^(2::Int) + (y2 - y1)^(2::Int) + (z2 - z1)^(2::Int)
43 -- | Returns 'True' if p1 is close to (within 'epsilon' of) p2,
45 is_close :: Point -> Point -> Bool
46 is_close p1 p2 = (distance p1 p2) ~= 0