2016年10月12日水曜日

開発環境

素数夜曲 (吉田 武 (著)、東海大学出版会)の付録D(女王陛下のLISP)、D.5(コラッツの問題)、成長率を調べる(ステップ数)を JavaScript で取り組んでみる。

コード(Emacs)

HTML5

<div id="graph0"></div>
<label for="n0">n = </label>
<input id="n0" type="number" min="1" step="1" value="99">
<select id="plot0">
  <option>Rect</option>
  <option>Circle</option>
</select>

<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="coconatz3.js"></script>

JavaScript

{
    'use strict';
    let dataset,
        w = 600,
        h = 600,
        padding = 50,
        div_graph = document.querySelector('#graph0'),
        input_n = document.querySelector('#n0'),
        input_plot = document.querySelector('#plot0');

    let cocolatz = (n) => {
        return n === 1 ? [1] :
            [n].concat(
                n % 2 === 0 ? cocolatz(n / 2) : cocolatz(3 * n + 1)
            );
    };
    let iota = (start, end) => {
        let a = [];

        for (let i = start; i <= end; i += 1) {
            a.push(i);
        }
        return a;
    };
    let steps = (n) => {
        return iota(1, n).map((i) => cocolatz(i).length);
    };
    let drawRect = (svg, dataset, xscale, yscale) => {
        let rect_w = xscale(2) - xscale(1);
        
        svg.selectAll('rect')
            .data(dataset)
            .enter()
            .append('rect')
            .attr('x', (d, i) => xscale(i))
            .attr('y', (d) => yscale(d))
            .attr('width', rect_w)
            .attr('height', (d) => yscale(0) - yscale(d))
            .attr('fill', 'rgba(0, 0, 0, 0)')
            .attr('stroke', 'green');
    };
    let drawCircle = (svg, dataset, xscale, yscale) => {
        svg.selectAll('circle')
            .data(dataset)
            .enter()
            .append('circle')
            .attr('cx', (d, i) => xscale(i))
            .attr('cy', (d) => yscale(d))
            .attr('r', 1)
            .attr('fill', 'green');
    };

    let draw = () => {
        let n = parseInt(input_n.value, 10),
            plot = input_plot.value;
        
        dataset = steps(n);

        let xmax = n,
            ymax = Math.max(...dataset);

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

        let xscale = d3.scaleLinear()
            .domain([0, xmax])
            .range([padding, w]);
        let yscale = d3.scaleLinear()
            .domain([0, ymax])
            .range([h - padding, padding]);
        let xaxis = d3.axisBottom().scale(xscale);
        let yaxis = d3.axisLeft().scale(yscale);

        // let rect_w = xscale(2) - xscale(1);
        if (plot === 'Circle') {
            drawCircle(svg, dataset, xscale, yscale);
        } else {
            drawRect(svg, dataset, xscale, yscale);
        }
        
        svg.append('g')
            .attr('transform', `translate(0, ${h - padding})`)
            .call(xaxis);
        svg.append('g')
            .attr('transform', `translate(${padding}, 0)`)
            .call(yaxis);
    };
    input_n.onchange = draw;
    input_plot.onchange = draw;

    draw();
}

0 コメント:

コメントを投稿

Comments on Google+: