html5画布贝塞尔曲线获得所有积分

时间:2016-01-08 16:16:08

标签: javascript html5 canvas

我喜欢从bezier曲线得到一些分数。我找到了

Find all the points of a cubic bezier curve in javascript

位置很容易。首先,计算混合函数。它们控制着控制点在曲线上的“效果”。

strtr("référence",$unwanted_array);

注意 当t为0时,B0_t为1(其他一切为零)。此外,当t为1时,B3_t为1(其他一切都为零)。因此曲线从(ax,ay)开始,到(dx,dy)结束。 任何中间点(px_t,py_t)将由以下给出(从0到1变化t,在循环内以小增量变化):

B0_t = (1-t)^3

B1_t = 3 * t * (1-t)^2

B2_t = 3 * t^2 * (1-t)

B3_t = t^3

我的代码

px_t = (B0_t * ax) + (B1_t * bx) + (B2_t * cx) + (B3_t * dx)

py_t = (B0_t * ay) + (B1_t * by) + (B2_t * cy) + (B3_t * dy)

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:3)

如何沿贝塞尔曲线找到像素

enter image description here

这组函数将在[x,y]的三次贝塞尔曲线上以T的间隔找到0<=T<=1点。

简单来说:它从起点到终点沿着三次贝塞尔曲线绘制点。

// Given the 4 control points on a Bezier curve 
// get x,y at interval T along the curve (0<=T<=1)
// The curve starts when T==0 and ends when T==1
function getCubicBezierXYatPercent(startPt, controlPt1, controlPt2, endPt, percent) {
    var x = CubicN(percent, startPt.x, controlPt1.x, controlPt2.x, endPt.x);
    var y = CubicN(percent, startPt.y, controlPt1.y, controlPt2.y, endPt.y);
    return ({
        x: x,
        y: y
    });
}

// cubic helper formula
function CubicN(T, a, b, c, d) {
    var t2 = T * T;
    var t3 = t2 * T;
    return a + (-a * 3 + T * (3 * a - a * T)) * T + (3 * b + T * (-6 * b + b * 3 * T)) * T + (c * 3 - c * 3 * T) * t2 + d * t3;
}

您可以通过向绘图函数发送大量介于0.00&amp;之间的T值来获取曲线上的点。 1.00。

示例代码和演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

var cBez1=[{x:250,y: 120},{x:290,y:-40},{x:300,y:200},{x:400,y:150}]

drawBez(cBez1);

var cPoints=findCBezPoints(cBez1);

drawPlots(cPoints);


function findCBezPoints(b){
  var startPt=b[0];
  var controlPt1=b[1];
  var controlPt2=b[2];
  var endPt=b[3];
  var pts=[b[0]];
  var lastPt=b[0];
  var tests=5000;
  for(var t=0;t<=tests;t++){
    // calc another point along the curve
    var pt=getCubicBezierXYatT(b[0],b[1],b[2],b[3], t/tests);
    // add the pt if it's not already in the pts[] array
    var dx=pt.x-lastPt.x;
    var dy=pt.y-lastPt.y;
    var d=Math.sqrt(dx*dx+dy*dy);
    var dInt=parseInt(d);
    if(dInt>0 || t==tests){
      lastPt=pt;
      pts.push(pt);
    }
  }
  return(pts);
}

// Given the 4 control points on a Bezier curve 
// Get x,y at interval T along the curve (0<=T<=1)
// The curve starts when T==0 and ends when T==1
function getCubicBezierXYatT(startPt, controlPt1, controlPt2, endPt, T) {
  var x = CubicN(T, startPt.x, controlPt1.x, controlPt2.x, endPt.x);
  var y = CubicN(T, startPt.y, controlPt1.y, controlPt2.y, endPt.y);
  return ({
    x: x,
    y: y
  });
}

// cubic helper formula
function CubicN(T, a, b, c, d) {
  var t2 = T * T;
  var t3 = t2 * T;
  return a + (-a * 3 + T * (3 * a - a * T)) * T + (3 * b + T * (-6 * b + b * 3 * T)) * T + (c * 3 - c * 3 * T) * t2 + d * t3;
}

function drawPlots(pts){
  ctx.fillStyle='red';
  // don't draw the last dot b/ its radius will display past the curve
  for(var i=0;i<pts.length-1;i++){
    ctx.beginPath();
    ctx.arc(pts[i].x,pts[i].y,1,0,Math.PI*2);
    ctx.fill();
  }
}

function drawBez(b){
  ctx.lineWidth=7;
  ctx.beginPath();
  ctx.moveTo(b[0].x,b[0].y);
  ctx.bezierCurveTo(b[1].x,b[1].y, b[2].x,b[2].y, b[3].x,b[3].y);
  ctx.stroke();
}
body{ background-color: ivory; }
#canvas{border:1px solid red; margin:0 auto; }
<h4>Black line is context.bezierCurveTo<br>Red "line" is really dot-points plotted along the curve</h4>
<canvas id="canvas" width=500 height=300></canvas>