Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Compute Angles of a Right Triangle

by true (Pilgrim)
on Dec 10, 2003 at 20:26 UTC ( [id://313852]=perlquestion: print w/replies, xml ) Need Help??

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

Problem: I want a function to compute the angles in a right triangle only knowing the length of all three sides (i don't know the angles).

I tried to solve this with the Math::Cephes module, But the function tells me I have to pass an array reference to it. I've tried to pass it my X,Y points in several ways but no dice.

Anybody know a simple way to do this? Here's the code from my last attempt with Cephes. Code does NOT work. The module loads fine. My error is: "Must supply array references at C:/Perl/site/lib/Math/Cephes.pm line 107."

use Math::Cephes qw(:all); # double p[3], q[3], vecang();$y = vecang( $p, $q ); # For two vectors p, q, the angle A between them is given by my @p = ("0","0"); my @q = ("4","3"); my $ang = vecang(@p,@q); print "ang=$ang\n";
thanks for readin,
jtrue

Replies are listed 'Best First'.
Re: Compute Angles of a Right Triangle
by Coruscate (Sexton) on Dec 10, 2003 at 20:49 UTC

    I don't know the module, but some basic trig knowledge can help. In a right triangle, there is a simple relationship between angles and sides. If you have all three sides (SSS), then the following relationship holds true. Here's a diagram that demonstrates the SOH CAH TOA acronym. (sin = opposite/hypotenuse, cos = adjacant/hypotenuse, tan = opposite/adjacant). You could take this knowledge and create a routine that takes the sides in a specific order, calculates the sine values for the two unknown angles and then uses Math::Trig::asin() to get the angles. Seems quite long winded though :)

      A
      |\         sin A = a/b {(b is hypotenuse 4)
      | \        sin A = 3/5
      |  \
    4 |   \ 5    sin C = c/b
      |    \     sin C = 4/5
      |90   \
      -------|
     B   3    C
    

    Update: Not to mention that you only need to calculate one of the unknown angles to get all three. Since you have the 90 degrees, calculate one, then knowing that all 3 angles of a triangle have a sum of 180, you just subtract the two known angles from 180 and there's your third.

Re: Compute Angles of a Right Triangle
by traveler (Parson) on Dec 10, 2003 at 20:56 UTC
    Unless my trig fails me, something like this should work. I converted to degrees, but you could use Math::Trig::Degrees, too.
    use strict; use Math::Trig; my @lengths = (3,4,5); my @slengths = sort {$a <=> $b} @lengths; my $a1 = atan($slengths[1]/$slengths[0]) * 180/3.141592653589; my $a2 = 90 - $a1; print "Angles are $a1, $a2\n";
    HTH, --traveler
      That's a nice solution. To comment on it a little, the sort finds the two shortest sides (so the hypotenuse is ignored). atan computes the the arctangent.

      Math::Trig is bundled with perl, but if you wanted to use a builtin, you could use atan2 instead, passing the two sides as separate arguments.

      That isn't as acurate a value of pi as possible. 3.14159265359 is closer (since the digits following 89 are 7932). You can use 4*atan2(1,1) to get a value with accuracy commensurate with your perl's floating point accuracy.

        You can use 4*atan2(1,1) to get ... [pi]

        Or, since atan2() is properly ranged from -pi to pi, you can use atan2(0,-1) and forego the multiplication

Re: Compute Angles of a Right Triangle
by Plankton (Vicar) on Dec 10, 2003 at 20:48 UTC
    You should be able to do this with just the atan2() function. I just found this on google. It explains the math pretty well.
    Perldoc has this ...
    perldoc -f atan2 atan2 Y,X Returns the arctangent of Y/X in the range -PI to PI. For the tangent operation, you may use the "Math::Trig::tan" function, or use the familiar relation: sub tan { sin($_[0]) / cos($_[0]) }
    Here's the start of a script you could use ...
    bash-2.03$ cat ./atan2.pl #!/usr/local/bin/perl -w use strict; my $x = defined($_=shift) ? $_ : die "Enter X\n"; my $y = defined($_=shift) ? $_ : die "Enter Y\n"; my $r = atan2( $y, $x ); print "angle = $r radians\n"; bash-2.03$ ./atan2.pl 4 3 angle = 0.643501108793284 radians

    Plankton: 1% Evil, 99% Hot Gas.
Re: Compute Angles of a Right Triangle
by boo_radley (Parson) on Dec 10, 2003 at 20:34 UTC

    If it's expecting references, maybe my $ang = vecang(\@p,\@q);?
    the forward slashes pass the arrays by reference. see perlref and perlreftut for details.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-16 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found