Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Andrew's monotone chain hull over 8 integer points: pts = [(0,0); (1,1); (2,0); (2,2); (0,2); (1,0); (3,3); (5,1)] Sort lex, build lower hull L->R then upper R->L, popping while the cross product is non-positive (collinear included on hull). Hull traverse: (0,0) -> (2,0) -> (5,1) -> (3,3) -> (0,2) = 5 ((2,0) lies on the lower edge from (0,0) to (5,1)). Tests List.sort with 2-tuple comparator using nested pair destructure, repeated `let (x, y) = arr.(i) in` array tuple destructure across both passes, while + cont-flag pattern. 170 baseline programs total.
44 lines
1.1 KiB
OCaml
44 lines
1.1 KiB
OCaml
let cross ox oy ax ay bx by =
|
|
(ax - ox) * (by - oy) - (ay - oy) * (bx - ox)
|
|
|
|
let hull_size pts =
|
|
let n = List.length pts in
|
|
if n < 3 then n
|
|
else begin
|
|
let sorted = List.sort (fun (a, b) (c, d) ->
|
|
if a <> c then compare a c else compare b d) pts in
|
|
let arr = Array.of_list sorted in
|
|
let h = Array.make (2 * n) (0, 0) in
|
|
let k = ref 0 in
|
|
for i = 0 to n - 1 do
|
|
let (xi, yi) = arr.(i) in
|
|
let cont = ref true in
|
|
while !cont && !k >= 2 do
|
|
let (ox, oy) = h.(!k - 2) in
|
|
let (ax, ay) = h.(!k - 1) in
|
|
if cross ox oy ax ay xi yi <= 0 then k := !k - 1
|
|
else cont := false
|
|
done;
|
|
h.(!k) <- (xi, yi);
|
|
k := !k + 1
|
|
done;
|
|
let lo = !k + 1 in
|
|
for i = n - 2 downto 0 do
|
|
let (xi, yi) = arr.(i) in
|
|
let cont = ref true in
|
|
while !cont && !k >= lo do
|
|
let (ox, oy) = h.(!k - 2) in
|
|
let (ax, ay) = h.(!k - 1) in
|
|
if cross ox oy ax ay xi yi <= 0 then k := !k - 1
|
|
else cont := false
|
|
done;
|
|
h.(!k) <- (xi, yi);
|
|
k := !k + 1
|
|
done;
|
|
!k - 1
|
|
end
|
|
|
|
;;
|
|
|
|
hull_size [(0, 0); (1, 1); (2, 0); (2, 2); (0, 2); (1, 0); (3, 3); (5, 1)]
|