<script language="JavaScript" src="http://www.ab126.com/d/js/14/geometry.js"></script>
<script language="javascript">
function clearsquare()
{
document.square.wid.value = "";
document.square.len.value = "";
document.square.area.value = "";
document.square.perimeter.value = "";
document.square.diagonal.value = "";
document.square.perimeter.style.backgroundColor = '';
document.square.wid.style.backgroundColor = '';
document.square.len.style.backgroundColor = '';
document.square.area.style.backgroundColor = '';
document.square.diagonal.style.backgroundColor = '';
document.square.wid.focus();
}
function isNum(args)
{
args = args.toString();
if (args.length == 0)
return false;
for (var i = 0; i "9") && args.substring(i, i+1) != ".")
{
return false;
}
}
return true;
}
function squarecal(p)
{
var wid = document.square.wid.value;
var len = document.square.len.value;
var area = document.square.area.value;
var perimeter = document.square.perimeter.value;
var diagonal = document.square.diagonal.value;
if (p == "1")
{
if (!isNum(wid))
{
wid = wid.substring(0,wid.length-1);
document.square.wid.value = wid;
}
else
{
document.square.wid.style.backgroundColor = '#cccccc';
}
return;
}
if (p == "2")
{
if (!isNum(len))
{
len = len.substring(0,len.length-1);
document.square.len.value = len;
}
else
{
document.square.len.style.backgroundColor = '#cccccc';
}
return;
}
if (p == "3")
{
if (!isNum(area))
{
area = area.substring(0,area.length-1);
document.square.area.value = area;
}
else
{
document.square.area.style.backgroundColor = '#cccccc';
}
return;
}
if (p == "4")
{
if (!isNum(perimeter))
{
perimeter = perimeter.substring(0,perimeter.length-1);
document.square.perimeter.value = perimeter;
}
else
{
document.square.perimeter.style.backgroundColor = '#cccccc';
}
return;
}
if (p == "5")
{
if (!isNum(diagonal))
{
diagonal = diagonal.substring(0,diagonal.length-1);
document.square.diagonal.value = diagonal;
}
else
{
document.square.diagonal.style.backgroundColor = '#cccccc';
}
return;
}
}
function squareblur(p)
{
//alert("good");
var wid = document.square.wid.value;
var len = document.square.len.value;
var area = document.square.area.value;
var perimeter = document.square.perimeter.value;
var diagonal = document.square.diagonal.value;
var flag=0;
if (isNum(wid))
{
flag++;
wid = wid * 1;
}
if (isNum(len))
{
flag++;
len = len * 1;
}
if (isNum(area))
{
flag++;
area = area * 1;
}
if (isNum(perimeter))
{
flag++;
perimeter = perimeter * 1;
}
if (isNum(diagonal))
{
flag++;
diagonal = diagonal * 1;
}
if (flag < 2) return;
//alert("good");
if (p == "1")
{
if (isNum(len))
{
area = wid * len;
perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(area))
{
len = area/wid;
perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(perimeter))
{
len = (perimeter - 2 * wid )/2;
//perimeter = 2 * wid + 2 * len;
area = wid * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(diagonal))
{
len = Math.sqrt(diagonal * diagonal - wid * wid);
perimeter = 2 * wid + 2 * len;
area = wid * len;
}
}
else if ( p == "2")
{
//alert("2");
if (isNum(wid))
{
area = wid * len;
perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
//alert(area);
//alert(perimeter);
//alert(dianogal);
}
else if (isNum(area))
{
wid = area/len;
perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(perimeter))
{
wid = (perimeter - 2 * len)/2;
//perimeter = 2 * wid + 2 * len;
area = wid * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(diagonal))
{
wid = Math.sqrt(diagonal * diagonal - len * len);
perimeter = 2 * wid + 2 * len;
area = wid * len;
}
}
else if ( p == "3")
{
//alert(3);
if (isNum(wid))
{
len = area/wid;
//area = wid * len;
perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(len))
{
wid = area/len;
//area = wid * len;
perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(perimeter))
{
//alert(area);
var a = 1;
var b = -0.5 * perimeter;
var c = area;
ret = quadcal(a,b,c);
if (ret[2] == 0 || ret[1] == 0)
{
//alert("value error!")
return;
}
//alert(ret[1]);
//alert(ret[2]);
if (ret[1] > ret[2])
{
len = ret[1];
wid = ret[2];
diagonal = Math.sqrt(wid * wid + len * len);
}
else
{
wid = ret[1];
len = ret[2];
diagonal = Math.sqrt(wid * wid + len * len);
}
}
else if (isNum(diagonal))
{
//wid = Math.sqrt(diagonal * diagonal - len * len);
//perimeter = 2 * wid + 2 * len;
//area = wid * len;
var a = 1;
var b = -1 * diagonal * diagonal;
var c = area * area;
ret = quadcal(a,b,c);
if (ret[2] < 0 || ret[1] < 0)
{
return;
}
if (ret[1] > ret[2])
{
len = Math.sqrt(ret[1]);
wid = Math.sqrt(ret[2]);
perimeter = 2 * (len + wid);
}
else
{
wid = Math.sqrt(ret[1]);
len = Math.sqrt(ret[2]);
perimeter = 2 * (len + wid);
}
}
}
else if ( p == "4")
{
if (isNum(wid))
{
len = (perimeter - 2 * wid) / 2;
area = wid * len;
//perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(len))
{
wid = (perimeter - 2 * len) / 2;
//wid = area/len;
area = wid * len;
//perimeter = 2 * wid + 2 * len;
diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(area))
{
//alert(area);
var a = 1;
var b = -0.5 * perimeter;
var c = area;
ret = quadcal(a,b,c);
if (ret[2] == 0 || ret[1] == 0)
{
//alert("value error!")
return;
}
//alert(ret[1]);
//alert(ret[2]);
if (ret[1] > ret[2])
{
len = ret[1];
wid = ret[2];
diagonal = Math.sqrt(wid * wid + len * len);
}
else
{
wid = ret[1];
len = ret[2];
diagonal = Math.sqrt(wid * wid + len * len);
}
}
else if (isNum(diagonal))
{
var a = 2;
var b = -1 * perimeter;
var c = 0.25 * perimeter * perimeter - diagonal * diagonal;
ret = quadcal(a,b,c);
if (ret[2] == 0 || ret[1] == 0)
{
return;
}
if (ret[1] > ret[2])
{
len = ret[1];
wid = ret[2];
area = len * wid;
}
else
{
wid = ret[1];
len = ret[2];
area = len * wid;
}
}
}
else if ( p == "5")
{
if (isNum(wid))
{
len = Math.sqrt(diagonal * diagonal - wid * wid);
area = wid * len;
perimeter = 2 * wid + 2 * len;
//diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(len))
{
wid = Math.sqrt(diagonal * diagonal - len * len);
//wid = area/len;
area = wid * len;
perimeter = 2 * wid + 2 * len;
//diagonal = Math.sqrt(wid * wid + len * len);
}
else if (isNum(area))
{
var a = 1;
var b = -1 * diagonal * diagonal;
var c = area * area;
ret = quadcal(a,b,c);
if (ret[2] == 0 || ret[1] == 0)
{
return;
}
if (ret[1] > ret[2])
{
len = Math.sqrt(ret[1]);
wid = Math.sqrt(ret[2]);
perimeter = 2 * (len + wid);
}
else
{
wid = Math.sqrt(ret[1]);
len = Math.sqrt(ret[2]);
perimeter = 2 * (len + wid);
}
}
else if (isNum(perimeter))
{
var a = 2;
var b = -1 * perimeter;
var c = 0.25 * perimeter * perimeter - diagonal * diagonal;
ret = quadcal(a,b,c);
if (ret[2] == 0 || ret[1] == 0)
{
return;
}
if (ret[1] > ret[2])
{
len = ret[1];
wid = ret[2];
area = len * wid;
}
else
{
wid = ret[1];
len = ret[2];
area = len * wid;
}
}
}
wid = fixed(wid,4);
len = fixed(len,4);
area = fixed(area,4);
perimeter = fixed(perimeter,4);
diagonal = fixed(diagonal,4);
document.square.wid.value = wid;
document.square.len.value = len;
document.square.area.value = area;
document.square.perimeter.value = perimeter;
document.square.diagonal.value = diagonal;
}
function calcs()
{
var wid = document.square.wid.value;
var len = document.square.len.value;
var area = document.square.area.value;
var perimeter = document.square.perimeter.value;
var diagonal = document.square.diagonal.value;
var flag=0;
if (isNum(wid))
{
flag++;
wid = wid * 1;
}
if (isNum(len))
{
flag++;
len = len * 1;
}
if (isNum(area))
{
flag++;
area = area * 1;
}
if (isNum(perimeter))
{
flag++;
perimeter = perimeter * 1;
}
if (isNum(diagonal))
{
flag++;
diagonal = diagonal * 1;
}
if (flag < 2) return;
if (isNum(wid)) {squareblur(1);}
else if (isNum(len)) {squareblur(2);}
else if (isNum(area)) {squareblur(3);}
else if (isNum(perimeter)) {squareblur(4);}
else if (isNum(diagonal)) {squareblur(5);}
}
function quadcal(a,b,c)
{
var det = Math.pow(b,2) - 4 * a * c;
//alert("det is: " + det);
var rootpart = "NaN";
if (det > 0) rootpart = Math.sqrt(det) / (2 * a);
//else rootpart += "i";
//alert("rootpart is: " + rootpart);
var rootparti = "NaNi";
if (det <0)
{
var rootpartnum = (Math.sqrt(-det) / (2 * a));
rootparti = (Math.sqrt(-det) / (2 * a)) + "i";
}
//else rootparti += "i";
//alert("rootparti is: " + rootparti);
//if (parseFloat(rootparti) < 0)
if (a < 0)
{
rootparti1 = rootparti;
rootparti2 = (Math.sqrt(-det) / (2 * -1 * a)) + "i";
}
else
{
if (rootparti == "NaNi")
{
rootparti1 = "NaNi";
rootparti2 = "NaNi";
}
else
{
rootparti1 = (Math.sqrt(-det) / (2 * -1 * a)) + "i";
rootparti2 = rootparti;
}
//alert(rootparti1);
//alert(rootparti2);
}
if (rootparti1 == "1i")
{
rootparti1 = i;
rootparti2 = "-i";
}
else if (rootparti1 == "-1i")
{
rootparti1 = "-i";
rootparti2 = i;
}
if (det == 0)
{
x1 = x2 = -b / (2 * a);
}
else if (det > 0)
{
x1 = (-b + Math.sqrt(det)) / (2 * a);
x2 = (-b - Math.sqrt(det)) / (2 * a);
}
else if ((-b / (2 * a)) == 0)
{
x1 = rootparti1;
x2 = rootparti2;
}
else
{
alert("value error!");
x2 =0 ;
x1 =0;
//x1 = (-b / (2 * a) + " + " + rootparti1);
//x2 = (-b / (2 * a) + " + " + rootparti2);
}
var ret = new Array();
ret[1] = x1;
ret[2] = x2;
return ret;
}
</script>
长方形对角线长度
计算公式:
|
|
1. 面积 = a × b
2. 周长 = 2a + 2b
3. 对角线 = √a
× a + b × b
|
更新:20210423 103953