Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Finding the hypotenuse of a right triangle!

by munchie (Monk)
on Apr 08, 2002 at 23:44 UTC ( [id://157602]=CUFP: print w/replies, xml ) Need Help??

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)

  • Replies are listed 'Best First'.
    Re: Finding the hypotenuse of a right triangle!
    by rinceWind (Monsignor) on Apr 09, 2002 at 12:30 UTC
      Just a small nitpick.

      It's a good idea to avoid using variables $a and $b, as they have special meaning for sort.

    Re: Finding the hypotenuse of a right triangle!
    by belg4mit (Prior) on Apr 08, 2002 at 23:53 UTC
      Can't think of the last time I needed the hypotenuse of a trinagle. Though I do still remember the Pyhagorean theorem. A few points
    • There is no difference between sides b and c as far as math is concerned, as such you can simplify the code greatly, and remove the need for -
    • a heredoc might make for an easier and cleaner way to store the picture
    • What if the user gives a negative length?
    • --
      perl -pe "s/\b;([mnst])/'\1/mg"

        Actually, A and B are the same mathematically, not B and C. If I leave the dash out, how is the user supposed to tell the program the 12 5 means A & B, or A & C, or B & C?

        Oh, about the first point (not needing to know what the hypotenuse of a triangle is). I'm 13 years old, and I'm in a national math competition called Mathcount. This year our team got to states, coming first at regionals. I eat sleep and breathe math. Read more on my team and mathcounts here (an entry in my blog).

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

          It's simply a matter of how you label them (yes the usual is a**2 + b**2 = c**2, but they're variables so the names don't really matter). Anyways, that kind of goes to the point of the picture not being very discernible in it's current state.

          With your side notation you only need two cases A & B or A & C. Traditionally in such cases one uses switches, see Getopt::Std (among others).

          PS> I didn't say there was no point in knowing what a hypotenuse was. I just joked that it's not something one is often called upon to find. --
          perl -pe "s/\b;([mnst])/'\1/mg"

        a negative length doesnt exist smarty pants and who in the hell would use one?
          It's called data validation tr0ll. Although this particular algorythm is transparent(and by this I mean it could care less -1**2==1**2)to negative numbers, that does not mean one should let bad data through.

          --
          perl -pe "s/\b;([mnst])/'\1/mg"

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: CUFP [id://157602]
    Approved by rob_au
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2024-04-19 11:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found