<!DOCTYPE HTML>
<html>
<meta charset="ANSI">
<title>Vector Addition</title>
<style>
.text
{
width: 50px;
}
#result
{
margin-top: 8px;
}
</style>
<script src="
http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script>
$(document).ready(function(){
$("#cal").click(function(){
var x = Number($("#x").val());
var A = Number($("#A").val());
var y = Number($("#y").val());
var B = Number($("#B").val());
if (x < 0)
{
x = -x;
A += 180;
}
if (y < 0)
{
y = -y;
B += 180;
}
//A has to be greater than B
A = firstperiod(A);
B = firstperiod(B);
if (B > A)
{
var B0 = B;
B = A;
A = B0;
}
var z_sq = x * x + y * y + 2 * x * y * Math.cos(rad(A - B));
var z = Math.sqrt(z_sq);
if (z > 0)
{
var cos_D = (x * x - y * y + z_sq) / (2 * x * z);
var D = Math.acos(cos_D);
D = degree(D);
var C = 0;
if (A - B <= 180)
C = B + D;
else
C = A + D;
$("#result").html("= " + z + " vec " + C + "°");
}
else
$("#result").html("= <b>0</b>"); //zero vector
});
});
function firstperiod(angle)
{
if (angle < 0)
{
angle = -angle;
angle = 360 - angle % 360;
if (angle == 360)
angle = 0;
}
else
angle = angle % 360;
return angle;
}
function rad(dg)
{
return dg * Math.PI / 180;
}
function degree(r)
{
var d = r * 180 / Math.PI;
return Math.round(d * 1000) / 1000;
}
</script>
<body>
<h3>Vector Addition Calculator V1.0</h3>
<div>
<input id="x" type="text" value="1" class="text">
vec
<input id="A" type="text" value="0" class="text">
°
+
<input id="y" type="text" value="1" class="text">
vec
<input id="B" type="text" value="0" class="text">
°
<input id="cal" type="button" value="Calculate">
</div>
<div id="result"></div>
</body>
</html>