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