@@ -3034,6 +3034,7 @@ <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+ < label for ="segment-height "> Segment Height:</ label >
30373038< input id ="segment-height " type ="number " step ="any ">
30383039< br >
30393040< label for ="chord-length "> Chord Length:</ label >
@@ -3044,30 +3045,37 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30443045< br >
30453046< script >
30463047let autoFilledField = null ;
3048+ let userEntered = { h : false , l : false , r : false } ;
30473049
30483050function segmentArea ( ) {
30493051 const hInput = document . getElementById ( 'segment-height' ) ;
30503052 const lInput = document . getElementById ( 'chord-length' ) ;
30513053 const rInput = document . getElementById ( 'parent-radius' ) ;
30523054 const output = document . getElementById ( 'segment-area' ) ;
30533055
3056+ // Detect which field the user is typing in
3057+ const activeElement = document . activeElement ;
3058+ if ( activeElement . id === "segment-height" ) userEntered . h = true ;
3059+ if ( activeElement . id === "chord-length" ) userEntered . l = true ;
3060+ if ( activeElement . id === "parent-radius" ) userEntered . r = true ;
3061+
30543062 let h = parseFloat ( hInput . value ) ;
30553063 let l = parseFloat ( lInput . value ) ;
30563064 let r = parseFloat ( rInput . value ) ;
30573065
30583066 // Reset if user edits the auto-filled field
3059- const activeElement = document . activeElement ;
30603067 if ( autoFilledField && activeElement . id === autoFilledField ) {
30613068 hInput . value = "" ;
30623069 lInput . value = "" ;
30633070 rInput . value = "" ;
30643071 output . innerText = "" ;
30653072 autoFilledField = null ;
3073+ userEntered = { h : false , l : false , r : false } ;
30663074 return ;
30673075 }
30683076
3069- // Workflow A: height + radius
3070- if ( ! isNaN ( h ) && ! isNaN ( r ) ) {
3077+ // Workflow A: height + radius → derive length
3078+ if ( userEntered . h && userEntered . r && ! userEntered . l && ! isNaN ( h ) && ! isNaN ( r ) ) {
30713079 let angle = Acos ( ( r - h ) / r ) ;
30723080 l = 2 * r * sin ( angle ) ;
30733081 lInput . value = l . toFixed ( 5 ) ;
@@ -3080,8 +3088,8 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30803088 return ;
30813089 }
30823090
3083- // Workflow B: height + length
3084- if ( ! isNaN ( h ) && ! isNaN ( l ) ) {
3091+ // Workflow B: height + length → derive radius
3092+ if ( userEntered . h && userEntered . l && ! userEntered . r && ! isNaN ( h ) && ! isNaN ( l ) ) {
30853093 r = ( l ** 2 + 4 * h ** 2 ) / ( 8 * h ) ;
30863094 rInput . value = r . toFixed ( 5 ) ;
30873095 autoFilledField = "parent-radius" ;
@@ -3094,8 +3102,8 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
30943102 return ;
30953103 }
30963104
3097- // Workflow C: length + radius
3098- if ( ! isNaN ( l ) && ! isNaN ( r ) ) {
3105+ // Workflow C: length + radius → derive height
3106+ if ( userEntered . l && userEntered . r && ! userEntered . h && ! isNaN ( l ) && ! isNaN ( r ) ) {
30993107 h = r - Math . sqrt ( r ** 2 - ( l / 2 ) ** 2 ) ;
31003108 hInput . value = h . toFixed ( 5 ) ;
31013109 autoFilledField = "segment-height" ;
@@ -3108,7 +3116,12 @@ <h3 itemprop="eduQuestionType" style="margin:7px">Area of a Circle Segment</h3>
31083116 return ;
31093117 }
31103118
3111- }
3119+ // If only one field filled
3120+ if ( [ ! isNaN ( h ) , ! isNaN ( l ) , ! isNaN ( r ) ] . filter ( Boolean ) . length < 2 ) {
3121+ output . innerText = "Enter one more value to compute." ;
3122+ }
3123+ }
3124+
31123125// Attach listeners
31133126document . getElementById ( 'segment-height' ) . addEventListener ( 'input' , segmentArea ) ;
31143127document . getElementById ( 'chord-length' ) . addEventListener ( 'input' , segmentArea ) ;
0 commit comments