aboutsummaryrefslogtreecommitdiff
path: root/path/fixed.ml
diff options
context:
space:
mode:
Diffstat (limited to 'path/fixed.ml')
-rwxr-xr-xpath/fixed.ml27
1 files changed, 21 insertions, 6 deletions
diff --git a/path/fixed.ml b/path/fixed.ml
index 2d42566..176d818 100755
--- a/path/fixed.ml
+++ b/path/fixed.ml
@@ -76,6 +76,9 @@ module Make(Point:P) = struct
: int * path list -> path array
= fun (n, t) ->
+ (* The array is initialized with a magic number, and just after
+ filled with the values from the list in reverse. All the elements are set.
+ *)
let res = Obj.magic (Array.make n 0) in
List.iteri t
~f:(fun i elem -> Array.set res (n - i - 1) elem );
@@ -125,10 +128,18 @@ module Make(Point:P) = struct
) in
Repr.stop repr
+
+ type approx =
+ { distance : float
+ ; closest_point : Gg.v2
+ ; ratio : float
+ ; p0 : Point.t
+ ; p1 : Point.t }
+
(** Return the distance between a given point and the curve. May return
None if the point is out of the curve *)
let distance
- : Gg.v2 -> t -> (Gg.v2 * float * Point.t * Point.t) option
+ : Gg.v2 -> t -> approx option
= fun point beziers ->
Array.fold_left beziers.path
@@ -151,12 +162,16 @@ module Make(Point:P) = struct
; ctrl0 = bezier.ctrl0
; ctrl1 = bezier.ctrl1 }
) in
- let _, point' = Shapes.Bezier.get_closest_point point bezier' in
- let distance = Gg.V2.( norm (point - point') ) in
+ let ratio, point' = Shapes.Bezier.get_closest_point point bezier' in
+ let distance' = Gg.V2.( norm (point - point') ) in
match res with
- | None -> Some (point', distance, bezier.p0, bezier.p1)
- | Some (_, d, _, _) when d < distance -> res
- | _ -> (Some (point', distance, bezier.p0, bezier.p1))
+ | Some {distance; _} when distance < distance' -> res
+ | _ -> Some
+ { closest_point = point'
+ ; distance = distance'
+ ; p0 = bezier.p0
+ ; p1 = bezier.p1
+ ; ratio }
)
let map_point