<body> <divid="head"> <pre>每行有多少角点?</pre> <inputtype="number"id="colCount" /> <pre>有多少行?</pre> <inputtype="number"id="rowCount" /> <pre> 坡的宽高比:</pre> <inputtype="number"id="M" /> <pre> 方格边长a:</pre> <inputtype="number"id="A" /> <buttonid="start">生成</button> </div> <scripttype="text/javascript"> var start = document.getElementById("start"); var m = ""; var a = ""; var d = null; start.addEventListener("click", res => { //document.removeChild(document.getElementById('forms'));
var row = document.getElementById("rowCount").value; var col = document.getElementById("colCount").value; m = parseFloat(document.getElementById("M").value); a = parseFloat(document.getElementById("A").value); if (row == "" || col == "" || m == "" || a == "") returnfalse; elsecreatetables(row, col); }); functioncreatetables(row, col) { //搭建界面 var determinants = newArray(row); for (var i = 0; i < row; i++) determinants[i] = newArray(col); var form = document.createElement("table"); form.setAttribute("id", "forms"); document.body.appendChild(form); var tr = newArray(row); for (var i = 0; i < row; i++) { tr[i] = document.createElement("tr"); form.appendChild(tr[i]); for (var j = 0; j < col; j++) { var td = document.createElement("td"); tr[i].appendChild(td); determinants[i][j] = document.createElement("input"); determinants[i][j].setAttribute("class", "deter"); determinants[i][j].setAttribute("type", "number"); td.appendChild(determinants[i][j]); } } d = determinants;
var count = document.createElement("button"); count.setAttribute("id", "count"); form.appendChild(count);
count.appendChild(document.createTextNode("计算")); var result = document.createElement("div"); result.setAttribute("id", "result"); document.body.appendChild(result); count.onclick = function () { calculation(row, col, result); }; } functioncalculation(row, col, result) { //总算法 //安全检查 d.forEach(item => { if (item.value == "") returnfalse; }); var total = newwork(); total.init(0, 0);
//提取四条边 var up = newArray(), left = newArray(), buttom = newArray(), right = newArray();
for (var i = 0; i < row; i++) { for (var j = 0; j < col; j++) { if (i == 0) up.push(parseFloat(d[i][j].value)); if (j == 0) left.push(parseFloat(d[i][j].value)); if (i == row - 1) buttom.push(parseFloat(d[i][j].value)); if (j == col - 1) right.push(parseFloat(d[i][j].value)); } }
var sides = newArray(up, buttom, left, right);
//提取格点 var latticeResult = newwork().init(0, 0);
for (var i = 0; i < row - 1; i++) { for (var j = 0; j < col - 1; j++) { var temp = newArray( d[i][j].value, d[i + 1][j].value, d[i][j + 1].value, d[i + 1][j + 1].value ); latticeResult.add(CalInGrid(temp)); console.log(latticeResult.toString("格点")); } }