caml2geom / test1.ml

open Caml2geom
open Printf

(*-----------------------------------------------------------------------*)

let _ = printf "Coord.epsilon = %f\n" (Coord.epsilon) ;;
let _ = printf "Coord.infinity () = %f\n" (Coord.infinity ()) ;;
let _ = printf "Coord.are_near 4.00 4.09 = %B\n" (Coord.are_near 4.00 4.09) ;;
let _ = printf "Coord.are_near_eps 0.1 4.00 4.09 = %B\n" (Coord.are_near_eps 0.1 4.00 4.09);;
let _ = printf "Coord.rel_error_bound 4.00 4.09 = %B\n" (Coord.rel_error_bound 4.00 4.09) ;;
let _ = printf "Coord.rel_error_bound_eps 0.1 4.00 4.09 = %B\n" (Coord.rel_error_bound_eps 0.1 4.00 4.09);;

(*-----------------------------------------------------------------------*)

let c = Coord.epsilon

let p = Point.copy (Point.make 0. 0.) ;;
Point.set_coord p 1 50. ;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
Point.set_coord p 0 10. ;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
Point.normalize p ;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
let p = Point.ccw p ;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
let p = Point.cw p ;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
let p = Point.neg p ;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
Point.add_to p (Point.make 100. 100.);;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
Point.sub_from p (Point.make 100. 100.);;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
Point.mul_by p 1000.;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
Point.div_by p 1000.;;
let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;

let q = Point.make 0. 0.;;
let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
let q = Point.default ();;
let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
Point.set q (Point.make (-50.) 50.);;
let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;

(*-----------------------------------------------------------------------*)

let bez = Bezier.make_order3 0. 10. 15. 1. ;;
let _ = printf "at0 -> %f\n" (Bezier.at0 bez) ;;
let _ = printf "at1 -> %f\n" (Bezier.at1 bez) ;;
let _ = printf "at 0.25 -> %f\n" (Bezier.value_at bez 0.25) ;;
let _ = printf "at 1/3 -> %f\n" (Bezier.value_at bez (1./.3.)) ;;
let _ = printf "at 0.5 -> %f\n" (Bezier.value_at bez 0.5) ;;
let _ = printf "at 2/3 -> %f\n" (Bezier.value_at bez (2./.3.)) ;;
let _ = printf "at 0.75 -> %f\n" (Bezier.value_at bez 0.75) ;;

(*-----------------------------------------------------------------------*)

let bc1 =
  Bezier_curve.of_four_points (Point.make (-1.) (-3.)) (Point.make 10. 10.) (Point.make 15. 5.) (Point.make 20. 1.) ;;
let _ =
  printf "initial_point = (%f,%f)\n" (Point.coord (Bezier_curve.initial_point bc1) 0)
    (Point.coord (Bezier_curve.initial_point bc1) 1) ;;
let _ =
  printf "final_point = (%f,%f)\n" (Point.coord (Bezier_curve.final_point bc1) 0)
    (Point.coord (Bezier_curve.final_point bc1) 1) ;;
let _ =
  printf "point_at 0.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc1 0.0) 0)
    (Point.coord (Bezier_curve.point_at bc1 0.0) 1) ;;
let _ =
  printf "point_at 0.4 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc1 0.4) 0)
    (Point.coord (Bezier_curve.point_at bc1 0.4) 1) ;;
let _ =
  printf "point_at 1.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc1 1.0) 0)
    (Point.coord (Bezier_curve.point_at bc1 1.0) 1) ;;
let _ =
  printf "value_at 0.0 = (%f,%f)\n" (Bezier_curve.value_at bc1 0.0 Coord.X)
    (Bezier_curve.value_at bc1 0.0 Coord.Y) ;;
let _ =
  printf "value_at 0.4 = (%f,%f)\n" (Bezier_curve.value_at bc1 0.4 Coord.X)
    (Bezier_curve.value_at bc1 0.4 Coord.Y) ;;
let _ =
  printf "value_at 1.0 = (%f,%f)\n" (Bezier_curve.value_at bc1 1.0 Coord.X)
    (Bezier_curve.value_at bc1 1.0 Coord.Y) ;;
let _ =
  printf "value_at 0.0 = (%f,%f)\n" (Bezier_curve.value_at (Bezier_curve.reverse bc1) 0.0 Coord.X)
    (Bezier_curve.value_at (Bezier_curve.reverse bc1) 0.0 Coord.Y) ;;
