## 2016年11月24日木曜日

### 数学 - JavaScript - 連分数・円周率・ベルヌーイ数 - 連分数による無理数の定義(正多角形と円周率)

コード(Emacs)

HTML5

```<div id="graph0"></div>

<br>
<label for="n0">n = </label>
<input id="n0" type="number" min="0" step="1" value="0">
(6 + n 角形)

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.6/d3.min.js" integrity="sha256-5idA201uSwHAROtCops7codXJ0vja+6wbBrZdQ6ETQc=" crossorigin="anonymous"></script>
<script src="sample10.js"></script>

```

JavaScript

```{
let div_graph = document.querySelector('#graph0'),
svg,
width = 600,
height = 600,
r = 300,
input_n = document.querySelector('#n0'),
div_output = document.querySelector('#output0');

xscale = d3.scaleLinear()
.domain([-300, 300])
.range([0, 600]);
yscale = d3.scaleLinear()
.domain([-300, 300])
.range([600, 0]);

let pi_gon = (n) => {
let gon = (n) => {
if (n === 0) {
return 1;
}
return Math.sqrt(2 - Math.sqrt(4 - Math.pow(gon(n - 1), 2)));
};
return 3 * Math.pow(2, n) * gon(n);
};

let points = (n) => {
let result = [],
a = 2 * Math.PI / n;

for (let i = 0; i < Math.PI * 2; i += a) {
result.push([r * Math.cos(i), r * Math.sin(i)]);
}
return result;
};
let output = () => {
let n = parseInt(input_n.value, 10),
a = 6 + n,
ps = points(a);

div_graph.innerHTML = '';
svg = d3.select('#graph0')
.append('svg')
.attr('width', width)
.attr('height', height);

console.log(ps.length);
svg.selectAll('line')
.data(ps.slice(0, -1))
.enter()
.append('line')
.attr('x1', (d) => xscale(d[0]))
.attr('y1', (d) => yscale(d[1]))
.attr('x2', (d, i) => xscale(ps[i + 1][0]))
.attr('y2', (d, i) => yscale(ps[i + 1][1]))
.attr('stroke', 'blue');

svg.append('circle')
.attr('cx', xscale(0))
.attr('cy', yscale(0))
.attr('r', xscale(300) - xscale(0))
.attr('fill', 'rgba(0, 255, 0, 0.1)');

div_output.innerHTML = pi_gon(n);
};

input_n.onchange = output;

output();
}
```

(6 + n 角形)