]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Point.hs
95b33640f4e81406f71a685c617a3c273a2a6284
[spline3.git] / src / Point.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
3 module Point
4 where
5
6 import Comparisons
7
8
9 type Point = (Double, Double, Double)
10
11 instance Num Point where
12 (x1,y1,z1) + (x2,y2,z2) = (x1+x2, y1+y2, z1+z2)
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 abs (x, y, z) = (abs x, abs y, abs z)
16 signum (x, y, z) = (signum x, signum y, signum z)
17 fromInteger n = (fromInteger n, fromInteger n, fromInteger n)
18
19
20 -- | Scale a point by a constant.
21 scale :: Point -> Double -> Point
22 scale (x, y, z) d = (x*d, y*d, z*d)
23
24
25 -- | Returns the distance between p1 and p2.
26 distance :: Point -> Point -> Double
27 distance p1 p2 =
28 sqrt $ p1 `dot` p2
29
30
31 -- | Returns the dot product of two points (taken as three-vectors).
32 dot :: Point -> Point -> Double
33 dot (x1, y1, z1) (x2, y2, z2) =
34 (x2 - x1)^(2::Int) + (y2 - y1)^(2::Int) + (z2 - z1)^(2::Int)
35
36
37 -- | Returns 'True' if p1 is close to (within 'epsilon' of) p2,
38 -- 'False' otherwise.
39 is_close :: Point -> Point -> Bool
40 is_close p1 p2 = (distance p1 p2) ~= 0