Skip to content

Commit eafc890

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

File tree

1 file changed

+33
-62
lines changed

1 file changed

+33
-62
lines changed

index.html

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,71 +3042,42 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30423042
<label for="parent-radius">Circle Radius:</label>
30433043
<input id="parent-radius" type="number" value="0" step="any">
30443044

3045-
<script>
3046-
function segmentArea(length, height, angle, radius) {
3047-
return angle * radius ** 2 - (radius - height) * length / 2;
3048-
}
3049-
3050-
function updateSegmentArea() {
3051-
const length = parseFloat(document.getElementById('chord-length').value);
3052-
const height = parseFloat(document.getElementById('segment-height').value);
3053-
let radius = parseFloat(document.getElementById('parent-radius').value);
3054-
3055-
if (isNaN(height) || isNaN(length) || isNaN(radius)) {
3056-
document.getElementById('segment-area').innerText = '';
3057-
return;
3058-
}
3059-
3060-
// Case 1: height and radius known
3061-
if (height > 0 && radius > 0) {
3062-
if (height > radius) {
3063-
document.getElementById('segment-area').innerText =
3064-
'The height of a circle segment is shorter than the parent radius.';
3065-
return;
3066-
}
3067-
3068-
const ratio = (radius - height) / radius;
3069-
const angle = Acos(ratio);
3070-
const length = 2 * Math.sqrt(radius ** 2 - (radius - height) ** 2);
3071-
const area = segmentArea(length, height, angle, radius);
3072-
3073-
document.getElementById('segment-area').innerText =
3074-
`Area: ${area.toFixed(5)} square units`;
3075-
}
3076-
3077-
// Case 2: height and chord known
3078-
else if (height > 0 && length > 0) {
3079-
const ratio = 2 * height / length;
3080-
3081-
if (ratio <= 0) {
3082-
document.getElementById('segment-area').innerText = '';
3083-
return;
3084-
}
3045+
<script>
3046+
function segmentArea(length, height, angle, radius) {
3047+
// Sector area minus triangle area
3048+
return angle * radius ** 2 - (radius - height) * length / 2;
3049+
}
30853050

3086-
if (ratio < 0.11 || ratio > 1) {
3087-
document.getElementById('segment-area').innerText = 'This ratio is out of range';
3088-
return;
3089-
}
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 {
3064+
document.getElementById('segment-area').innerText =
3065+
'Error: Provide either radius or chord length.';
3066+
return;
3067+
}
30903068

3091-
const angle = 2 * Atan(ratio);
3092-
const sine = sin(angle);
3093-
radius = length / (2 * sine);
3094-
const area = segmentArea(length, height, angle, radius);
3095-
3096-
if (ratio === 1) {
3097-
document.getElementById('segment-area').innerText =
3098-
`Area of semi-circle: ${area.toFixed(5)} square units`;
3099-
} else {
3100-
document.getElementById('segment-area').innerText =
3101-
`Area: ${area.toFixed(5)} square units`;
3102-
}
3103-
}
3104-
}
3069+
area = segmentArea(length, height, angle, radius);
31053070

3106-
document.getElementById('chord-length').addEventListener('input', updateSegmentArea);
3107-
document.getElementById('segment-height').addEventListener('input', updateSegmentArea);
3108-
document.getElementById('parent-radius').addEventListener('input', updateSegmentArea);
3109-
</script>
3071+
// Special semicircle cases
3072+
if (radius === height || height === length / 2) {
3073+
document.getElementById('segment-area').innerText =
3074+
`Area of semi-circle: ${area.toFixed(5)} square units`;
3075+
} else {
3076+
document.getElementById('segment-area').innerText =
3077+
`Area: ${area.toFixed(5)} square units`;
3078+
}
3079+
}
3080+
</script>
31103081
<p id="segment-area"></p>
31113082
</div>
31123083
</section>

0 commit comments

Comments
 (0)