正多边形的编程绘制(javascript)

2019-11-26 11:11:59

参考地址 正多边形的编程绘制(javascript)

顺带给大家介绍一个网站,玄数,这个网站主要就是讲的算法,和各种计算。本身绘制五边形,就需要各种正弦,余弦的计算,相比大部分程序员已经忘记了怎么算了吧。大家可以从这个网站学习到很多算法。

如何用程序来绘制正多边形?

在一般情况下,会使用 x = radius * Math.cos(angle), y = radius * Math.sin(angle) 来进行绘制,但这是关于x轴对称的,如果遇到正多边形的边数为奇数,而你又希望它是以y轴对称时,可按照下面的方法。

正多边形

如图,正五边形ABCDE关于y轴对称,B与E,C与D互为对称点。A的坐标为(0, r)。 半径OA旋转一个内角θ,便是OB,此时B的坐标为(r·sin0, r·cos0)。继续旋转,可以得到OC、OD、OE等半径,坐标求法与OB的一致,只需把对应的角度依次增加(2π/边数)。

 

编程的流程图如下:

 

algorithm regular polygon

使用两个javascript文件:
Polygon.js —— 正多边形的类,在构造函数中求得所有的顶点,放在数组vertices

var Point = function(x, y)
{
    this.x = x;
    this.y = y;
};

var Polygon = function(x, y, radius, sides)
{
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.sides = sides;
    this.vertices = getPoints(x, y, radius, sides);    

    function getPoints(x, y, radius, sides){
        var points = [],
            angle = 0,
            centerAngle = 2 * Math.PI / sides;

        for(var i = 0;  i < sides;  i++){
            points.push(new Point( x + radius * Math.sin(angle), y - radius * Math.cos(angle) ));
            angle += centerAngle;
        }
        console.log(points);
        return points;
    }

    this.strokeStyle = 'black';
    this.fillStyle = 'rgba(200, 200, 200, 1)';    
};

Polygon.prototype = {

    createPath: function(context){

        context.beginPath();
        context.moveTo(this.vertices[0].x, this.vertices[0].y);
        for(var i = 1;  i < this.sides;  i++){
            context.lineTo(this.vertices[i].x, this.vertices[i].y);
        }
        context.closePath();
    },

    stroke: function(context){

        context.save();
        this.createPath(context);
        context.strokeStyle = this.strokeStyle;
        context.stroke();
        context.restore();
    },

    fill: function(context){

        context.save();
        this.createPath(context);
        context.fillStyle = this.fillStyle;
        context.fill();
        context.restore();
    }
}

drawPolygon.js —— 把多边形画到canvas上

 function init(){

    var canvas = document.getElementById('canvas'),
        cxt = canvas.getContext('2d');

    var polygon = new Polygon(200, 200, 130, 5);
    polygon.stroke(cxt);
}


  • 2020-12-07 22:02:44

    intellij idea远程开发的几个想法

    我之前是用idea上面自带的stfp来做的本地开发同步到linux服务器编译,但是我发现这个如果多个客户端同时开发,或者多个同事一起开发,服务器上的就不能更新到本地。是不能增量更新到本地,必须全部下载,比对下载也行,但是工程量打了就特别慢。

  • 2020-12-07 22:06:13

    System Extension Blocked - warning

    After upgrading your macOS computer to High Sierra 10.13.4 or higher (starting in April 2018), you may see a message about a System Extension Blocked. At Williams we have seen this warning appear for these programs:

  • 2020-12-08 08:57:12

    win10上使用win-sshfs

    首先在GitHub上下载DokanSetup-1.0.5.1000和WinSSHFS-1.6.1.13-devel 注意:Dokan不能使用最新的版本,得使用1.0.5版本。要不win-sshfs会报Dokan版本错误的问题。(win10版本)

  • 2020-12-08 11:51:54

    Ubuntu安装Node.js和npm

    Node.js是基于Chrome的JavaScript构建的跨平台JavaScript运行时环境,npm是Node.js的默认程序包管理器,也是世界上最大的软件注册表。本篇文章展示了三种在Ubuntu 20.04服务器上安装Node.js和npm的方法。

  • 2020-12-08 17:13:57

    nvm卸载、安装node和npm

    1、node.js、nvm、 npm (1)在cmd中输入`where node`找到node长须所在位置进行删除 (2)确保计算机-环境变量删除相关引用 (3)在cmd中输入`node -v` ,得到以下结果,删除成功