...

View Full Version : Help with basic trigonometry Function



evilguru
06-30-2005, 04:56 PM
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:

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.

WMJB
07-01-2005, 02:30 AM
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);

??

Harry Armadillo
07-01-2005, 04:40 AM
Two things will make your function smaller:

Objects - Giving your function an object as its argument allows you to use named arguments (http://www.javascriptkit.com/javatutors/namedfunction.shtml). 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)
<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum