EdgarasLegus has asked for the wisdom of the Perl Monks concerning the following question:

Hey can somebody properly connect thi code to Perl ? Code used from https://www.sanfoundry.com/java-program-to-implement-jarvis-algorithm/
** ** Java Program to Implement Jarvis Algorithm **/ import java.util.Scanner; import java.util.Arrays; /** Class point **/ class Point { int x, y; } /** Class Jarvis **/ public class Jarvis { private boolean CCW(Point p, Point q, Point r) { int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y +); if (val >= 0) return false; return true; } public void convexHull(Point[] points) { int n = points.length; /** if less than 3 points return **/ if (n < 3) return; int[] next = new int[n]; Arrays.fill(next, -1); /** find the leftmost point **/ int leftMost = 0; for (int i = 1; i < n; i++) if (points[i].x < points[leftMost].x) leftMost = i; int p = leftMost, q; /** iterate till p becomes leftMost **/ do { /** wrapping **/ q = (p + 1) % n; for (int i = 0; i < n; i++) if (CCW(points[p], points[i], points[q])) q = i; next[p] = q; p = q; } while (p != leftMost); /** Display result **/ display(points, next); } public void display(Point[] points, int[] next) { System.out.println("\nConvex Hull points : "); for (int i = 0; i < next.length; i++) if (next[i] != -1) System.out.println("("+ points[i].x +", "+ points[i].y ++")"); } /** Main function **/ public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Jarvis Algorithm Test\n"); /** Make an object of Jarvis class **/ Jarvis j = new Jarvis(); System.out.println("Enter number of points n :"); int n = scan.nextInt(); Point[] points = new Point[n]; System.out.println("Enter "+ n +" x, y cordinates"); for (int i = 0; i < n; i++) { points[i] = new Point(); points[i].x = scan.nextInt(); points[i].y = scan.nextInt(); } j.convexHull(points); } }

Replies are listed 'Best First'.
Re: Convert Java to Perl
by stevieb (Canon) on Jun 01, 2018 at 13:09 UTC

    Welcome to the Monastery, EdgarasLegus!

    "Hey can somebody properly connect this code to Perl?"

    Probably, but you'd be better served by showing what code you've got that you've already tried. This is not a free code writing service here.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Convert Java to Perl
by choroba (Cardinal) on Jun 01, 2018 at 15:06 UTC
    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,
Re: Convert Java to Perl
by thanos1983 (Parson) on Jun 01, 2018 at 14:35 UTC

    Hello EdgarasLegus,

    Welcome to the Monastery. Have you read this question, check this question converting java code to perl maybe it will help you.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!