【问题标题】:calculating intersection point of quadratic bezier curve计算二次贝塞尔曲线的交点
【发布时间】:2014-12-27 04:21:17
【问题描述】:

这绝对是我三角学知识的极限。

有没有计算二次贝塞尔曲线和直线交点的公式?

示例:

在下图中,我有 P1、P2、C(这是控制点)和 X1、X2(对于我的特定计算,它只是 X 轴上的一条直线。)

我想知道的是T的X,Y位置以及T的切线角度。在红曲线和黑线的交点处。

在做了一些研究并找到this问题后,我知道我可以使用:

t = 0.5; // given example value
x = (1 - t) * (1 - t) * p[0].x + 2 * (1 - t) * t * p[1].x + t * t * p[2].x;
y = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y;

计算我在曲线上任何给定点的 X、Y 位置。所以使用它我可以沿着曲线遍历一堆点,检查是否有任何在我相交的 X 轴上。并从那里尝试计算我的切角。但这似乎并不是最好的方法。任何数学大师都知道最好的方法是什么?

我在想这可能比我想要的要复杂一些。

【问题讨论】:

  • 也许Pomax 可以为您提供一些帮助。
  • 我很确定我已经在 Primer 中的 curve intersection 部分介绍了它。只需旋转曲线和直线,使直线与 x 轴“对齐”,现在您已将“寻找交点”简化为“寻找根”(对齐解释 here)

标签: javascript html canvas trigonometry


【解决方案1】:

如果您只需要与 x 方向的直线相交,则您已经知道相交的 y 坐标。要获得 x 坐标,请执行以下操作:

  • 你的行的公式就是y = b
  • 将其设置为等于贝塞尔函数y(t) 的 y 方程得到:
    b = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y
  • 为 t 求解 * 得到你:
    t = (p[0].y - p[1].y - sqrt(b*a + p[1].y*p[1].y - p[0].y*p[2].y)) / a
    与a = p[0].y - 2*p[1].y + p[2].y
  • 将得到的 t 插入贝塞尔函数 x(t) 的 x 方程中,得到 x 坐标,就大功告成了。

您可能需要注意一些特殊情况,例如不存在解决方案时,因为平方根的参数可能会变为负数或分母 (a) 可能会变为零,或类似的情况。

如果您需要更多帮助或与任意行的交集,请发表评论。

(*) 我用 wolfram alpha 解方程,因为我很懒:Wolfram alpha solution。

【讨论】:

  • 我真的很喜欢这种方法。计算水平或垂直线时更简单和优雅。对我的应用非常有用。
  • 对形状的扫描线转换非常有用!
  • 请注意,如果您同时平移和旋转贝塞尔曲线和相交线,使相交线等于 x 轴,则此方法始终可以用于贝塞尔线交叉。
【解决方案2】:

二次曲线公式:

y=ax^2+bx+c // where a,b,c are known

直线公式:

// note: this `B` is not the same as the `b` in the quadratic formula ;-)

y=m*x+B  // where m,B are known.

曲线和直线相交,其中两个方程对相同的 [x,y] 均成立:

这是带注释的代码和一个演示:

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

// linear interpolation utility
var lerp=function(a,b,x){ return(a+x*(b-a)); };

// qCurve & line defs
var p1={x:125,y:200};
var p2={x:250,y:225};
var p3={x:275,y:100};
var a1={x:30,y:125};
var a2={x:300,y:175};

// calc the intersections
var points=calcQLintersects(p1,p2,p3,a1,a2);

// plot the curve, line & solution(s)
var textPoints='Intersections: ';
ctx.beginPath();
ctx.moveTo(p1.x,p1.y);
ctx.quadraticCurveTo(p2.x,p2.y,p3.x,p3.y);
ctx.moveTo(a1.x,a1.y);
ctx.lineTo(a2.x,a2.y);
ctx.stroke();
ctx.beginPath();
for(var i=0;i<points.length;i++){
  var p=points[i];
  ctx.moveTo(p.x,p.y);
  ctx.arc(p.x,p.y,4,0,Math.PI*2);
  ctx.closePath();
  textPoints+=' ['+parseInt(p.x)+','+parseInt(p.y)+']';
}
ctx.font='14px verdana';
ctx.fillText(textPoints,10,20);
ctx.fillStyle='red';
ctx.fill();

