Seeing this node inspired me to create a program that would allow me to find the hypotenuse of a right triangle when supplied its' A & B. It also works to find B when A & C are known.

I've created two versions:

  • One that runs in a user interface
  • One that runs on the command line
    Here is the one that runs in a user interface. It is simply called as perl hypo.pl.

    #! Perl -w # hypo.pl -- find the hypotenuse use strict; sub compute { my $opt = shift; if ($opt == 1) { print "\n\tWhat is A? "; chomp(my $a = <STDIN>); print "\tWhat is B? "; chomp(my $b = <STDIN>); my $c = sqrt($a**2 + $b**2); print "\tC = $c\n"; } elsif ($opt == 2) { print "\n\tWhat is A? "; chomp(my $a = <STDIN>); print "\tWhat is C? "; chomp(my $c = <STDIN>); die "A cannot be greater than or equal to C!" if $a >= $c; my $b = sqrt($c**2 - $a**2); print "\tB = $b\n"; } else { die "User did no enter 1 or two"; }} print "\nC /|\n \\ / |\n / |-B\n", " / |\n/____|\t1: Use A & B to find C\n", " \\\t2: Use A & C to find B\n A\n"; print "\tChoice: "; chomp(my $choice = <STDIN>); compute($choice);

    Here is the other version, clhypo.pl. This version isn't pretty (infact, there's no interface!) but it does the same thing. It is called like: perl clhypo.pl A B C. A, B, or C must be left out for the program to work properly. For example, perl clhypo.pl 12 - 19 find B, as - 3 5 finds A, and 7 12 - finds C.

    #! Perl -w # clhypo.pl -- Hypotenuse command line version use strict; my($a, $b, $c) = @ARGV; if ($c eq "-") { $c = sqrt($a**2 + $b**2); print "C is $c"; } elsif ($b eq "-") { die "A cannot be greater than or equal to C!" if $a >= $c; $b = sqrt($c**2 - $a**2); print "B is $b"; } elsif ($a eq "-") { die "B cannot be greater than or equal to C!" if $b >= $c; $a = sqrt($c**2 - $b**2); print "A is $a"; }

    I'm like the interfaced version better, but the command line version may suit some people. I'm not sure if this is the fastest way to compute it algorithm-wise, but I think sqrt($a**2 + $b**2) would be very fast.

    > munchie, the number munchin newb
    Llama: The other other white meat!
    (you had to be there :-P)


    In reply to Finding the hypotenuse of a right triangle! by munchie

    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.