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

I was playing with some puzzles and I wanted to create a program in perl that given a set of data , it can figure out if it is a rectangle or not.

the fun part is that I am given 3 points, how do I prove a 4th point will make a rectangle?

I tested for parallel sides to be congruent and have a single 90 angle to prove it

my question is there a module in perl that can take a set of points to figure out lengths, angles...too messy with all little calculations to get lengths

I , as always, looking for some advice in making my program more readable and extending my perl experience with modules

# C(cx,cy)* ****** *D(?x,?y) # * * # * * # A(ax,ay)* ****** * B(bx,by) use strict; use warnings; use Data::Dump qw(dump); use Math::Trig; my @points = ( 1,5, #point A 5,5, #point B 1,9, #point C ); my @new =( 5,9); #point D will be given by user later (testing my +theory) #lets' get the length of each segment; if parallel sides are equal #plus #test for 90 angles and IT's a rectangle or square # GET LENGTHS : #from A to B my $ABx= $points[2]-$points[0]; my $ABy= $points[3]-$points[1]; #points from A to C my $ACx= $points[4]-$points[0]; my $ACy= $points[5]-$points[1]; #get length to new points from C TO D my $CDx= $new[0]-$points[4]; my $CDy= $new[1]-$points[5]; #from B to D my $BDx= $new[0]-$points[2]; my $BDy= $new[1]-$points[3]; #test if lengths are the same #if so, then test if all angles are 90 if ($ABx == $CDx && $ACy == $BDy){ #find angle my $a= atan($ABx/$ACy)*(360/pi); $a==90 ? print "this is a Rectangle/square\n" : print "this is a r +hombus of some sort\n"; }else { print " This shape doesn't have congruent sides; NOT a RECTANG +LE or Square";}

Replies are listed 'Best First'.
Re: Is there a Module that can handle a set of data points?
by NetWallah (Canon) on Nov 12, 2015 at 06:41 UTC
    How about Math::Geometry::Planar ?

    It has potentially useful methods like:

    • SegmentLength($p1,$p2);
    • Perpendicular($p1,$p2,$p3,$p4);
    • DistanceToSegment($p1,$p2,$p3);
    • Various Polygon rotation, movement, and mirroring methods

            “The sources of quotes found on the internet are not always reliable.” — Abraham Lincoln.3; cf.

Re: Is there a Module that can handle a set of data points?
by salva (Canon) on Nov 12, 2015 at 08:22 UTC
Re: Is there a Module that can handle a set of data points?
by LanX (Saint) on Nov 12, 2015 at 07:48 UTC
    I'd go with vector calculus to approach this kind of problems.

    A module would need to be able to define vectors as objects and overload operators for addition, subtraction, scalar product and vector product.

    There are plenty of modules in the geometry / algebra / math / pdl namespace, not sure which to recommend, personally I might start with my own toy project to play around with overloading.

    update

    Salva's module looks pretty much how I'd envision it. :)

    (though I might ponder about allowing 2d cross products by adding a third 0 component)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Thanks!

      I am thinking of adding a little bit more of complexity now that you mentioned it( 3 D.) I will start to play with the Math module...maybe more questions on that later. I appreciate the advice

      quick question

      what's the right syntax to have only 3 decimal points for the angle calculation? $a= atan($ABx/$ACy)*(360/pi){3}

      output can be like 90.123

        That's the precise example given at the start of the sprintf documentation.