@@ -3289,88 +3289,81 @@ <h3 itemprop="name" style="margin:7px">Area of a Circle Segment</h3>
32893289< br >
32903290< br >
32913291< div style ="margin:12px " id ="circle-segment_area_calculator ">
3292- < label for ="segment-height "> Segment Height:</ label >
3293- < input id ="segment-height " type ="number " value ="0 " step ="any ">
3294- < br >
3295- < label for ="chord-length "> Chord Length:</ label >
3296- < input id ="chord-length " type ="number " value ="0 " step ="any ">
3297- < br >
3298- < label for ="parent-radius "> Circle Radius:</ label >
3299- < input id ="parent-radius " type ="number " value ="0 " step ="any ">
3300- < script >
3301- function segmentArea ( length , height , angle , radius ) {
3302- return angle * radius ** 2 - ( radius - height ) * length / 2 ;
3303- }
3292+ < label for ="segment-height "> Segment Height:</ label >
3293+ < input id ="segment-height " type ="number " value ="0 " step ="any ">
3294+ < br >
3295+ < label for ="chord-length "> Chord Length:</ label >
3296+ < input id ="chord-length " type ="number " value ="0 " step ="any ">
3297+ < br >
3298+ < label for ="parent-radius "> Circle Radius:</ label >
3299+ < input id ="parent-radius " type ="number " value ="0 " step ="any ">
3300+
3301+ < script >
3302+ function segmentArea ( length , height , angle , radius ) {
3303+ return angle * radius ** 2 - ( radius - height ) * length / 2 ;
3304+ }
33043305
3305- function updateSegmentArea ( ) {
3306- const length = parseFloat ( document . getElementById ( 'chord-length' ) . value ) ;
3307- const height = parseFloat ( document . getElementById ( 'segment-height' ) . value ) ;
3308- const radius = parseFloat ( document . getElementById ( 'parent-radius' ) . value ) ;
3309-
3310- if ( isNaN ( height ) || isNaN ( length ) || isNaN ( radius ) ) {
3311- document . getElementById ( 'segment-area' ) . innerText = '' ;
3312- return ;
3313- }
3306+ function updateSegmentArea ( ) {
3307+ const length = parseFloat ( document . getElementById ( 'chord-length' ) . value ) ;
3308+ const height = parseFloat ( document . getElementById ( 'segment-height' ) . value ) ;
3309+ let radius = parseFloat ( document . getElementById ( 'parent-radius' ) . value ) ;
3310+
3311+ if ( isNaN ( height ) || isNaN ( length ) || isNaN ( radius ) ) {
3312+ document . getElementById ( 'segment-area' ) . innerText = '' ;
3313+ return ;
3314+ }
3315+
3316+ // Case 1: height and radius known
3317+ if ( height > 0 && radius > 0 ) {
3318+ if ( height > radius ) {
3319+ document . getElementById ( 'segment-area' ) . innerText =
3320+ 'The height of a circle segment cannot exceed the radius.' ;
3321+ return ;
3322+ }
33143323
3315- // If height and parent radius are known
3316-
3317- if ( height > 0 || radius > 0 ) {
3318-
3319- // Segment validity check
3324+ const ratio = ( radius - height ) / radius ;
3325+ const angle = Acos ( ratio ) ;
3326+ const chordLength = 2 * Math . sqrt ( radius ** 2 - ( radius - height ) ** 2 ) ;
3327+ const area = segmentArea ( chordLength , height , angle , radius ) ;
33203328
3329+ document . getElementById ( 'segment-area' ) . innerText =
3330+ `Area: ${ area . toFixed ( 5 ) } square units` ;
3331+ }
33213332
3322- if ( height > radius ) {
3323- document . getElementById ( 'segment-area' ) . innerText = 'The height of a circle segment is shorter than the parent radius.' ;
3324- return ;
3325- }
3333+ // Case 2: height and chord known
3334+ else if ( height > 0 && length > 0 ) {
3335+ const ratio = 2 * height / length ;
33263336
3327- const ratio = ( radius - height ) / radius ;
3328- const angle = parseFloat ( Acos ( ratio ) ) ;
3329- const length = parseFloat ( 2 * sqrt ( radius ** 2 - ( radius - height ) ** 2 ) ) ;
3330-
3331- document . getElementById ( 'segment-area' ) . innerText =
3332- `Area: ${ area . toFixed ( 5 ) } square units` ;
3333- }
3334-
3335-
3336-
3337- else if ( height > 0 || length > 0 ) {
3338-
3339- const ratio = 2 * height / length ;
3340-
3341- // Segment validity check
3337+ if ( ratio <= 0 ) {
3338+ document . getElementById ( 'segment-area' ) . innerText = '' ;
3339+ return ;
3340+ }
33423341
3343- if ( ratio === 0 ) {
3344- document . getElementById ( 'segment-area' ) . innerText = '' ;
3345- return ;
3346- }
3342+ if ( ratio < 0.11 || ratio > 1 ) {
3343+ document . getElementById ( 'segment-area' ) . innerText = 'This ratio is out of range ' ;
3344+ return ;
3345+ }
33473346
3348- if ( ratio < 0.11 || ratio > 1 ) {
3349- document . getElementById ( 'segment-area' ) . innerText = 'This ratio is out of range' ;
3350- return ;
3351- }
3352-
3353- const angle = parseFloat ( 2 * Atan ( ratio ) ) ;
3354- const sine = parseFloat ( sin ( angle ) ) ;
3355- const radius = parseFloat ( ( length / 2 / sine ) . toFixed ( 5 ) ) ;
3356- const area = segmentArea ( length , height , angle , radius ) ;
3357-
3358- if ( ratio === 1 ) {
3359- document . getElementById ( 'segment-area' ) . innerText =
3360- `Area of semi-circle: ${ area . toFixed ( 5 ) } square units` ;
3361- }
3362-
3363- document . getElementById ( 'segment-area' ) . innerText =
3364- `Area: ${ area . toFixed ( 5 ) } square units` ;
3365-
3366- }
3347+ const angle = 2 * Math . atan ( ratio ) ;
3348+ const sine = sin ( angle ) ;
3349+ radius = length / ( 2 * sine ) ;
3350+ const area = segmentArea ( length , height , angle , radius ) ;
3351+
3352+ if ( ratio === 1 ) {
3353+ document . getElementById ( 'segment-area' ) . innerText =
3354+ `Area of semi-circle: ${ area . toFixed ( 5 ) } square units` ;
3355+ } else {
3356+ document . getElementById ( 'segment-area' ) . innerText =
3357+ `Area: ${ area . toFixed ( 5 ) } square units` ;
3358+ }
3359+ }
3360+ }
33673361
3368- }
3369-
3370- document . getElementById ( 'chord-length' ) . addEventListener ( 'input' , updateSegmentArea ) ;
3371- document . getElementById ( 'segment-height' ) . addEventListener ( 'input' , updateSegmentArea ) ;
3372- </ script >
3373- < p id ="segment-area "> </ p >
3362+ document . getElementById ( 'chord-length' ) . addEventListener ( 'input' , updateSegmentArea ) ;
3363+ document . getElementById ( 'segment-height' ) . addEventListener ( 'input' , updateSegmentArea ) ;
3364+ document . getElementById ( 'parent-radius' ) . addEventListener ( 'input' , updateSegmentArea ) ;
3365+ </ script >
3366+ < p id ="segment-area "> </ p >
33743367</ div >
33753368</ section >
33763369< br >
0 commit comments