Skip to content
Newer
Older
100644 86 lines (72 sloc) 2.31 KB
3fe68b8 first commit
Brian Rogoff authored
1
2 module TwoD =
3 struct
4 type real = float
5 type point = float * float
6 type elt = point
7 type interval = float * float
8 type range = (float * float) array
9
10 let noninterval (x0, x1) = x1 > x0
11
12 let add_loc ((x0, x1) as interval) x =
13 if x0 < x1 then
14 if x < x0 then
15 (x,x1)
16 else if x > x1 then
17 (x0,x)
18 else
19 interval
20 else if x0 = x1 then
21 if x > x0 then (x0,x) else if x < x0 then (x,x0) else interval
22 else (* x0 > x1, invalid range *)
23 (x,x)
24
25 let dim = 2
26 let null_interval = (Pervasives.max_float, Pervasives.min_float)
27 let null_range = Array.create dim null_interval
28
29 let range_maker r (f0,f1) =
30 let mapf i interval = if i = 0 then add_loc interval f0 else add_loc interval f1 in
31 Array.mapi mapf r
32
33 let intersect_intervals (x00,x01) (x10,x11) =
34 if x01 < x10 || x11 < x00 then
35 null_interval
36 else
37 (max x00 x10, min x01 x11)
38
39 let is_valid_interval (x0,x1) = x0 <= x1
40
41 let intersect_ranges r0 r1 =
42 let ((x00,x01) as x0_interval) = Array.get r0 0 in
43 let ((y00,y01) as y0_interval) = Array.get r0 1 in
44 let ((x10,x11) as x1_interval) = Array.get r1 0 in
45 let ((y10,y11) as y1_interval) = Array.get r1 1 in
46 let ivi = is_valid_interval in
47 if ivi x0_interval && ivi x1_interval && ivi y0_interval && ivi y1_interval then
48 let x_interval = intersect_intervals x0_interval x1_interval in
49 let y_interval = intersect_intervals y0_interval y1_interval in
50 if ivi x_interval && ivi y_interval then
51 [|x_interval; y_interval|]
52 else
53 null_range
54 else
55 null_range
56
57 let point_in_range r (x,y) =
58 let (x0,x1) = Array.get r 0 in
59 let (y0,y1) = Array.get r 1 in
60 x >= x0 && x <= x1 && y >= y0 && y <= y1
61
62 let to_point e = e
63 let square x = x *. x
64
65 let axial_compare n (x0,y0) (x1,y1) =
66 if n = 0 then
67 Pervasives.compare x0 x1
68 else if n = 1 then
69 Pervasives.compare y0 y1
70 else
71 invalid_arg ("TwoD.select: " ^ (Pervasives.string_of_int n))
72
73 let squared_distance (x0,y0) (x1,y1) =
74 let dx = x1 -. x0 in
75 let dy = y1 -. y0 in
76 dx *. dx +. dy *. dy
77
78 let squared_axial_distance n (x0,y0) (x1,y1) =
79 if n = 0 then
80 square (x0 -. x1)
81 else if n = 1 then
82 square (y0 -. y1)
83 else
84 invalid_arg ("TwoD.select: " ^ (Pervasives.string_of_int n))
85 end;;
Something went wrong with that request. Please try again.