我有一个与指定点相交的抛物线曲线的等式,在我的情况下,用户点击图形。
// 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值进行相同的操作来纠正此问题,但这感觉就像是黑客。
解决方案:
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);
}
答案 0 :(得分: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 = m x + b等式中的相应x即可得到交点的y坐标
编辑&gt;
要得到最后一个等式,你只需要说等式1中的“y”等于等式2中的“y”(因为你正在寻找交叉点!)。 这给了你:
A x2+ B x + C = m x + b
并重新组合
A x2+ (B-m) x + (C-b) = 0
这是一个二次方程。
公式3只是这个二次方的两种可能的解决方案。
编辑2&gt;
重新阅读你的代码,似乎你的抛物线是由
y(x) = A x2
其中
A = (target.y / (target.x)2)
所以在你的情况下,Eq 3变得简单
x = ((m +- Sqrt[4 A b + m^2])/(2 A)) (Eq 3b)
HTH!
答案 1 :(得分:3)
获取曲线的等式并将您的线放入y = mx + b形式。求解x,然后确定X是否在您的线段的起点和终点之间。
退房:http://mathcentral.uregina.ca/QQ/database/QQ.09.03/senthil1.html
答案 2 :(得分:3)
您是否经常这样做,希望在实际计算交叉点之前进行单独的测试以查看交叉点是否存在?如果是这样,考虑你的抛物线是函数f(x,y)= y - (B * x * x)/(A * A)的水平集 - 特别是f(x, y)= 0.将两个端点插入f(x,y) - 如果它们具有相同的符号,则它们位于抛物线的同一侧,而如果它们具有不同的符号,则它们位于不同的侧面抛物线。
现在,你仍然可能有一个与抛物线相交的段,而且这个测试没有捕捉到它。但是关于你定义问题的方式让我觉得也许你的应用程序可以。
答案 3 :(得分:0)
换句话说,您需要计算每个线段y = Ax + B
的等式,将其与曲线方程y = Cx^2 + Dx + E
进行比较,然后Ax + B - Cx^2 - Dx - E = 0
,看看startX
之间是否存在解决方案}和endX
值。