@@ -3034,87 +3034,89 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30343034< meta itemprop ="disambiguatingDescription " content ="Area based on the A(circle)=3.2*radius^2 formula, instead of the pi=3.14... approximation ">
30353035< meta itemprop ="usageInfo " content ="Enter the segment height and either the chord length or the radius of the parent circle. The other value will be discarded in the result. ">
30363036< label for ="segment-height "> Segment Height:</ label >
3037- < input id ="segment-height " type ="number " step ="any " oninput =" segmentArea() " >
3037+ < input id ="segment-height " type ="number " step ="any ">
30383038< br >
30393039< label for ="chord-length "> Chord Length:</ label >
3040- < input id ="chord-length " type ="number " step ="any " oninput =" segmentArea() " >
3040+ < input id ="chord-length " type ="number " step ="any ">
30413041< br >
30423042< label for ="parent-radius "> Circle Radius:</ label >
3043- < input id ="parent-radius " type ="number " step ="any " oninput =" segmentArea() " >
3043+ < input id ="parent-radius " type ="number " step ="any ">
30443044< br >
30453045< script >
3046- let calculated = false ;
3046+ let autoFilledField = null ;
30473047
30483048function segmentArea ( ) {
3049- if ( calculated ) {
3050- // Clear everything on next input after a calculation
3051- document . getElementById ( 'segment-height' ) . value = "" ;
3052- document . getElementById ( 'parent-radius' ) . value = "" ;
3053- document . getElementById ( 'chord-length' ) . value = "" ;
3054- document . getElementById ( 'segment-area' ) . innerText = "" ;
3055- calculated = false ; // reset flag
3049+ const hInput = document . getElementById ( 'segment-height' ) ;
3050+ const rInput = document . getElementById ( 'parent-radius' ) ;
3051+ const lInput = document . getElementById ( 'chord-length' ) ;
3052+ const output = document . getElementById ( 'segment-area' ) ;
3053+
3054+ let h = parseFloat ( hInput . value ) ;
3055+ let r = parseFloat ( rInput . value ) ;
3056+ let l = parseFloat ( lInput . value ) ;
3057+
3058+ // If user edits the auto-filled field → reset
3059+ const activeElement = document . activeElement ;
3060+ if ( autoFilledField && activeElement . id === autoFilledField ) {
3061+ hInput . value = "" ;
3062+ rInput . value = "" ;
3063+ lInput . value = "" ;
3064+ output . innerText = "" ;
3065+ autoFilledField = null ;
30563066 return ;
30573067 }
30583068
3059- let h = parseFloat ( document . getElementById ( 'segment-height' ) . value ) ;
3060- let r = parseFloat ( document . getElementById ( 'parent-radius' ) . value ) ;
3061- let l = parseFloat ( document . getElementById ( 'chord-length' ) . value ) ;
3062-
3069+ // Count known values
30633070 let known = [ ! isNaN ( h ) , ! isNaN ( r ) , ! isNaN ( l ) ] . filter ( Boolean ) . length ;
30643071 if ( known < 2 ) {
3065- document . getElementById ( 'segment-area' ) . innerText = "" ;
3072+ output . innerText = "" ;
30663073 return ;
30673074 }
30683075
30693076 // Case 1: height + radius known → derive length
30703077 if ( ! isNaN ( h ) && ! isNaN ( r ) && isNaN ( l ) ) {
30713078 let angle = Acos ( ( r - h ) / r ) ;
30723079 l = 2 * r * sin ( angle ) ;
3073- document . getElementById ( 'chord-length' ) . value = l . toFixed ( 5 ) ;
3080+ lInput . value = l . toFixed ( 5 ) ;
3081+ autoFilledField = "chord-length" ;
30743082 }
30753083
30763084 // Case 2: height + length known → derive radius
30773085 if ( ! isNaN ( h ) && ! isNaN ( l ) && isNaN ( r ) ) {
30783086 r = ( l ** 2 + 4 * h ** 2 ) / ( 8 * h ) ;
3079- document . getElementById ( 'parent-radius' ) . value = r . toFixed ( 5 ) ;
3087+ rInput . value = r . toFixed ( 5 ) ;
3088+ autoFilledField = "parent-radius" ;
30803089 }
30813090
30823091 // Case 3: length + radius known → derive height
30833092 if ( ! isNaN ( l ) && ! isNaN ( r ) && isNaN ( h ) ) {
30843093 h = r - Math . sqrt ( r ** 2 - ( l / 2 ) ** 2 ) ;
3085- document . getElementById ( 'segment-height' ) . value = h . toFixed ( 5 ) ;
3094+ hInput . value = h . toFixed ( 5 ) ;
3095+ autoFilledField = "segment-height" ;
30863096 }
30873097
30883098 // Validity checks
3089- if ( h > r || l < 2 * h || r < h ) {
3090- document . getElementById ( 'segment-area' ) . innerText =
3091- 'The height of the segment must be shorter than the radius and half the length.' ;
3092-
3093- // Clear everything so it doesn't get stuck
3094- document . getElementById ( 'segment-height' ) . value = "" ;
3095- document . getElementById ( 'parent-radius' ) . value = "" ;
3096- document . getElementById ( 'chord-length' ) . value = "" ;
3097-
3098- calculated = false ; // reset flag
3099- return ;
3100- }
3099+ if ( h > r || l < 2 * h || r < h ) {
3100+ output . innerText = 'Invalid input: must satisfy h ≤ r, l ≥ 2h, r ≥ h.' ;
3101+ return ;
3102+ }
31013103
31023104 // Compute angle and area
31033105 let angle = Acos ( ( r - h ) / r ) ;
31043106 let area = angle * r ** 2 - ( r - h ) * ( l / 2 ) ;
31053107
31063108 // Output with semicircle reminders
31073109 if ( h === r || h === l / 2 || l === 2 * r ) {
3108- document . getElementById ( 'segment-area' ) . innerText =
3109- `Semicircle area: ${ area . toFixed ( 5 ) } square units` ;
3110+ output . innerText = `Semicircle area: ${ area . toFixed ( 5 ) } square units` ;
31103111 } else {
3111- document . getElementById ( 'segment-area' ) . innerText =
3112- `Area: ${ area . toFixed ( 5 ) } square units` ;
3112+ output . innerText = `Area: ${ area . toFixed ( 5 ) } square units` ;
31133113 }
3114-
3115- // Mark that a calculation has been completed
3116- calculated = true ;
31173114}
3115+
3116+ // Attach listeners to all inputs
3117+ document . getElementById ( 'segment-height' ) . addEventListener ( 'input' , segmentArea ) ;
3118+ document . getElementById ( 'parent-radius' ) . addEventListener ( 'input' , segmentArea ) ;
3119+ document . getElementById ( 'chord-length' ) . addEventListener ( 'input' , segmentArea ) ;
31183120</ script >
31193121 < p id ="segment-area "> </ p >
31203122</ div >
0 commit comments