这些年,我穿过的那些队服

神奇的阿基米德螺线

posted @ 2014年5月23日 19:06 in 未分类 with tags javascript math , 1460 阅读
分享到: 更多

今天在读数学史,正巧读到阿基米德螺线,于是写了一段js代码,生成螺线。

See the Pen Spiral by gangtao (@gangtao) on CodePen.

更多的有趣内容请参考这篇文章

代码如下(需要jquery和d3):

HTML

<div id="chart"></div>

 

CSS

body {
background-color:#000000;
}

 

JS

function drawCircile(pc, r, container) {
    var circle = container.append("circle").attr("cx", pc.x).attr("cy", pc.y).attr("r", r)
        .attr("stroke", "#fa6900").attr("stroke-width", 1).attr("fill", "none");
    return circle;

}

function drawLine(p1, p2, c, isDash) {
    var line = c.append("line").attr("x1", p1.x).attr("y1", p1.y)
        .attr("x2", p2.x).attr("y2", p2.y)
        .style("stroke", "#ccc").style("stroke-width", 1);
    if (isDash) {
        line.style("stroke-dasharray", "5,5");
    }
    return line;
}

$(function () {
    var center = {x: 300, y :300};
    var l = 250;
    var a = 0, b = 0, p0, p1, line;
    var v = 0.3; //直线移动速度
    var f = 360; //转动速度 

    var root = d3.select("#chart").append("svg").append("g");
    drawCircile(center,3,root)

    var timer = setInterval(function(){
        a = a + Math.PI / f;
        b = b + v;
        p0 = {x:center.x+ l * Math.sin(a), y:center.y - l * Math.cos(a) };
        p1 = {x:center.x + b * Math.sin(a), y:center.y - b * Math.cos(a) };
        drawCircile(p1,1,root)

        if( b > l ) {
            clearInterval(timer);
        } else {
            if(!line) {
                line =  drawLine(center, p0, root);    
            } else {
                line.transition().duration(10).attr("x2",p0.x).attr("y2",p0.y);
            } 
        }
    },10);
});

改变v和f的值分别改变点在直线上的移动过速度和直线转动的角速度,会得到不同的结果。

我拿给我还在读一年级的女儿看,她很是入迷,不断要求我修改参数,结果得到许多有趣的图形,大家可以通过这个链接自己试试看。

f=2.5 海星

f=3 雪花

f=1.414

f = 0.618

f=8.8

f = 12.12

更多的结果,大家可以自己去探索。

感叹于数学的神奇和美丽,感叹于小孩子的探索精神, 感叹于计算机和编程使得数学的探索变得简单和有趣!

 


分享到: 更多
Avatar_small
walden university st 说:
2022年8月19日 08:32

Welcome to the myWalden student portal. Click below to log in with your Walden University e-mail address and password. walden university student portal How do I reset my Walden portal password? You can reset ... What do I do if I am missing a class in my student portal? Contact your ... Where do I login walden university student portal

Avatar_small
Emma 说:
2023年1月17日 02:06

The Archimedes spiral, named after the ancient Greek mathematician Archimedes of Syracuse, is a type of spiral that can be described by the polar equation r = a?, where r is the distance from real estate services Dallas the origin, ? is the angle from the positive x-axis, and a is a constant. It is also called the "Magical Archimedes Spiral" because of the unique mathematical properties of the spiral. Good to see the details you have shared here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter