|
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 |
| -constc=6; |
5 |
| -constr=0.5; |
| 1 | +/* |
| 2 | + * Let circleRadius is the radius of circle. |
| 3 | + * circleRadius is also the side length of the inscribed hexagon |
| 4 | + */ |
| 5 | +constcircleRadius=1; |
| 6 | + |
| 7 | +/** |
| 8 | + *@param {number} sideLength |
| 9 | + *@param {number} splitCounter |
| 10 | + *@return {number} |
| 11 | + */ |
| 12 | +functiongetNGonSideLength(sideLength,splitCounter){ |
| 13 | +if(splitCounter<=0){ |
| 14 | +returnsideLength; |
| 15 | +} |
| 16 | + |
| 17 | +consthalfSide=sideLength/2; |
6 | 18 |
|
7 |
| -constgetSideLength=(sideLength,count)=>{ |
8 |
| -if(count<=0)returnsideLength; |
9 |
| -constm=sideLength/2; |
| 19 | +// Liu Hui used the Gou Gu (Pythagorean theorem) theorem repetitively. |
| 20 | +constperpendicular=Math.sqrt((circleRadius**2)-(halfSide**2)); |
| 21 | +constexcessRadius=circleRadius-perpendicular; |
| 22 | +constsplitSideLength=Math.sqrt((excessRadius**2)+(halfSide**2)); |
10 | 23 |
|
11 |
| -// Liu Hui used the Gou Gu theorem repetitively. |
12 |
| -constg=Math.sqrt((r**2)-(m**2)); |
13 |
| -constj=r-g; |
| 24 | +returngetNGonSideLength(splitSideLength,splitCounter-1); |
| 25 | +} |
14 | 26 |
|
15 |
| -returngetSideLength(Math.sqrt((j**2)+(m**2)),count-1); |
16 |
| -}; |
| 27 | +/** |
| 28 | + *@param {number} splitCount |
| 29 | + *@return {number} |
| 30 | + */ |
| 31 | +functiongetNGonSideCount(splitCount){ |
| 32 | +// Liu Hui began with an inscribed hexagon (6-gon). |
| 33 | +consthexagonSidesCount=6; |
17 | 34 |
|
18 |
| -constgetSideCount=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 | +returnhexagonSidesCount*(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 | exportdefaultfunctionliuHui(splitCount=1){
|
36 |
| -constsideLength=getSideLength(r,splitCount-1); |
37 |
| -constsideCount=getSideCount(splitCount-1); |
38 |
| -constp=sideLength*sideCount; |
39 |
| -constarea=(p/2)*r; |
| 47 | +constnGonSideLength=getNGonSideLength(circleRadius,splitCount-1); |
| 48 | +constnGonSideCount=getNGonSideCount(splitCount-1); |
| 49 | +constnGonPerimeter=nGonSideLength*nGonSideCount; |
| 50 | +constapproximateCircleArea=(nGonPerimeter/2)*circleRadius; |
40 | 51 |
|
41 |
| -returnarea/(r**2); |
| 52 | +// Return approximate value of pi. |
| 53 | +returnapproximateCircleArea/(circleRadius**2); |
42 | 54 | }
|