代码改变世界

javascript画圆

2010-01-22 17:46  BlueDream  阅读(6942)  评论(0编辑  收藏  举报

先上个图.这样就很容易理解了

圆的参数方程 x=a+r cosθ y=b+r sinθ (a,b)为圆心坐标 r为圆半径 θ为参数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style>
    span{position:absolute; color: red}
  </style>
 </head>
 <body>
 </body>
 <script type="text/javascript">
 //<![CDATA[
    var showCircle = function() {
        var PI = Math.PI;
        return {
            draw: function(r/*半径*/, _x/*x轴偏移*/, _y/*y轴偏移*/) {
                // 获得x y坐标
                var x, y;
                for(var i = 0; i < 360; i += 6) {
                    x = Math.cos(PI / 180 * i) * r + _x;
                    y = Math.sin(PI / 180 * i) * r + _y;
                    var O = document.createElement('span');
                    O.appendChild(document.createTextNode('.'));
                    document.body.appendChild(O);
                    O.style.left = x + 'px';
                    O.style.top = y + 'px';
                }        
            }
        }
    }();

    showCircle.draw(100, 400, 200);
 //]]>
 </script>
</html>