class Point { has Num $.x; has Num $.y; my $num_re = rx{ \-? \d+ \. \d+ }; method points ( Str $s ) { return gather for $s.split(' ') { / ^ ($num_re) \, ($num_re) $ / or say 'invalid input' and exit; take Point.new( x => +$0, y => +$1 ); } } # This algorithm is a translation of pnpoly() from the # comp.graphics.algorithms FAQ question q2.03, "How do I find if a point # lies within a polygon?", http://www.faqs.org/faqs/graphics/algorithms-faq/ # It returns True for strictly interior points, False for strictly exterior. # For points on the boundary, use a different algorithm. method in_poly ( $p: @ps ) returns Bool { my Bool $c = False; my Point $j = @ps[*-1]; for @ps -> Point $i { next unless $i.y <= $p.y < $j.y or $j.y <= $p.y < $i.y; $c = !$c if $p.x < $i.x + ($j.x - $i.x) * ($p.y - $i.y) / ($j.y - $i.y); $j = $i; } return $c; } } my @poly = Point.points: $*IN.get; my ($point) = Point.points: $*IN.get; say $point.in_poly(@poly) ?? 'yes' !! 'no';