let _ =
  printf "value_at 0.4 = (%f,%f)\n" (Bezier_curve.value_at (Bezier_curve.reverse bc1) 0.4 Coord.X)
    (Bezier_curve.value_at (Bezier_curve.reverse bc1) 0.4 Coord.Y) ;;
let _ =
  printf "value_at 1.0 = (%f,%f)\n" (Bezier_curve.value_at (Bezier_curve.reverse bc1) 1.0 Coord.X)
    (Bezier_curve.value_at (Bezier_curve.reverse bc1) 1.0 Coord.Y) ;;
let _ =
  printf "point_at 0.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at (Bezier_curve.portion bc1 0. 0.4) 0.0) 0)
    (Point.coord (Bezier_curve.point_at (Bezier_curve.portion bc1 0. 0.4) 0.0) 1) ;;
let _ =
  printf "point_at 0.4 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at (Bezier_curve.portion bc1 0. 0.4) 0.4) 0)
    (Point.coord (Bezier_curve.point_at (Bezier_curve.portion bc1 0. 0.4) 0.4) 1) ;;
let _ =
  printf "point_at 1.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at (Bezier_curve.portion bc1 0. 0.4) 1.0) 0)
    (Point.coord (Bezier_curve.point_at (Bezier_curve.portion bc1 0. 0.4) 1.0) 1) ;;
let _ =
  printf "value_at 0.0 = (%f,%f)\n" (Bezier_curve.value_at (Bezier_curve.derivative bc1) 0.0 Coord.X)
    (Bezier_curve.value_at (Bezier_curve.derivative bc1) 0.0 Coord.Y) ;;
let _ =
  printf "value_at 0.4 = (%f,%f)\n" (Bezier_curve.value_at (Bezier_curve.derivative bc1) 0.4 Coord.X)
    (Bezier_curve.value_at (Bezier_curve.derivative bc1) 0.4 Coord.Y) ;;
let _ =
  printf "value_at 1.0 = (%f,%f)\n" (Bezier_curve.value_at (Bezier_curve.derivative bc1) 1.0 Coord.X)
    (Bezier_curve.value_at (Bezier_curve.derivative bc1) 1.0 Coord.Y) ;;
let bc2 =
  Bezier_curve.of_four_points (Point.make (1.) (1.)) (Point.make 10. 10.) (Point.make 15. 15.) (Point.make 20. 20.) ;;
let bc3 = Bezier_curve.of_two_points (Point.make (1.) (1.)) (Point.make 10. 10.) ;;
let bc4 =
  Bezier_curve.of_four_points (Point.make (1.) (1.)) (Point.make 1. 1.) (Point.make 1. 1.) (Point.make 1. 1.) ;;
let _ = printf "bc1 is_degenerate = %B\n" (Bezier_curve.is_degenerate bc1) ;;
let _ = printf "bc2 is_degenerate = %B\n" (Bezier_curve.is_degenerate bc2) ;;
let _ = printf "bc3 is_degenerate = %B\n" (Bezier_curve.is_degenerate bc3) ;;
let _ = printf "bc4 is_degenerate = %B\n" (Bezier_curve.is_degenerate bc4) ;;
let p_array = Bezier_curve.point_and_derivatives bc1 1.0 3 ;;
Array.iteri (fun i p -> printf "deriv %d (%f,%f)\n" i (Point.coord p 0) (Point.coord p 1)) p_array ;;
let p_array = Bezier_curve.point_and_derivatives bc2 1.0 3 ;;
Array.iteri (fun i p -> printf "deriv %d (%f,%f)\n" i (Point.coord p 0) (Point.coord p 1)) p_array ;;
let p_array = Bezier_curve.point_and_derivatives bc3 1.0 3 ;;
Array.iteri (fun i p -> printf "deriv %d (%f,%f)\n" i (Point.coord p 0) (Point.coord p 1)) p_array ;;
let p_array = Bezier_curve.point_and_derivatives bc4 1.0 3 ;;
Array.iteri (fun i p -> printf "deriv %d (%f,%f)\n" i (Point.coord p 0) (Point.coord p 1)) p_array ;;
let _ = printf "bc1 length = %f\n" (Bezier_curve.length bc1 0.01) ;;
let _ = printf "bc2 length = %f\n" (Bezier_curve.length bc2 0.01) ;;
let _ = printf "bc3 length = %f\n" (Bezier_curve.length bc3 0.01) ;;
let _ = printf "bc4 length = %f\n" (Bezier_curve.length bc4 0.01) ;;
let p1 = Point.make (-400.) (-200.) ;;
Bezier_curve.set_initial bc4 p1 ;;
let _ = printf "point_at 0.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc4 0.0) 0)
  (Point.coord (Bezier_curve.point_at bc4 0.0) 1) ;;
