]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Point.hs
More doc updates.
[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 p1 + p2 = (x1+x2, y1+y2, z1+z2)
22 where
23 x1 = x_coord p1
24 x2 = x_coord p2
25 y1 = y_coord p1
26 y2 = y_coord p2
27 z1 = z_coord p1
28 z2 = z_coord p2
29
30 p1 - p2 = (x1-x2, y1-y2, z1-z2)
31 where
32 x1 = x_coord p1
33 x2 = x_coord p2
34 y1 = y_coord p1
35 y2 = y_coord p2
36 z1 = z_coord p1
37 z2 = z_coord p2
38
39 p1 * p2 = (x1*x2, y1*y2, z1*z2)
40 where
41 x1 = x_coord p1
42 x2 = x_coord p2
43 y1 = y_coord p1
44 y2 = y_coord p2
45 z1 = z_coord p1
46 z2 = z_coord p2
47
48 abs (x, y, z) = (abs x, abs y, abs z)
49 signum (x, y, z) = (signum x, signum y, signum z)
50 fromInteger n = (fromInteger n, fromInteger n, fromInteger n)
51
52
53 -- | Scale a point by a constant.
54 scale :: Point -> Double -> Point
55 scale (x, y, z) d = (x*d, y*d, z*d)
56
57
58 -- | Returns the distance between p1 and p2.
59 distance :: Point -> Point -> Double
60 distance p1 p2 =
61 sqrt $ (x2 - x1)^(2::Int) + (y2 - y1)^(2::Int) + (z2 - z1)^(2::Int)
62 where
63 x1 = x_coord p1
64 x2 = x_coord p2
65 y1 = y_coord p1
66 y2 = y_coord p2
67 z1 = z_coord p1
68 z2 = z_coord p2
69
70
71 -- | Returns 'True' if p1 is close to (within 'epsilon' of) p2,
72 -- 'False' otherwise.
73 is_close :: Point -> Point -> Bool
74 is_close p1 p2 = (distance p1 p2) ~= 0