///////////////////////////////////////////////////

function calcQLintersects(p1, p2, p3, a1, a2) {
  var intersections=[];

  // inverse line normal
  var normal={
    x: a1.y-a2.y,
    y: a2.x-a1.x,
  }

  // Q-coefficients
  var c2={
    x: p1.x + p2.x*-2 + p3.x,
    y: p1.y + p2.y*-2 + p3.y
  }

  var c1={
    x: p1.x*-2 + p2.x*2,
    y: p1.y*-2 + p2.y*2,
  }

  var c0={
    x: p1.x,
    y: p1.y
  }

  // Transform to line 
  var coefficient=a1.x*a2.y-a2.x*a1.y;
  var a=normal.x*c2.x + normal.y*c2.y;
  var b=(normal.x*c1.x + normal.y*c1.y)/a;
  var c=(normal.x*c0.x + normal.y*c0.y + coefficient)/a;

  // solve the roots
  var roots=[];
  d=b*b-4*c;
  if(d>0){
    var e=Math.sqrt(d);
    roots.push((-b+Math.sqrt(d))/2);
    roots.push((-b-Math.sqrt(d))/2);
  }else if(d==0){
    roots.push(-b/2);
  }

  // calc the solution points
  for(var i=0;i<roots.length;i++){
    var minX=Math.min(a1.x,a2.x);
    var minY=Math.min(a1.y,a2.y);
    var maxX=Math.max(a1.x,a2.x);
    var maxY=Math.max(a1.y,a2.y);
    var t = roots[i];
    if (t>=0 && t<=1) {
      // possible point -- pending bounds check
      var point={
        x:lerp(lerp(p1.x,p2.x,t),lerp(p2.x,p3.x,t),t),
        y:lerp(lerp(p1.y,p2.y,t),lerp(p2.y,p3.y,t),t)
      }
      var x=point.x;
      var y=point.y;
      // bounds checks
      if(a1.x==a2.x && y>=minY && y<=maxY){  
        // vertical line
        intersections.push(point);
      }else if(a1.y==a2.y && x>=minX && x<=maxX){
        // horizontal line
        intersections.push(point);
      }else if(x>=minX && y>=minY && x<=maxX && y<=maxY){
        // line passed bounds check
        intersections.push(point);
      }
    }
  }
  return intersections;
}
body{ background-color: ivory; padding:10px; }
#canvas{border:1px solid red;}
<h4>Calculate intersections of QBez-Curve and Line</h4>
<canvas id="canvas" width=350 height=350></canvas>

【讨论】:

  • 他要求贝塞尔函数,没有y(x),只有参数表示x(t),y(t)
  • 这让我更加困惑。你能添加一个带有值的例子吗?我发现当我看到工作正常时我能更好地理解。
  • @Gigo:哎呀,昨晚凌晨 1 点,我疲惫的大脑没有意识到这一点——我的错。所以现在数学相当复杂。您可以转换线曲线对,使线轴对齐,然后计算根。 Gigo,你已经编写过这个解决方案了吗?如果没有,我将支付罚款并为 tyler Mackenzie 编写解决方案......
  • @Gigo,我发布了计算 QBez 和 Line 交点的代码。
  • 我们如何解决 3D 贝塞尔曲线和线交点的相同问题?例如对于点: var p1={ x:0, y:0, z: 1 }; var p2={ x:100, y:0, z: 1 }; var c={ x:0, y:0, z: -5 }; var x1={ x:0, y:0, z: 0 }; var x2={ x:100, y:0, z: 0 };
【解决方案3】:

用x坐标计算直线的tangθ

那么曲线的 (x, y) 的交点应该是相同的 tangθ

所以解决办法是

a = 线从 (line.x,0) 到 (0,0) 的 x 距离

(curve.x + a) / curve.y = tangθ(θ可以从与x坐标的直线交点得到)

【讨论】:

    猜你喜欢
    • 2011-08-03
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多