]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Point.hs
Fix some hlint warnings.
[spline3.git] / src / Point.hs
1 {-# LANGUAGE TypeSynonymInstances #-}
2
3 module Point
4 where
5
6 import Comparisons
7
8
9 type Point = (Double, Double, Double)
10
11 x_coord :: Point -> Double
12 x_coord (x, _, _) = x
13
14 y_coord :: Point -> Double
15 y_coord (_, y, _) = y
16
17 z_coord :: Point -> Double
18 z_coord (_, _, z) = z
19
20 instance Num Point where
21 (x1,y1,z1) + (x2,y2,z2) = (x1+x2, y1+y2, z1+z2)
22 (x1,y1,z1) - (x2,y2,z2) = (x1-x2, y1-y2, z1-z2)
23 (x1,y1,z1) * (x2,y2,z2) = (x1*x2, y1*y2, z1*z2)
24 abs (x, y, z) = (abs x, abs y, abs z)
25 signum (x, y, z) = (signum x, signum y, signum z)
26 fromInteger n = (fromInteger n, fromInteger n, fromInteger n)
27
28
29 -- | Scale a point by a constant.
30 scale :: Point -> Double -> Point
31 scale (x, y, z) d = (x*d, y*d, z*d)
32
33
34 -- | Returns the distance between p1 and p2.
35 distance :: Point -> Point -> Double
36 distance p1 p2 =
37 sqrt $ (x2 - x1)^(2::Int) + (y2 - y1)^(2::Int) + (z2 - z1)^(2::Int)
38 where
39 x1 = x_coord p1
40 x2 = x_coord p2
41 y1 = y_coord p1
42 y2 = y_coord p2
43 z1 = z_coord p1
44 z2 = z_coord p2
45
46
47 -- | Returns 'True' if p1 is close to (within 'epsilon' of) p2,
48 -- 'False' otherwise.
49 is_close :: Point -> Point -> Bool
50 is_close p1 p2 = (distance p1 p2) ~= 0