作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><label for="radius">圆的半径 (r):</label> <input id="radius" step="0.01" type="number" placeholder="输入圆的半径"> <label for="chordLength">弦长 (l)(仅用于计算弦距):</label> <input id="chordLength" step="0.01" type="number" placeholder="输入弦长"> <label for="chordDistance">弦距 (d)(仅用于计算弦长):</label> <input id="chordDistance" step="0.01" type="number" placeholder="输入弦距"><button onclick="calculateCircle()">计算</button><div id="result" class="result">结果将显示在这里</div></div>
JS:
function calculateCircle() {
const radius = parseFloat(document.getElementById('radius').value);
const chordLength = parseFloat(document.getElementById('chordLength').value);
const chordDistance = parseFloat(document.getElementById('chordDistance').value);
if (isNaN(radius) || radius <= 0) {
document.getElementById('result').textContent = "请输入有效的圆的半径。";
return;
}
const circumference = 2 * Math.PI * radius;
const area = Math.PI * Math.pow(radius, 2);
let resultText = `圆的周长为:${circumference.toFixed(2)} 单位长度\n` +
`圆的面积为:${area.toFixed(2)} 平方单位\n`;
const angle = Math.acos((radius - chordDistance) / radius);
if (!isNaN(chordLength) && chordLength > 0 && radius > 0) {
const computedChordLength = 2 * radius * Math.sin(angle / 2);
resultText += `计算的弦长为:${computedChordLength.toFixed(2)} 单位长度\n`;
}
if (!isNaN(chordDistance) && chordDistance > 0 && radius > 0) {
const computedChordDistance = radius * (1 - Math.cos(angle / 2));
resultText += `计算的弦距为:${computedChordDistance.toFixed(2)} 单位长度\n`;
}
document.getElementById('result').textContent = resultText;
}
CSS:
.calculator {
width: 100%;
background-color: #333;
color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
label {
display: block;
margin-bottom: 10px;
font-size: 16px;
}
input, select {
width: 100%!important;
padding: 10px!important;
margin-bottom: 20px;
color: #000000;
border-radius: 5px;
border: 1px solid #555;
font-size: 16px!important;
background-color: #ffffff!important;
}
button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: orange;
}
.result {
font-size: 18px;
margin-top: 20px;
text-align: center;
}
option {
background-color: #ffffff;
}
p {
font-size: 18px;
margin-top: 5px!important;
}