Jump to content

<<<Question Involving Functions with Triangle Application>>>


lotrfan

Recommended Posts

Hi All,

 

Here's the code.

 

Code:

 

 

$side_a = $_REQUEST[side_a];
$side_b = $_REQUEST[side_b];
$side_c = $_REQUEST[side_c];
$angle_a = $_REQUEST[angle_a];
$angle_b = $_REQUEST[angle_b];
$angle_c = $_REQUEST[angle_c];

$side[0]=$side_a;
$side[1]=$side_b;
$side[2]=$side_c;
$angle[0]=$angle_a;
$angle[1]=$angle_b;
$angle[2]=$angle_c;

SAS($side, $angle);

function SAS($side, $angle){

	if (empty($side[0]))
	{
	$side[0] =  sqrt((($side[1]*$side[1])+($side[2]*$side[2]))-(2*$side[1]*$side[2]*cos($angle[0])));
	echo "<BR>" ,$side[0], " ";}

    
	if (empty($side[1]))
  {
   $side[1] =  sqrt((($side[0]*$side[0])+($side[2]*$side[2]))-(2*$side[0]*$side[2]*cos($angle[1])));
	echo "<BR>" ,$side[1], " ";
   }
   
   if (empty($side[2]))
  {
   $side[2] =  sqrt((($side[0]*$side[0])+($side[1]*$side[1]))-(2*$side[0]*$side[1]*cos($angle[2])));
	echo "<BR>" ,$side[2], " ";
   }
  
     }

 

 

Here the user has input into a form (whose information is taken by $_REQUEST):

 

--> "1" for side a

--> "1" for side b

--> "90" for angle C

 

Here's my question...I've solved this problem out with a calculator. When a Side-Angle-Side has values of 1, 90 degrees, and 1 (respectively) the last side should be about "1.4142135623730950488016887242097".

 

Yet when I use this function (which is the Law of Cosines) my browser displays "1.70180704907" as an answer.  I think that my function may be faulty -- I'm trying to pass array values into a function -- and I don't know if it is working....

 

***I know this is a very involved question...so I know that this is a lot to ask. Thanks in advance.

Link to comment
Share on other sites

Ah, I see. You are saying that when I use the cosine function, that instead of "90" degrees -- it is thinking that it is "90" radians...

That would actually make perfect sense, and I can't believe I didn't think of something like that.

 

Would I change it something like this?

 

$angle[0] = deg2rad($angle[0]);

 

Thanks again.

 

Link to comment
Share on other sites

you have a few options, you can make a bool switch saying if measuremeants are rads or degrees and based on then work on that or just make the assumption that all are in degrees as only time you use radians is when you calculate a value in a calc class really.  Of couse when you assume well you make a (assOUTOFuANDme) if you get the point

Link to comment
Share on other sites

Ah, I see. You are saying that when I use the cosine function, that instead of "90" degrees -- it is thinking that it is "90" radians...

That would actually make perfect sense, and I can't believe I didn't think of something like that.

 

Would I change it something like this?

 

$angle[0] = deg2rad($angle[0]);

 

Thanks again.

 

 

Yep.

 

Check this page

Link to comment
Share on other sites

OK, but if I change "90" from radians to degrees (which is good because I want them in degrees) the PHP converts "90 radians" into degrees -- which still is not correct.

 

How do I set the variable that the user inputs (from the very start) to be a degree value instead? I've tried to search the PHP manual, but I can't seem to find anything.

 

Thanks for your time.

Link to comment
Share on other sites

No.  The most user friendly way would be to ask the user which form they're entering it as.

 

For example, in the other thread about this topic (grr two threads! ;p), I posted a script with a radio button where a user may say if the number entered is degree or radian....

 

Then, if you know the entered number is degrees, you convert it to radians, but if it's in radians, you just leave it alone ;p.

Link to comment
Share on other sites

Sorry about the two threads...I thought I'd try to get a couple different perspectives from each of the forums -- little did I know that the users here would be so interdisciplinary in their PHP knowledge!

 

But yes, I will adapt something like the radio method you used and make it a user's choice. It will also make it more user-friendly and applicable.

 

Thanks all (especially to corbin for working two posts at once, lol)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.