Point.set_coord p1 1 200. ;;
let _ = printf "point_at 0.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc4 0.0) 0)
  (Point.coord (Bezier_curve.point_at bc4 0.0) 1) ;;
Bezier_curve.set_initial bc4 p1 ;;
let _ = printf "point_at 0.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc4 0.0) 0)
  (Point.coord (Bezier_curve.point_at bc4 0.0) 1) ;;
let p1 = Point.make (500.) (300.) ;;
Bezier_curve.set_final bc4 p1 ;;
let _ = printf "point_at 1.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc4 1.0) 0)
  (Point.coord (Bezier_curve.point_at bc4 1.0) 1) ;;
Point.set_coord p1 1 (-300.) ;;
let _ = printf "point_at 1.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc4 1.0) 0)
  (Point.coord (Bezier_curve.point_at bc4 1.0) 1) ;;
Bezier_curve.set_final bc4 p1 ;;
let _ = printf "point_at 1.0 = (%f,%f)\n" (Point.coord (Bezier_curve.point_at bc4 1.0) 0)
  (Point.coord (Bezier_curve.point_at bc4 1.0) 1) ;;

let _ = printf "bc1 degrees_of_freedom = %d\n" (Bezier_curve.degrees_of_freedom bc1) ;;
let _ = printf "bc2 degrees_of_freedom = %d\n" (Bezier_curve.degrees_of_freedom bc2) ;;
let _ = printf "bc3 degrees_of_freedom = %d\n" (Bezier_curve.degrees_of_freedom bc3) ;;
let _ = printf "bc4 degrees_of_freedom = %d\n" (Bezier_curve.degrees_of_freedom bc4) ;;

let _ = printf "bc1 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc1 0.4) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc1 0.4) 1) ;;
let _ = printf "bc2 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc2 0.4) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc2 0.4) 1) ;;
let _ = printf "bc3 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc3 0.4) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc3 0.4) 1) ;;
let _ = printf "bc4 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc4 0.4) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc4 0.4) 1) ;;

let _ = printf "bc1 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc1 1.0) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc1 1.0) 1) ;;
let _ = printf "bc2 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc2 1.0) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc2 1.0) 1) ;;
let _ = printf "bc3 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc3 1.0) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc3 1.0) 1) ;;
let _ = printf "bc4 unit_tangent_at = (%f,%f)\n"
  (Point.coord (Bezier_curve.unit_tangent_at bc4 1.0) 0)
  (Point.coord (Bezier_curve.unit_tangent_at bc4 1.0) 1) ;;

let _ = printf "winding: %d\n" (Bezier_curve.winding bc1 (Point.make 4. 36.)) ;;

(*-----------------------------------------------------------------------*)

let path1 = Path.default () ;;
let path2 = Path.make (Point.make 10. 10.) ;;
let _ = printf "path2 point_at 0.0 = (%f,%f)\n"
  (Point.coord (Path.point_at path2 0.0) 0)
  (Point.coord (Path.point_at path2 0.0) 1) ;;
let _ = printf "path2 point_at 0.0 = (%f,%f)\n"
  (Path.value_at path2 0.0 Coord.X)
  (Path.value_at path2 0.0 Coord.Y) ;;
let path3 = Path.portion path2 0.0 0.0 ;;
let _ = printf "path3 point_at 0.0 = (%f,%f)\n"
  (Point.coord (Path.point_at path3 0.0) 0)
  (Point.coord (Path.point_at path3 0.0) 1) ;;
let path4 = Path.portion path2 0.0 1.0 ;;
let _ = printf "path4 point_at 0.0 = (%f,%f)\n"
  (Point.coord (Path.point_at path4 0.0) 0)
  (Point.coord (Path.point_at path4 0.0) 1) ;;
