Skip to content

Commit 7b7b807

Browse files
authored
Update index.html
Segment area calculator
1 parent eafc890 commit 7b7b807

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

index.html

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,35 +3043,60 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30433043
<input id="parent-radius" type="number" value="0" step="any">
30443044

30453045
<script>
3046-
function segmentArea(length, height, angle, radius) {
3047-
// Sector area minus triangle area
3048-
return angle * radius ** 2 - (radius - height) * length / 2;
3049-
}
3046+
function segmentArea() {
3047+
// Read inputs
3048+
let h = parseFloat(document.getElementById('segment-height').value);
3049+
let r = parseFloat(document.getElementById('parent-radius').value);
3050+
let l = parseFloat(document.getElementById('chord-length').value);
3051+
3052+
// Detect which two are provided
3053+
let known = [!isNaN(h), !isNaN(r), !isNaN(l)].filter(Boolean).length;
3054+
if (known < 2) {
3055+
document.getElementById('segment-area').innerText =
3056+
'Error: Provide one more known property';
3057+
return;
3058+
}
30503059

3051-
function calculateSegmentArea(height, radius = null, length = null) {
3052-
let angle, area;
3053-
3054-
if (radius) {
3055-
// Case 1: height + radius → derive chord length
3056-
const ratio = (radius - height) / radius;
3057-
angle = Acos(ratio);
3058-
length = 2 * radius * sin(angle / 2);
3059-
} else if (length) {
3060-
// Case 2: height + chord length → derive radius
3061-
radius = (height / 2) + (length ** 2) / (8 * height);
3062-
angle = 2 * Asin(length / (2 * radius));
3063-
} else {
3060+
// Case 1: height + radius known → derive length
3061+
if (!isNaN(h) && !isNaN(r) && isNaN(l)) {
3062+
let angle = Acos((r - h) / r);
3063+
l = 2 * r * sin(angle);
3064+
document.getElementById('chord-length').value = l.toFixed(5);
3065+
}
3066+
3067+
// Case 2: height + length known → derive radius
3068+
if (!isNaN(h) && !isNaN(l) && isNaN(r)) {
3069+
r = (l ** 2 + 4 * h ** 2) / (8 * h);
3070+
document.getElementById('parent-radius').value = r.toFixed(5);
3071+
}
3072+
3073+
// Case 3: length + radius known → derive height
3074+
if (!isNaN(l) && !isNaN(r) && isNaN(h)) {
3075+
h = r - Math.sqrt(r ** 2 - (l / 2) ** 2);
3076+
document.getElementById('segment-height').value = h.toFixed(5);
3077+
}
3078+
3079+
// Validity checks
3080+
if (h > r || l < 2 * h || r < h) {
30643081
document.getElementById('segment-area').innerText =
3065-
'Error: Provide either radius or chord length.';
3082+
'Invalid input: must satisfy h ≤ r, l ≥ 2h, r ≥ h.';
30663083
return;
30673084
}
30683085

3069-
area = segmentArea(length, height, angle, radius);
3086+
// Compute angle and area
3087+
let angle = Acos((r - h) / r);
3088+
let area = angle * r ** 2 - (r - h) * (l / 2);
30703089

3071-
// Special semicircle cases
3072-
if (radius === height || height === length / 2) {
3090+
// Output with semicircle reminders
3091+
if (h === r) {
3092+
document.getElementById('segment-area').innerText =
3093+
`Semicircle area: ${area.toFixed(5)} square units`;
3094+
} else if (h === l / 2) {
3095+
document.getElementById('segment-area').innerText =
3096+
`Semicircle area: ${area.toFixed(5)} square units`;
3097+
} else if (l === 2 * r) {
30733098
document.getElementById('segment-area').innerText =
3074-
`Area of semi-circle: ${area.toFixed(5)} square units`;
3099+
`Semicircle area: ${area.toFixed(5)} square units`;
30753100
} else {
30763101
document.getElementById('segment-area').innerText =
30773102
`Area: ${area.toFixed(5)} square units`;

0 commit comments

Comments
 (0)