]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Point.hs
fd3ac58dec3ceef5dfd5c8aa1615e25a9bc0af53
[spline3.git] / src / Point.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
3 module Point (
4 Point,
5 dot,
6 scale
7 )
8 where
9
10 type Point = (Double, Double, Double)
11
12 instance Num Point where
13 (x1,y1,z1) + (x2,y2,z2) = (x1+x2, y1+y2, z1+z2)
14 (x1,y1,z1) - (x2,y2,z2) = (x1-x2, y1-y2, z1-z2)
15 (x1,y1,z1) * (x2,y2,z2) = (x1*x2, y1*y2, z1*z2)
16 abs (x, y, z) = (abs x, abs y, abs z)
17 signum (x, y, z) = (signum x, signum y, signum z)
18 fromInteger n = (fromInteger n, fromInteger n, fromInteger n)
19
20
21 -- | Scale a point by a constant.
22 scale :: Point -> Double -> Point
23 scale (x, y, z) d = (x*d, y*d, z*d)
24
25
26 -- | Returns the dot product of two points (taken as three-vectors).
27 {-# INLINE dot #-}
28 dot :: Point -> Point -> Double
29 dot (x1, y1, z1) (x2, y2, z2) =
30 (x2 - x1)^(2::Int) + (y2 - y1)^(2::Int) + (z2 - z1)^(2::Int)