已知圆心半径画等边三角形

基础公式:r * cos(angle) = x ; r * sin(angle) = y;

核心方法参考扩展了下,根据角度话等多边形

function Circle(x, y, r) {
  this.mPointX = x;
  this.mPointY = y;
  this.mRadius = r;
}

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

Circle.prototype.getPointOnCircle = function (angle) {
  let mPointX = this.mPointX;
  let mRadius = this.mRadius;
  let mPointY = this.mPointY;
  let lth = 360/angle;
  let startangle = parseInt(Math.random() * angle);
  let list = []
  for(let i = 0; i<lth; i++) {
    list.push(new Point(mPointX + (mRadius * Math.cos((angle * i + startangle)  * Math.PI / 180)),
    mPointY + (mRadius * Math.sin((angle * i + startangle)  * Math.PI / 180))))
  }
  return list
}


var circle = new Circle(10,20,50);
var p = circle.getPointOnCircle(process.argv[2] || 10);
console.log(p)

绘制到canvas 上

function drawPath(path){
    var cs = document.getElementById('canvas');
    var ctx = cs.getContext('2d');
    ctx.moveTo(path[0]['x'],path[0]['y']);
    for(let i=1; i<path.length; i++) {
        ctx.lineTo(path[i]['x'], path[i]['y'])
    }
    ctx.closePath();
    ctx.stroke()
}

体验地址: http://works.ibeeger.com/dev/math/sanjiaoxing.html

文章来源: 已知圆心半径画等边三角形