坐标计算器

March 26, 2025

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>坐标计算器(仅限正数)</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; background-color: #f8f9fa; } .container { max-width: 900px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } h1 { text-align: center; color: #333; } h2 { color: #444; margin-top: 25px; font-size: 1.2em; } .form-group { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; font-weight: bold; } input, select { width: 100%; padding: 8px; box-sizing: border-box; border: 1px solid #ddd; border-radius: 4px; } .row { display: flex; gap: 15px; } .col { flex: 1; } button { display: block; width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; margin-top: 10px; } button:hover { background-color: #45a049; } table { width: 100%; border-collapse: collapse; margin-top: 15px; margin-bottom: 25px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: center; } th { background-color: #f2f2f2; color: #333; } tr:nth-child(even) { background-color: #f9f9f9; } .current { background-color: #e6f7ff; font-weight: bold; } .formula { margin: 20px 0; padding: 10px; background-color: #f0f0f0; border-radius: 4px; } .error { color: red; margin-top: 10px; padding: 10px; background-color: #ffeeee; border-radius: 4px; border-left: 3px solid red; display: none; } .results-container { display: flex; gap: 20px; flex-direction: column; } .current-value { padding: 10px; background-color: #e6f7ff; border-radius: 4px; text-align: center; font-weight: bold; margin: 15px 0; font-size: 1.1em; border: 1px solid #b8e0ff; } </style> </head> <body> <div class="container"> <h1>坐标计算器(仅限正数)</h1> <div class="formula"> <p>说明:输入初始X和Y坐标值,以及X和Y的步长,计算器将生成前后的坐标系列。</p> </div> <div class="row"> <div class="col"> <div class="form-group"> <label for="initialX">初始X坐标:</label> <input type="number" id="initialX" value="0" min="0" step="any"> </div> </div> <div class="col"> <div class="form-group"> <label for="stepX">X步长:</label> <input type="number" id="stepX" value="1" min="0" step="any"> </div> </div> </div> <div class="row"> <div class="col"> <div class="form-group"> <label for="initialY">初始Y坐标:</label> <input type="number" id="initialY" value="0" min="0" step="any"> </div> </div> <div class="col"> <div class="form-group"> <label for="stepY">Y步长:</label> <input type="number" id="stepY" value="1" min="0" step="any"> </div> </div> </div> <div class="row"> <div class="col"> <div class="form-group"> <label for="coordType">坐标类型:</label> <select id="coordType"> <option value="x">X坐标</option> <option value="y">Y坐标</option> </select> </div> </div> <div class="col"> <div class="form-group"> <label for="inputValue">输入值:</label> <input type="number" id="inputValue" min="0" step="any"> </div> </div> </div> <button onclick="calculate()">计算</button> <div id="errorMessage" class="error"></div> <div id="results" class="results-container" style="display: none;"> <div id="currentValueContainer" class="current-value"></div> <div> <h2>小于输入值的10个坐标</h2> <table id="smallerTable"> <thead> <tr> <th>X 坐标</th> <th>Y 坐标</th> </tr> </thead> <tbody id="smallerBody"></tbody> </table> </div> <div> <h2>大于输入值的10个坐标</h2> <table id="largerTable"> <thead> <tr> <th>X 坐标</th> <th>Y 坐标</th> </tr> </thead> <tbody id="largerBody"></tbody> </table> </div> </div> </div> <script> function showError(message) { const errorElement = document.getElementById('errorMessage'); errorElement.textContent = message; errorElement.style.display = 'block'; document.getElementById('results').style.display = 'none'; } function hideError() { document.getElementById('errorMessage').style.display = 'none'; } function validatePositive(value, name) { if (value < 0) { return `${name} 必须为正数或零`; } return null; } function calculate() { hideError(); // 获取输入值 const initialX = parseFloat(document.getElementById('initialX').value); const stepX = parseFloat(document.getElementById('stepX').value); const initialY = parseFloat(document.getElementById('initialY').value); const stepY = parseFloat(document.getElementById('stepY').value); const coordType = document.getElementById('coordType').value; const inputValue = parseFloat(document.getElementById('inputValue').value); // 验证数据 if (isNaN(initialX) || isNaN(stepX) || isNaN(initialY) || isNaN(stepY) || isNaN(inputValue)) { showError('请输入有效的数值'); return; } // 验证正数 let errorMsg = validatePositive(initialX, '初始X坐标') || validatePositive(stepX, 'X步长') || validatePositive(initialY, '初始Y坐标') || validatePositive(stepY, 'Y步长') || validatePositive(inputValue, '输入值'); if (errorMsg) { showError(errorMsg); return; } if ((coordType === 'x' && stepX === 0) || (coordType === 'y' && stepY === 0)) { showError('步长不能为0'); return; } // 根据输入值和选择的坐标类型计算最接近的n let n; if (coordType === 'x') { n = Math.round((inputValue - initialX) / stepX); } else { // y n = Math.round((inputValue - initialY) / stepY); } // 计算精确值(最接近inputValue的值) const exactX = initialX + stepX * n; const exactY = initialY + stepY * n; // 准备小于和大于的结果数组 const smallerResults = []; const largerResults = []; // 计算小于的结果 for (let i = n - 1; smallerResults.length < 10 && i >= 0; i--) { const x = initialX + stepX * i; const y = initialY + stepY * i; // 过滤掉负数结果 if (x < 0 || y < 0) continue; // 插入到开头,确保值从小到大排序 smallerResults.unshift({ x: x, y: y }); } // 计算大于的结果 for (let i = n + 1; largerResults.length < 10; i++) { const x = initialX + stepX * i; const y = initialY + stepY * i; // 过滤掉负数结果 if (x < 0 || y < 0) continue; largerResults.push({ x: x, y: y }); } // 显示当前值 const currentValueContainer = document.getElementById('currentValueContainer'); if (coordType === 'x') { currentValueContainer.textContent = `输入X坐标: ${inputValue} → 最接近值: X=${exactX.toFixed(6)}, Y=${exactY.toFixed(6)}`; } else { currentValueContainer.textContent = `输入Y坐标: ${inputValue} → 最接近值: X=${exactX.toFixed(6)}, Y=${exactY.toFixed(6)}`; } // 显示小于的结果 const smallerBody = document.getElementById('smallerBody'); smallerBody.innerHTML = ''; if (smallerResults.length === 0) { const row = document.createElement('tr'); const cell = document.createElement('td'); cell.colSpan = 2; cell.textContent = '没有小于该值的正数结果'; row.appendChild(cell); smallerBody.appendChild(row); } else { smallerResults.forEach(result => { const row = document.createElement('tr'); const xCell = document.createElement('td'); xCell.textContent = result.x.toFixed(6); const yCell = document.createElement('td'); yCell.textContent = result.y.toFixed(6); row.appendChild(xCell); row.appendChild(yCell); smallerBody.appendChild(row); }); } // 显示大于的结果 const largerBody = document.getElementById('largerBody'); largerBody.innerHTML = ''; if (largerResults.length === 0) { const row = document.createElement('tr'); const cell = document.createElement('td'); cell.colSpan = 2; cell.textContent = '没有大于该值的结果'; row.appendChild(cell); largerBody.appendChild(row); } else { largerResults.forEach(result => { const row = document.createElement('tr'); const xCell = document.createElement('td'); xCell.textContent = result.x.toFixed(6); const yCell = document.createElement('td'); yCell.textContent = result.y.toFixed(6); row.appendChild(xCell); row.appendChild(yCell); largerBody.appendChild(row); }); } // 显示结果区域 document.getElementById('results').style.display = 'flex'; } </script> </body> </html>