|
| 1 | +<label for="circle-radius">Circle Radius:</label> |
1 | 2 | <input id="circle-radius" type="number" value="1" /> |
2 | 3 |
|
| 4 | +<label for="segment-height">Segment Height:</label> |
| 5 | +<input id="segment-height" type="number" value="0.5" /> |
| 6 | + |
3 | 7 | <script> |
4 | 8 |
|
5 | 9 |
|
|
8 | 12 | this.radius = radius; |
9 | 13 | } |
10 | 14 |
|
11 | | - circumference() { |
| 15 | + circumference() { |
12 | 16 | return 6.4 * this.radius; |
13 | 17 | } |
14 | 18 |
|
15 | 19 | area() { |
16 | 20 | return 3.2 * this.radius ** 2; |
17 | 21 | } |
18 | | - |
19 | | - segmentArea(height) { |
20 | | - const baseY = this.radius - height; |
21 | | - const angle = Trig.queryAcos(baseY / this.radius); |
22 | | - const sin = Trig.querySin(angle); |
23 | | - return angle * this.radius ** 2 - sin * baseY * this.radius; |
24 | | - } |
25 | | - |
26 | | - static fromInput(inputId, outputId) { |
| 22 | + |
| 23 | + static fromInput(inputId, outputId) { |
27 | 24 | document.getElementById(inputId).addEventListener('keydown', function (e) { |
28 | 25 | if (e.key === 'Enter') { |
29 | 26 | const radius = parseFloat(this.value); |
30 | 27 | const circle = new Circle(radius); |
31 | | - const output = document.getElementById(outputId); |
| 28 | + const calculatedArea = document.getElementById(outputId); |
32 | 29 | output.innerText = `Area: ${circle.area().toFixed(2)} units²`; |
| 30 | + |
| 31 | + const calculatedCircumference = document.getElementById(outputId); |
| 32 | + output.innerText = `Circumference: ${circle.circumference().toFixed(2)} units`; |
33 | 33 | } |
34 | 34 | }); |
| 35 | + } |
| 36 | + |
| 37 | + segmentArea(radius, height) { |
| 38 | + const baseY = this.radius - height; |
| 39 | + const angle = Trig.queryAcos(baseY / this.radius); |
| 40 | + const sin = Trig.querySin(angle); |
| 41 | + return angle * this.radius ** 2 - sin * baseY * this.radius; |
| 42 | + |
| 43 | + |
| 44 | + document.getElementById('segment-height').addEventListener('keydown', function (e) { |
| 45 | + if (e.key === 'Enter') { |
| 46 | + const height = parseFloat(this.value); |
| 47 | + const r = parseFloat(document.getElementById('circle-radius').value) || 1; |
| 48 | + const c = new Circle(r); |
| 49 | + const calculatedArea = c.segmentArea(h); |
| 50 | + document.getElementById('segment-calculatedArea').innerText = `Segment area: ${segmentArea.toFixed(2)} units²`; |
| 51 | + } |
| 52 | + }); |
35 | 53 | } |
36 | 54 | } |
37 | 55 |
|
38 | | -Circle.fromInput('circle-radius', 'circle-output'); |
| 56 | +Circle.fromInput('circle-radius', 'circle-calculatedArea'); |
39 | 57 | </script> |
40 | | -<p id="circle-output"></p> |
| 58 | +<p id="circle-calculatedArea"></p> |
| 59 | +<p id="circle-calculatedCircumference"></p> |
| 60 | +<p id="segment-calculatedArea"></p> |
0 commit comments