我有一个抛物线曲线与指定点相交的方程,在我的情况下,用户点击一个图形。抛物线曲线和线段的交点
// 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
我感觉有一个相当简单的方式来做到这一点,但我真的不知道该怎么寻找,我也不是非常好精通“正确的”数学,所以真正的代码示例将非常值得赞赏。
UPDATE:
我有交集的工作,但我的解决方案给我的坐标y轴的反面。
更换我分别用A和B的目标COORDS,给出了这个方程的情节:
(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值进行相同的操作来纠正此问题,但那感觉像是一种黑客攻击。
SOLUTION:
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);
}
Yor解决方案与我的解决方案相同。我认为你应该采取积极的选择(+ A * Math.sqrt)而不是( - A * Math.sqrt),并让其他方法保持不变///二次方程式。有两种解决方案...只需选择其他解决方案 – 2010-08-31 15:10:41
注意////您的(B /(A * A))=我的A – 2010-08-31 15:16:35
+1发布解决方案 – 2010-08-31 22:49:45