4

在用户单击图表的情况下,我有一个与指定点相交的抛物线曲线的方程。

 // 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);
 }

这给出了如下图:

抛物线

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

startX:Number, startY:Number, endX:Number, endY:Number

我需要找出这条曲线是否与这些线段相交以及在何处相交 (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)

一个正确的答案,但我想要第二种可能的变化。我已经设法通过在计算之前将 m 乘以 -1 并对最后一次计算返回的 x 值做同样的事情来纠正这个问题,但这感觉就像一个 hack。

解决方案:

 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);
 }
4

4 回答 4

6

或者,更明确。

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

你的线是 y(x) = m x + b (Eq 2)

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

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

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

编辑>

要获得最后一个等式,您只需说 eq 1 中的“y”等于 eq 2 中的“y”(因为您正在寻找一个交点!)。这给了你:

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

并重新组合

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

这是一个二次方程。

方程 3 只是这个二次方程的两种可能的解。

编辑 2>

重新阅读您的代码,您的抛物线似乎由 y(x) = A x2

在哪里
A = (target.y / (target.x)2)

因此,在您的情况下,方程式 3 变得简单

 x = ((m +- Sqrt[4 A b + m^2])/(2 A))   (Eq 3b)  

于 2010-08-31T12:52:56.233 回答
3

取曲线方程并将您的线放入 y = mx +b 形式。求解 x,然后确定 X 是否位于线段的起点和终点之间。

查看: http: //mathcentral.uregina.ca/QQ/database/QQ.09.03/senthil1.html

于 2010-08-31T12:35:53.000 回答
3

您是否经常这样做以希望在实际计算交点之前进行单独的测试以查看是否存在交点?如果是这样,请考虑这样一个事实,即您的抛物线是函数 f(x, y) = y - (B * x * x) / (A * A) 的水平集 - 具体而言,f(x, y) = 0。将两个端点插入 f(x,y)——如果它们具有相同的符号,则它们在抛物线的同一侧,而如果它们具有不同的符号,则它们位于抛物线的不同侧抛物线。

现在,您仍然可能有一个与抛物线相交的线段两次,而这个测试没有捕捉到这一点。但是你定义问题的方式让我觉得这对你的应用程序来说可能没问题。

于 2010-08-31T18:53:58.843 回答
0

换句话说,您需要计算每条线段的方程,y = Ax + B将其与曲线方程进行比较,y = Cx^2 + Dx + E然后查看和值Ax + B - Cx^2 - Dx - E = 0之间是否存在解。startXendX

于 2010-08-31T12:33:14.587 回答