let path5 = Path.make (Bezier_curve.initial_point bc1) ;;
Path.append_curve ~stitch:Path.STITCH_DISCONTINUOUS path5 (Bezier_curve.to_curve bc1) ;;
Path.append_curve ~stitch:Path.STITCH_DISCONTINUOUS path5 (Bezier_curve.to_curve bc2) ;;
let _ = printf "path5 point_at 0.5 = (%f,%f)\n"
  (Point.coord (Path.point_at path5 0.5) 0)
  (Point.coord (Path.point_at path5 0.5) 1) ;;
Path.append ~stitch:Path.STITCH_DISCONTINUOUS path5 path5 ;;
let _ = printf "path5 point_at 4.5 = (%f,%f)\n"
  (Point.coord (Path.point_at path5 4.5) 0)
  (Point.coord (Path.point_at path5 4.5) 1) ;;
let _ = printf "path1 empty = %B\n" (Path.empty path1) ;;
let _ = printf "path5 empty = %B\n" (Path.empty path5) ;;
let _ = printf "path1 closed = %B\n" (Path.closed path1) ;;
let _ = printf "path5 closed = %B\n" (Path.closed path5) ;;
Path.close path5 true ;;
let _ = printf "path5 closed = %B\n" (Path.closed path5) ;;
let _ = printf "path5 size_open = %d\n" (Path.size_open path5) ;;
let _ = printf "path5 size_closed = %d\n" (Path.size_closed path5) ;;
let _ = printf "path5 size_default = %d\n" (Path.size_default path5) ;;
let _ = printf "path5 size = %d\n" (Path.size path5) ;;
let _ = printf "path5 max_size = %d\n" (Path.max_size path5) ;;
let (t,d_squared) = Path.nearest_point path5 (Point.make 0. 0.) ;;
let _ = printf "path5 nearest_point to (0.,0.) = %f %f\n" t d_squared ;;
let (t,d_squared) = Path.nearest_point path5 (Point.make 100. 100.) ;;
let _ = printf "path5 nearest_point to (100.,100.) = %f %f\n" t d_squared ;;
let (t,d_squared) = Path.nearest_point ~from_t:0.4 ~to_t:0.9 (Path.portion path5 0.5 0.9) (Point.make 0. 0.) ;;
let _ = printf "path5 portion nearest_point to (0.,0.) = %f %f\n" t d_squared ;;
let (t,d_squared) = Path.nearest_point ~from_t:0.4 ~to_t:0.9 path5 (Point.make 0. 0.) ;;
let _ = printf "path5 nearest_point to (0.,0.) = %f %f\n" t d_squared ;;
let (t,d_squared) = Path.nearest_point ~from_t:2.4 ~to_t:2.9 path5 (Point.make 0. 0.) ;;
let _ = printf "path5 nearest_point to (0.,0.) = %f %f\n" t d_squared ;;
let (t,d_squared) = Path.nearest_point ~from_t:0.4 ~to_t:0.9 path5 (Point.make 100. 100.) ;;
let _ = printf "path5 nearest_point to (100.,100.) = %f %f\n" t d_squared ;;
let bc_a = Bezier_curve.of_tuple_array [|(1.,2.);(10.,5.);(50.,2.)|] ;;
let t_a = Bezier_curve.to_tuple_array bc_a ;;
print_string "[|"; Array.iter (fun (x,y) -> printf "(%f,%f);" x y) t_a; print_endline "|]" ;;
let _ = printf "bc_a order = %d\n" (Bezier_curve.order bc_a) ;;
let path_a = Path.make (Point.make 1. 2.) ;;
Path.append_curve path_a (Bezier_curve.to_curve bc_a) ;;
let (t,d_squared) = Path.nearest_point ~from_t:0.4 ~to_t:0.9 path_a (Point.make 0. 0.) ;;
let _ = printf "path_a nearest_point to (0.,0.) = %f %f\n" t d_squared ;;

let beziers = Path.to_cubic_beziers path5 0.001 ;;
List.iter
  (fun bez ->
    let tuples = Cubic_bezier.to_tuple_array bez in
    print_string "[|";
    Array.iter (fun (x,y) -> printf "(%f,%f);" x y) tuples;
    print_endline "|]")
beziers ;;

(*-----------------------------------------------------------------------*)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.