Keep It Simple, Stupid PerlMonks

### Help w/ Law of Sines script

by sifukurt (Hermit)
 on Aug 02, 2001 at 01:09 UTC Need Help??

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

Hello, fellow programmers of the cloth. I need some help. The wife of a good friend of mine is taking a trigonometry class. I thought that I'd whip up a quickie perl script that would perform the Law of Sines so she could quickly check her answers. After a bit of playing, I ended up with this:
I was more interested in doing it quickly that I was in making it efficient or elegant. Now that I have the time, though, I want to clean it up. My problem is that after looking at it for as many hours as I have, I could really use someone else's input. I use Math::NumberCruncher to perform any degree/radian conversions. I'm really just doing this for fun (egad, did I just say that I'm doing the Law of Sines for fun? I need to get out more....), so there is no hurry, but if someone could take a look at my code and give me an assist in making it more efficient and/or elegant, I would appreciate it greatly.

Thanks a bunch.
___________________
Kurt

Replies are listed 'Best First'.
Re: Help w/ Law of Sines script
by dragonchild (Archbishop) on Aug 02, 2001 at 01:29 UTC
A few suggestions:
• Break the Law_Of_Sines() function up a bit more. For example, you have a (rather hefty) section of code cut'n'pasted three times. Make it a subroutine.
• Instead of running \$A, \$a, \$B, \$b, \$C, and \$c, why not do a list of hashes called angles? So, you could do something like \$angles->[0]{B} to reference the second angle in the first triangle. The real benefit of that is to allow you to do for loops, reducing the number of lines while improving readability.
An example of the second suggestion would be:

unless ( \$A > 0 && \$B > 0 && \$C > 0 ) { if ( \$A > 0 && \$B > 0 ) { \$var = "C" } if ( \$A > 0 && \$C > 0 ) { \$var = "B" } if ( \$B > 0 && \$C > 0 ) { \$var = "A" } if ( \$var eq "C" ) { \$sum = \$angles{B} + \$angles{A}; \$other = 180 - \$sum; \$angles{\$var} = \$other; \$C = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$oth +er ) ); } elsif ( \$var eq "B" ) { \$sum = \$angles{A} + \$angles{C}; \$other = 180 - \$sum; \$angles{\$var} = \$other; \$B = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$oth +er ) ); } elsif ( \$var eq "A" ) { \$sum = \$angles{B} + \$angles{C}; print ("Sum: \$sum\n"); \$other = 180 - \$sum; \$angles{\$var} = \$other; \$A = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$oth +er ) );

if (grep \$_ <= 0, @{\$angles->[0]}{('A' .. 'C')}) { my \$zero_angle = 'A'; if (\$angles->[0]{C} <= 0) { \$zero_angle = 'C' } elsif (\$angles->[0]{B} <= 0) { \$zero_angle = 'B' } my @sides = ('A' .. 'C'); @sides = grep \$_ ne \$zero_angle, @sides; \$sum = 0; \$sum += \$_ for @{\$angles->[0]}{@sides}; \$other = 180 - \$sum; \$angles{\$var} = \$other; \$angles->[0]{\$zero_angle} = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$other ) ); }
The primary difference between your version and mine is that I chose a data structure that allows me to have the interpreter associate pieces of data that should be associated together. Your version requires that you continually associate them yourself.

The second difference is that I'm taking advantage of the easy conversions between hashes and arrays through hash-slices. Read up on them. They're SOOOO powerful. :)

Now, you don't have to use my data structure. I chose it because it was the closest to what you were doing. However, it can easily make sense to have the angles in an array and not a hash. So, triangle 1, angle 2 would be \$angles->[0][1]. Simple enough.

Keep with it. It's a neat idea!

Update: Fixed a few square brackets. Thanx Albannach and ChemBoy!

