|
1 |
| -// Liu Hui began with an inscribed hexagon. |
2 |
| -// Let r is the radius of circle. |
3 |
| -// r is also the side length of the inscribed hexagon |
4 |
| -const c = 6; |
5 |
| -const r = 0.5; |
| 1 | +/* |
| 2 | + * Let circleRadius is the radius of circle. |
| 3 | + * circleRadius is also the side length of the inscribed hexagon |
| 4 | + */ |
| 5 | +const circleRadius = 1; |
| 6 | + |
| 7 | +/** |
| 8 | + * @param {number} sideLength |
| 9 | + * @param {number} splitCounter |
| 10 | + * @return {number} |
| 11 | + */ |
| 12 | +function getNGonSideLength(sideLength, splitCounter) { |
| 13 | + if (splitCounter <= 0) { |
| 14 | + return sideLength; |
| 15 | + } |
| 16 | + |
| 17 | + const halfSide = sideLength / 2; |
6 | 18 |
|
7 |
| -const getSideLength = (sideLength, count) => { |
8 |
| - if (count <= 0) return sideLength; |
9 |
| - const m = sideLength / 2; |
| 19 | + // Liu Hui used the Gou Gu (Pythagorean theorem) theorem repetitively. |
| 20 | + const perpendicular = Math.sqrt((circleRadius ** 2) - (halfSide ** 2)); |
| 21 | + const excessRadius = circleRadius - perpendicular; |
| 22 | + const splitSideLength = Math.sqrt((excessRadius ** 2) + (halfSide ** 2)); |
10 | 23 |
|
11 |
| - // Liu Hui used the Gou Gu theorem repetitively. |
12 |
| - const g = Math.sqrt((r ** 2) - (m ** 2)); |
13 |
| - const j = r - g; |
| 24 | + return getNGonSideLength(splitSideLength, splitCounter - 1); |
| 25 | +} |
14 | 26 |
|
15 |
| - return getSideLength(Math.sqrt((j ** 2) + (m ** 2)), count - 1); |
16 |
| -}; |
| 27 | +/** |
| 28 | + * @param {number} splitCount |
| 29 | + * @return {number} |
| 30 | + */ |
| 31 | +function getNGonSideCount(splitCount) { |
| 32 | + // Liu Hui began with an inscribed hexagon (6-gon). |
| 33 | + const hexagonSidesCount = 6; |
17 | 34 |
|
18 |
| -const getSideCount = splitCount => c * (splitCount ? 2 ** splitCount : 1); |
| 35 | + // On every split iteration we make N-gons: 6-gon, 12-gon, 24-gon, 48-gon and so on. |
| 36 | + return hexagonSidesCount * (splitCount ? 2 ** splitCount : 1); |
| 37 | +} |
19 | 38 |
|
20 | 39 | /**
|
21 | 40 | * Calculate the π value using Liu Hui's π algorithm
|
22 | 41 | *
|
23 |
| - * Liu Hui argued: |
24 |
| - * Multiply one side of a hexagon by the radius (of its circumcircle), |
25 |
| - * then multiply this by three, to yield the area of a dodecagon; if we |
26 |
| - * cut a hexagon into a dodecagon, multiply its side by its radius, then |
27 |
| - * again multiply by six, we get the area of a 24-gon; the finer we cut, |
28 |
| - * the smaller the loss with respect to the area of circle, thus with |
29 |
| - * further cut after cut, the area of the resulting polygon will coincide |
30 |
| - * and become one with the circle; there will be no loss |
31 |
| - * |
32 |
| - * @param {number} splitCount repeat times |
| 42 | + * @param {number} splitCount - number of times we're going to split 6-gon. |
| 43 | + * On each split we will receive 12-gon, 24-gon and so on. |
33 | 44 | * @return {number}
|
34 | 45 | */
|
35 | 46 | export default function liuHui(splitCount = 1) {
|
36 |
| - const sideLength = getSideLength(r, splitCount - 1); |
37 |
| - const sideCount = getSideCount(splitCount - 1); |
38 |
| - const p = sideLength * sideCount; |
39 |
| - const area = (p / 2) * r; |
| 47 | + const nGonSideLength = getNGonSideLength(circleRadius, splitCount - 1); |
| 48 | + const nGonSideCount = getNGonSideCount(splitCount - 1); |
| 49 | + const nGonPerimeter = nGonSideLength * nGonSideCount; |
| 50 | + const approximateCircleArea = (nGonPerimeter / 2) * circleRadius; |
40 | 51 |
|
41 |
| - return area / (r ** 2); |
| 52 | + // Return approximate value of pi. |
| 53 | + return approximateCircleArea / (circleRadius ** 2); |
42 | 54 | }
|
0 commit comments