]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Point.hs
Clean up imports/exports.
[spline3.git] / src / Point.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
3 module Point (
4 Point,
5 distance,
6 dot,
7 is_close,
8 scale
9 )
10 where
11
12 import Comparisons ((~=))
13
14
15 type Point = (Double, Double, Double)
16
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)
24
25
26 -- | Scale a point by a constant.
27 scale :: Point -> Double -> Point
28 scale (x, y, z) d = (x*d, y*d, z*d)
29
30
31 -- | Returns the distance between p1 and p2.
32 distance :: Point -> Point -> Double
33 distance p1 p2 =
34 sqrt $ p1 `dot` p2
35
36
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)
41
42
43 -- | Returns 'True' if p1 is close to (within 'epsilon' of) p2,
44 -- 'False' otherwise.
45 is_close :: Point -> Point -> Bool
46 is_close p1 p2 = (distance p1 p2) ~= 0