Very nice. That's exactly the sort of thing I was looking for. Originally I had the repeated blocks of code as a subroutine, but for reasons I don't know, it didn't want to work correctly. It was calculating the correct values, but wasn't returning them correctly. Of course, I was working on this in the evening after a day of programming, so by that time I was probably suffering from that weird sort of programming blindness where the answer to the problem is hiding in plain sight.

I'm going to incorporate your suggestions (as well as the suggestions of others) and will post the modified version as soon as it is ready. Thanks much.
___________________
Kurt
Re: Help w/ Law of Sines script
by lucs (Sexton) on Aug 02, 2001 at 11:28 UTC
#! /usr/bin/perl -w # Here is another way to do it, using a trigonometry 'toolkit' # approach and a different interface. Sides are \$a, \$b, and \$c, and # corresponding opposing angles are \$A, \$B, and \$C. Note that # pathological cases will break the program (e.g., using two angles # whose sum is greater than 180); more robust error checking is # required. use strict; test(); main(); sub main { print "Enter an angle (in degrees) and its opposing side:\n"; print 'Angle: '; chomp(my \$A = <STDIN>); print 'Side : '; chomp(my \$a = <STDIN>); print q/Enter 'a' if you have an angle, 's' if you have a side: /; GET_WHAT: chomp(my \$what = lc <STDIN>); if (\$what eq 'a') { print 'Other angle: '; chomp(my \$B = <STDIN>); my \$C = 180 - \$A - \$B; my \$b = aAB_to_b(\$a, \$A, \$B); my \$c = aAB_to_b(\$a, \$A, \$C); printf 'The other two sides are %.2f and %.2f, ' . "and the other angle is %.2f\n", \$b, \$c, \$C; } elsif (\$what eq 's') { print 'Other side: '; chomp(my \$b = <STDIN>); my \$B = aAb_to_B(\$a, \$A, \$b); my \$C = 180 - \$A - \$B; my \$c = aAB_to_b(\$a, \$A, \$C); printf 'The other side is %.2f, ' . "and the other two angles are %.2f and %.2f\n", \$c, \$B, \$C; } else { print q/Please enter 'a' or 's': /; goto GET_WHAT; } } sub aAb_to_B { my (\$a, \$A, \$b) = @_; return rad2deg(asin(\$b * sin(deg2rad(\$A)) / \$a)); } sub aAB_to_b { my (\$a, \$A, \$B) = @_; return sin(deg2rad(\$B)) * \$a / sin(deg2rad(\$A)); } sub asin { my \$sine = shift; return undef unless \$sine >= -1 && \$sine <= 1; return atan2(\$sine, sqrt(1 - \$sine * \$sine)); } use constant PI => 4 * atan2(1, 1); sub deg2rad { \$_[0] / 180 * PI } sub rad2deg { \$_[0] / PI * 180 } sub test { # More tests would be welcome. expect('aAB_to_b', 10, 5, 30, 90); expect('aAb_to_B', 90, 5, 30, 10); } sub expect { my (\$sub, \$exp, @args) = @_; no strict 'refs'; my \$got = \$sub->(@args); return if abs(\$got - \$exp) < 0.0001; local \$" = ', '; printf "Expected \$sub(@args) to give \$exp, but got %.2f\n", \$got; }
Re: Help w/ Law of Sines script
by John M. Dlugosz (Monsignor) on Aug 02, 2001 at 02:15 UTC
Urrrgh! PM bites again, losing the long reply I typed. Instead of "Preview" it took me to Dragonchild's node, and hitting the Back button just went to the same place ad infinitum. I wish they'd fix that already... I've taken to copying the text to the clipboard before submitting, just in case. Wish I'd done it this time (sigh)

So, DC mentioned reducing the redundancy in your code, so I won't retype it.

But, I do want to encourage you and give you some food for thought: When I wrote this same thing in high school, I incorporated the law of sines, law of cosines, and the fact that the sum of the angles is 180 into a single unified program. It would prompt you for 3 sides and angles. Enter what you know, or ? to solve for that, or blank for don't know/don't care. Then it tells you what you wanted to know.

—John

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://101484]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2023-09-27 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?