%!PS % begin included library code % see https://codeberg.org/Firedrake/postscript-libraries/ /enumerate.array { 1 dict begin /a exch def [ 0 1 a length 1 sub { [ exch dup a exch get ] } for ] end } bind def /test { /test.count test.count 1 add def { /test.pass test.pass 1 add def } { ( ) print test.count (....) cvs print (-fail) print } ifelse } bind def /test.start { print (:) print /test.pass 0 def /test.count 0 def } bind def /test.end { ( ) print test.count 0 gt { (Passed ) print test.pass (...) cvs print (/) print test.count (...) cvs print ( \() print test.pass 100 mul test.count idiv (...) cvs print (%\)) print (\r\n) print } if } bind def % end included library code /nearestvalidpoint { 0 dict begin 3 1 roll /y exch def /x exch def /minmhd -1 def -1 exch enumerate.array { aload pop /p exch def /i exch def p 0 get x eq p 1 get y eq or { /mhd p 0 get x sub abs p 1 get y sub abs add def minmhd -1 eq mhd minmhd lt or { /minmhd mhd def pop i } if } if } forall end } bind def (nearestvalidpoint) test.start 3 4 [[1 2] [3 1] [2 4] [2 3]] nearestvalidpoint 2 eq test 2 5 [[3 4] [2 3] [1 5] [2 5]] nearestvalidpoint 3 eq test 1 1 [[2 2] [3 3] [4 4]] nearestvalidpoint -1 eq test 0 0 [[0 1] [1 0] [0 2] [2 0]] nearestvalidpoint 0 eq test 5 5 [[5 6] [6 5] [5 4] [4 5]] nearestvalidpoint 0 eq test test.end