Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Help with basic trigonometry Function

1. ## Help with basic trigonometry Function

Hi, I created this basic trigonometry function, which takes two bits of information about a right angled triangle and then works out the remaining four bits. However the function itself is quite large and I would like some help with cutting it down a good few lines (if it is possible). Someone else also said that the order in which I return my values is wrong, however I am not too sure can anyone help me?
Here is the function:
PHP Code:
``` function CalcTriRa(item1, item2, valtype, sidetype) {     if (valtype == 0) {         //angle, side         angle = new Array(90,item1, 110 - item1);         item1 = item1 * Math.PI / 180;         if (sidetype == 0) {             var adj = Math.cos(item1) * item2;             var opp = Math.sin(item1) * item2;             var hyp = item2;         }         else if (sidetype == 1) { //side is the cos of angle A             var hyp = item2 / Math.cos(item1);             var opp = hyp * Math.sin(item1);             var adj = item2;         }         else {             var hyp = item2 / Math.sin(item1);             var adj = hyp * Math.cos(item1);             var opp = item2;         }         var sides = new Array(hyp,adj,opp);         document.write(angle + "<br>");         document.write(sides);     }     else {         //side, side         if (sidetype == 0) { // adj + opp             var hyp = Math.sqrt(Math.pow(item1, 2) + Math.pow(item2, 2));             angleA = Math.asin(item2 / hyp) / Math.PI * 180;             angleB = 90 - angleA;             var angle = new Array(90,angleA,angleB);             var sides = new Array(hyp,item1,item2);         }         else if (sidetype == 1)  { //hyp + adj             var opp = Math.sqrt(Math.pow(item1, 2) - Math.pow(item2, 2));             angleA = Math.asin(opp / item1) / Math.PI * 180;             angleB = 90 - angleA;             var angle = new Array(90,angleA,angleB);             var sides = new Array(item1,item2,opp);         }         else { //hyp + opp             var adj = Math.sqrt(Math.pow(item1, 2) - Math.pow(item2, 2));             angleA = Math.asin(item2 / item1) / Math.PI * 180;             angleB = 90 - angleA;             var angle = new Array(90,angleA,angleB);             var sides = new Array(item1,adj,item2);         }             document.write(angle + "<br>");             document.write(sides);     } }  ```
Valtype is either 0 or 1, with 0 being an angle and a side and 1 being two sides and it will work out the remaining bits. Side type is what side/angle they are in relation to each other.
Thanks for all of your help.

• I haven't done trig for a long while... but shouldn't this line:

angle = new Array(90,item1, 110 - item1);

be

angle = new Array(90,item1, 90 - item1);

??

• Two things will make your function smaller:

Objects - Giving your function an object as its argument allows you to use named arguments. Returning an object will allow easy access to whatever piece of the output the user desires.

Lazy Execution - Javascript inherited from C a particular way of dealing with boolean expressions. Given something like

if ( 2>1 || prompt("hi","") ) alert("It didn't prompt!");

since 2>1 is true, and true OR'ed with anything is true, the javascript engine doesn't bother to execute the prompt expression since it won't alter the results. And since NaN (not a number) and undefined are considered false, you can do all sorts of fun things.

Here's how I'd to the right triangle thing (included examples with all eight possible argument pairs)
Code:
```<script type='text/javascript'>
function rightTri(obj){
var convert=180/Math.PI;
var sideA=obj.sideA||Math.sqrt(obj.hyp*obj.hyp-obj.sideB*obj.sideB)||obj.sideB*Math.tan(obj.angleA/convert)||obj.sideB/Math.tan(obj.angleB/convert)||obj.hyp*Math.sin(obj.angleA/convert)||obj.hyp*Math.cos(obj.angleB/convert);
var sideB=obj.sideB||Math.sqrt(obj.hyp*obj.hyp-sideA*sideA)||sideA/Math.tan(obj.angleA/convert)||sideA*Math.tan(obj.angleB/convert);
var hyp=Math.sqrt(sideA*sideA+sideB*sideB);
var angleA=Math.atan(sideB/sideA)*convert;
var angleB=Math.atan(sideA/sideB)*convert;
return {sideA:sideA,sideB:sideB,hyp:hyp,angleA:angleA,angleB:angleB};
}

document.write("<table border=1><tr><td>sideA</td><td>sideB</td><td>hypot</td><td>angleA</td><td>angleB</td></tr>");

x=rightTri({sideA:17.320508075688767,hyp:20})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({sideA:17.320508075688767,angleA:60})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({sideA:17.320508075688767,angleB:30})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({sideB:10,hyp:20})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({sideB:10,angleA:60})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({sideB:10,angleB:30})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({hyp:20,angleA:60})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

x=rightTri({hyp:20,angleB:30})
document.write("<tr><td>"+x.sideA.toFixed(3)+"</td><td>"+x.sideB.toFixed(3)+"</td><td>"+x.hyp.toFixed(3)+"</td><td>"+x.angleA.toFixed(3)+"</td><td>"+x.angleB.toFixed(3)+"</td></tr>")

document.write("</table>");
</script>```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•