actionscript-3 - 抛物线与线段的交点

标签 actionscript-3 language-agnostic math geometry 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);
 }

这给出了如下图:

parabolic curve

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

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

我需要找出该曲线是否与这些线段相交以及在何处相交 (A):

alt text

如果有任何帮助,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);
 }

最佳答案

或者,更明确一点。

如果你的抛物线是 y(x)= A x<sup>2</sup>+ 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=m x + b 方程中相应的 x 即可得到交点的 y 坐标

编辑>

要得到最后一个方程,您只需说方程 1 中的“y”等于方程 2 中的“y”(因为您正在寻找交集!)。 这给你:

A x<sup>2</sup>+ B x + C = m x + b<br/>

并重新组合

A x<sup>2</sup>+ (B-m) x + (C-b) = 0<br/>
这是一个二次方程。

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

编辑2>

重新阅读您的代码,似乎您的抛物线是由 y(x) = A x<sup>2</sup><br/>

哪里
A = (target.y / (target.x)<sup>2</sup>)

所以在你的例子中,方程 3 变得简单

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

呵呵!

关于actionscript-3 - 抛物线与线段的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3608977/

相关文章:

algorithm - 计算旋转矩形中的最大内接矩形

在函数中调用数组

algorithm - n!在 n^100 log n 中可实现的不同排列

actionscript-3 - 当我调用addEventListener 时,是否必须设置removeEventListener?

actionscript-3 - 用于 as3/flex 项目的 CI 友好型自动化构建

actionscript-3 - 使用 as3crypto 多次登录 assiSTLy.com

ios - Adobe AIR iOS 应用程序中 iPhone 6 和 6+ 的启动画面错误

language-agnostic - 您认为首先为命令行开发有什么想法?

time-complexity - 时间复杂度: O(log n) versus O(log 2n)

c - atan2 用于任意相移的两个正弦波?