object serve? It shouldn't exist, and all the methods in the Jarvis class should be static.Jarvis j
Anyway, almost 1:1 translation. There might be better ways to do it, though:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package Point; use Moo; use Types::Standard qw( Int ); has [qw[ x y ]] => (is => 'ro', isa => Int); } { package Jarvis; sub _ccw { my ($p, $q, $r) = @_; return ( ($q->y - $p->y) * ($r->x - $q->x) - ($q->x - $p->x) * ($r->y - $q->y) ) < 0 } sub convexHull { my ($points) = @_; return if @$points < 3; my @next = (-1) x @$points; my $leftmost = 0; for my $i (1 .. @$points - 1) { $leftmost = $i if $points->[$i]->x < $points->[$leftmost]- +>x; } my $p = $leftmost; my $q; do { $q = ($p + 1) % @$points; for my $j (0 .. @$points - 1) { $q = $j if _ccw($points->[$p], $points->[$j], $points- +>[$q]); } $next[$p] = $q; $p = $q; } while $p != $leftmost; display($points, \@next); } sub display { my ($points, $next) = @_; say "\nConvex Hull points:"; for my $i (grep $next->[$_] != -1, 0 .. $#$next) { say '(', $points->[$i]->x, ', ', $points->[$i]->y, ')' } } } say 'Jarvis Algorithm Test'; say 'Enter number of points n:'; chomp( my $n = <> ); say "Enter $n x, y cordinates"; my @points; while (@points < $n) { my ($x, $y) = split ' ', <>; push @points, 'Point'->new(x => $x, y => $y); } Jarvis::convexHull(\@points);
Update
The code above didn't implement the stream, it read the input in the form
only. You can fix that by installing something like File::Stream, but I don't know it, so I wrote a simple stream myself:1 2 3 4 5 6
{ package Stream; use Moo; has fh => (is => 'ro'); has _buffer => (is => 'rw', default => ""); sub next_token { my ($self) = @_; my $buffer = $self->_buffer; until ($buffer =~ /\S/) { $buffer = readline $self->fh; die "End of file" unless defined $buffer; } $buffer =~ s/(\S+)//; my $token = "$1"; $self->_buffer($buffer); return $token } }
and then use it when reading the input:
my $input = 'Stream'->new(fh => *STDIN); while (@points < $n) { my ($x, $y) = map $input->next_token, 1, 2; push @points, 'Point'->new(x => $x, y => $y); }
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
In reply to Re: Convert Java to Perl
by choroba
in thread Convert Java to Perl
by EdgarasLegus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |