mpjbrennan
07-01-2000, 02:15 PM
Someone asked for something like this on another site recently. I thought it was worth spreading around. The script takes two sides and the included angle of a triangle and computes the remaining sides and angles - then it draws the triangle. You can see it in action at http://www.patrick-brennan.com/javascript/triangle.html or just copy the script below.
(For Netscape 4+ users there is a version at http://www.patrick-brennan.com/javascript/triangle_ns.html - it runs somewhat slower.
patrick
------------------
<html>
<head>
<script language=javascript>
function validate(x){
if(isNaN(x)){alert("you must enter a number"); reset()}
if(x > 350){alert("values this large may exceed the screen dimensions")}
if(x < 10){alert("values this small will not display well"); reset()}
}
function reset(){
for (i = 0; i <= 2; i++){
document.forms[0].elements[i].value = ""
}}
var i
function compute(a, b, alpha){
if (a == "" || b == "" || alpha == ""){alert ("you must enter values for all variables"); reset(); return false}
if (alpha >= 180){alert ("this is an invalid value for a triangle"); reset(); return false}
var alphar = parseFloat(alpha)*2*Math.PI/360
var h = parseFloat(b)*Math.sin(alphar)
var x = Math.floor(parseFloat(h)/Math.tan(alphar))
var beta = Math.abs(Math.atan(h/(a - x)))
var c = h/Math.sin(beta)
var beta = beta*360/2/Math.PI
var gamma = 180 - beta - alpha
document.write("<body bgColor='#c6eff7'>")
document.write("<span style='font:10pt Arial; color:navy'>side 1 = " + a + " : side 2 = " + b + " : side 3 = " + parseInt(c) + "</span><br>")
document.write("<span style='font:10pt Arial; color:navy'>side 1/side 2 angle = " + alpha + " : side 1/side 3 angle = " + parseInt(beta) + " : side 2/side 3 angle = " + parseInt(gamma) + " (values rounded)</span>")
for (i = 0; i <= parseInt(a); i++){
document.write("<span style='position:absolute; color:red; top:" + 300 + "; left:" + (250 + i) + "'>.</span>")
}
for (i = 0; i <= parseInt(b); i++){
document.write("<span style='position:absolute; color:blue; top:" + (300 - i*parseInt(h)/parseInt(b)) + "; left:" + (250 + i*parseInt(x)/parseInt(b)) + "'>.</span>")
}
for (i = 0; i <= parseInt(c); i++){
document.write("<span style='position:absolute; color:green; top:" + (300 - i*parseInt(h)/parseInt(c)) + "; left:" + (250 + parseInt(a) - i*parseInt(a-x)/parseInt(c)) + "'>.</span>")
}
document.write("<span style='position:absolute; color:red; top:350; left:350'>side 1</span>")
document.write("<span style='position:absolute; color:blue; top:200; left:150'>side 2</span>")
document.write("<span style='position:absolute; color:green; top:200; left:500'>side 3</span>")
document.write("<form><input type=button value=return onClick='history.go(-1)'></form>")
document.close()
}
</script>
</head>
<body bgColor="#c6eff7">
<h1>Calculate the dimensions of a triangle</h1>
<br><br>
<form>
enter a value for side 1:*<input id=side1 type=text size=5 onChange="validate(this.value)">* *
enter a value for side 2:*<input id=side2 type=text size=5 onChange="validate(this.value)">* *
enter a value for the included angle:*<input id=angle1 type=text size=5 onChange="validate(this.value)"><br><br><br>
<input type=button value="Compute" onClick="compute(side1.value, side2.value, angle1.value)">* *
<input type=reset>
</form>
</body>
</html>
[Edited by mpjbrennan on 07-01-2000 at 11:58 AM]
(For Netscape 4+ users there is a version at http://www.patrick-brennan.com/javascript/triangle_ns.html - it runs somewhat slower.
patrick
------------------
<html>
<head>
<script language=javascript>
function validate(x){
if(isNaN(x)){alert("you must enter a number"); reset()}
if(x > 350){alert("values this large may exceed the screen dimensions")}
if(x < 10){alert("values this small will not display well"); reset()}
}
function reset(){
for (i = 0; i <= 2; i++){
document.forms[0].elements[i].value = ""
}}
var i
function compute(a, b, alpha){
if (a == "" || b == "" || alpha == ""){alert ("you must enter values for all variables"); reset(); return false}
if (alpha >= 180){alert ("this is an invalid value for a triangle"); reset(); return false}
var alphar = parseFloat(alpha)*2*Math.PI/360
var h = parseFloat(b)*Math.sin(alphar)
var x = Math.floor(parseFloat(h)/Math.tan(alphar))
var beta = Math.abs(Math.atan(h/(a - x)))
var c = h/Math.sin(beta)
var beta = beta*360/2/Math.PI
var gamma = 180 - beta - alpha
document.write("<body bgColor='#c6eff7'>")
document.write("<span style='font:10pt Arial; color:navy'>side 1 = " + a + " : side 2 = " + b + " : side 3 = " + parseInt(c) + "</span><br>")
document.write("<span style='font:10pt Arial; color:navy'>side 1/side 2 angle = " + alpha + " : side 1/side 3 angle = " + parseInt(beta) + " : side 2/side 3 angle = " + parseInt(gamma) + " (values rounded)</span>")
for (i = 0; i <= parseInt(a); i++){
document.write("<span style='position:absolute; color:red; top:" + 300 + "; left:" + (250 + i) + "'>.</span>")
}
for (i = 0; i <= parseInt(b); i++){
document.write("<span style='position:absolute; color:blue; top:" + (300 - i*parseInt(h)/parseInt(b)) + "; left:" + (250 + i*parseInt(x)/parseInt(b)) + "'>.</span>")
}
for (i = 0; i <= parseInt(c); i++){
document.write("<span style='position:absolute; color:green; top:" + (300 - i*parseInt(h)/parseInt(c)) + "; left:" + (250 + parseInt(a) - i*parseInt(a-x)/parseInt(c)) + "'>.</span>")
}
document.write("<span style='position:absolute; color:red; top:350; left:350'>side 1</span>")
document.write("<span style='position:absolute; color:blue; top:200; left:150'>side 2</span>")
document.write("<span style='position:absolute; color:green; top:200; left:500'>side 3</span>")
document.write("<form><input type=button value=return onClick='history.go(-1)'></form>")
document.close()
}
</script>
</head>
<body bgColor="#c6eff7">
<h1>Calculate the dimensions of a triangle</h1>
<br><br>
<form>
enter a value for side 1:*<input id=side1 type=text size=5 onChange="validate(this.value)">* *
enter a value for side 2:*<input id=side2 type=text size=5 onChange="validate(this.value)">* *
enter a value for the included angle:*<input id=angle1 type=text size=5 onChange="validate(this.value)"><br><br><br>
<input type=button value="Compute" onClick="compute(side1.value, side2.value, angle1.value)">* *
<input type=reset>
</form>
</body>
</html>
[Edited by mpjbrennan on 07-01-2000 at 11:58 AM]