如何编码n阶贝塞尔曲线

时间:2015-07-01 17:12:02

标签: javascript canvas

尝试在画布的javascript中为项目编写n阶bezier。我希望能够让用户按下按钮,在这种情况下' b',以选择每个终点和控制点。到目前为止,我能够在按键上获得鼠标坐标,并使用内置函数制作二次曲线和贝塞尔曲线。我将如何为第n个订单制作代码?

2 个答案:

答案 0 :(得分:6)

这是n阶贝塞尔曲线的Javascript实现:

// setup canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

canvas.height = window.innerHeight;
canvas.width = window.innerWidth;

ctx.fillText("INSTRUCTIONS: Press the 'b' key to add points to your curve. Press the 'c' key to clear all points and start over.", 20, 20);

// initialize points list
var plist = [];

// track mouse movements
var mouseX;
var mouseY;

document.addEventListener("mousemove", function(e) {
  mouseX = e.clientX;
  mouseY = e.clientY;
});

// from: http://rosettacode.org/wiki/Evaluate_binomial_coefficients#JavaScript
function binom(n, k) {
  var coeff = 1;
  for (var i = n - k + 1; i <= n; i++) coeff *= i;
  for (var i = 1; i <= k; i++) coeff /= i;
  return coeff;
}

// based on: https://stackoverflow.com/questions/16227300
function bezier(t, plist) {
  var order = plist.length - 1;

  var y = 0;
  var x = 0;

  for (i = 0; i <= order; i++) {
    x = x + (binom(order, i) * Math.pow((1 - t), (order - i)) * Math.pow(t, i) * (plist[i].x));
    y = y + (binom(order, i) * Math.pow((1 - t), (order - i)) * Math.pow(t, i) * (plist[i].y));
  }

  return {
    x: x,
    y: y
  };
}

// draw the Bezier curve
function draw(plist) {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  var accuracy = 0.01; //this'll give the 100 bezier segments
  ctx.beginPath();
  ctx.moveTo(plist[0].x, plist[0].y);

  for (p in plist) {
    ctx.fillText(p, plist[p].x + 5, plist[p].y - 5);
    ctx.fillRect(plist[p].x - 5, plist[p].y - 5, 10, 10);
  }

  for (var i = 0; i < 1; i += accuracy) {
    var p = bezier(i, plist);
    ctx.lineTo(p.x, p.y);
  }

  ctx.stroke();
  ctx.closePath();
}

// listen for keypress
document.addEventListener("keydown", function(e) {
  switch (e.keyCode) {
    case 66:
      // b key
      plist.push({
        x: mouseX,
        y: mouseY
      });
      break;
    case 67:
      // c key
      plist = [];
      break;
  }
  draw(plist);
});
html,
body {
  height: 100%;
  margin: 0 auto;
}
<canvas id="canvas"></canvas>

这基于this implementation of cubic Bezier curves。在您的应用程序中,听起来您希望使用用户定义的点填充points数组。

答案 1 :(得分:0)

这是您想要添加的任意数量的点以制作贝塞尔曲线的代码示例。您将传递的点是一个包含点的 x 和 y 值的对象数组。 [ { x: 1,y: 2 } , { x: 3,y: 4} ... ]

function factorial(n) {
    if(n<0)    
        return(-1); /*Wrong value*/      
    if(n==0)    
        return(1);  /*Terminating condition*/    
    else    
    {    
        return(n*factorial(n-1));        
    }
}

function nCr(n,r) {
    return( factorial(n) / ( factorial(r) * factorial(n-r) ) );
}

function BezierCurve(points) {
    let n=points.length;
    let curvepoints=[];
    for(let u=0; u <= 1 ; u += 0.0001 ){

        let p={x:0,y:0};

        for(let i=0 ; i<n ; i++){
            let B=nCr(n-1,i)*Math.pow((1-u),(n-1)-i)*Math.pow(u,i);
            let px=points[i].x*B;
            let py=points[i].y*B;
            
            p.x+=px;
            p.y+=py;
            
        }

        curvepoints.push(p);
    }
    
    return curvepoints;
}