割圆术计算 PI

以六边形开始,使用勾股定理。

迭代计算多边形边长

const getSideLength = (sideLength, count) => {
if (count <= 0) return sideLength;
const m = sideLength / 2;
const g = Math.sqrt((0.5 ** 2) - (m ** 2));
const j = 0.5 - g;
return getSideLength(Math.sqrt((j ** 2) + (m ** 2)), count - 1);
};

const pi = (splitCount = 0) => {
const sideLength = getSideLength(0.5, splitCount);
const sideCount = 6 * (splitCount ? 2 ** splitCount : 1);
return sideLength * sideCount;
};