What purpose does the
Jarvis j
object serve? It shouldn't exist, and all the methods in the Jarvis class should be static.

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

1 2 3 4 5 6
only. You can fix that by installing something like File::Stream, but I don't know it, so I wrote a simple stream myself:
{ 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.