抛物线与线段的交点

gra*_*ukt 4 language-agnostic math geometry actionscript-3 curves

我有一个与指定点相交的抛物线曲线的等式,在我的情况下,用户点击图形.

 // this would typically be mouse coords on the graph
 var _target:Point = new Point(100, 50);

 public static function plot(x:Number, target:Point):Number{
  return (x * x) / target.x * (target.y / target.x);
 }
Run Code Online (Sandbox Code Playgroud)

这给出了如下图:

抛物线

我还有一系列由开始和结束坐标定义的线段:

startX:Number, startY:Number, endX:Number, endY:Number
Run Code Online (Sandbox Code Playgroud)

我需要找到此曲线与这些段相交的位置(A):

替代文字

如果有任何帮助,startX总是如此< endX

我觉得有一个相当直接的方法来做到这一点,但我真的不知道要搜索什么,也不是我非常精通"正确"的数学,因此非常感谢实际的代码示例.

更新:

我有交叉点工作,但我的解决方案给了我y轴错误侧的坐标.

分别用A和B替换目标坐标,给出了该图的等式:

(x * x) / A * (B/A)

// this simplifies down to:
(B * x * x) / (A * A)

// which i am the equating to the line's equation
(B * x * x) / (A * A) =  m * x + b

// i run this through wolfram alpha (because i have no idea what i'm doing) and get:
(A * A * m - A * Math.sqrt(A * A * m * m + 4 * b * B)) / (2 * B)
Run Code Online (Sandbox Code Playgroud)

一个正确的答案,但我想要第二种可能的变化.我已经设法通过在计算之前将m与-1相乘并使用最后一次计算返回的x值进行相同的操作来纠正此问题,但这感觉就像一个黑客.

解:

 public static function intersectsSegment(targetX:Number, targetY:Number, startX:Number, startY:Number, endX:Number, endY:Number):Point {
  // slope of the line
  var m:Number = (endY - startY) / (endX - startX);

  // where the line intersects the y-axis
  var b:Number = startY - startX * m;

  // solve the two variatons of the equation, we may need both
  var ix1:Number = solve(targetX, targetY, m, b);
  var ix2:Number = solveInverse(targetX, targetY, m, b);

  var intersection1:Point;
  var intersection2:Point;

  // if the intersection is outside the line segment startX/endX it's discarded
  if (ix1 > startX && ix1 < endX) intersection1 = new Point(ix1, plot(ix1, targetX, targetY));
  if (ix2 > startX && ix2 < endX) intersection2 = new Point(ix2, plot(ix2, targetX, targetY));

  // somewhat fiddly code to return the smallest set intersection
  if (intersection1 && intersection2) {
   // return the intersection with the smaller x value
   return intersection1.x < intersection2.x ? intersection1 : intersection2;
  } else if (intersection1) {
   return intersection1;
  }

  // this effectively means that we return intersection2 or if that's unset, null
  return intersection2;
 }

 private static function solve(A:Number, B:Number, m:Number, b:Number):Number {
  return (m + Math.sqrt(4 * (B / (A * A)) * b + m * m)) / (2 * (B / (A * A)));
 }

 private static function solveInverse(A:Number, B:Number, m:Number, b:Number):Number {
  return (m - Math.sqrt(4 * (B / (A * A)) * b + m * m)) / (2 * (B / (A * A)));
 }

 public static function plot(x:Number, targetX:Number, targetY:Number):Number{
  return (targetY * x * x) / (targetX * targetX);
 }
Run Code Online (Sandbox Code Playgroud)

Dr.*_*ius 6

或者,更明确.

如果你的抛物线是 y(x)= A x2+ B x + C (Eq 1)

y(x) = m x + b (Eq 2)

A x2+ B x + C = m x + b

x的两个可能的解决方案(+和 - )是

x = ((-B + m +- Sqrt[4 A b + B^2 - 4 A C - 2 B m + m^2])/(2 A))   (Eq 3)
Run Code Online (Sandbox Code Playgroud)

您应该检查您的段端点(在x中)是否包含这两个点中的任何一个.如果是这样,只需替换y = mx + b等式中的相应x,即可获得交点的y坐标

编辑>

为了得到最后的等式,你只需要说方程1中的"y"等于方程2中的"y"(因为你正在寻找交叉点!).这给你:

A x2+ (B-m) x + (C-b) = 0

并重新组合

y(x) = A x2

这是一个二次方程.

公式3只是这个二次方的两种可能解.

编辑2>

重新阅读你的代码,似乎你的抛物线是由定义的 A = (target.y / (target.x)2)

哪里
y(x)= A x2+ B x + C (Eq 1)

y(x) = m x + b (Eq 2)

所以在你的情况下,方程3变得简单

 x = ((m +- Sqrt[4 A b + m^2])/(2 A))   (Eq 3b)  
Run Code Online (Sandbox Code Playgroud)

HTH!