-
25-09-2019 - |
题
我有一个抛物曲线相交的指定点的等式,在我的情况,其中用户点击的曲线图。
// 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轴的反面。
更换我的目标COORDS用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)
此是一个正确的答案,但我想在第二可能的变化。 我设法用-1计算之前最后一次计算收益乘以m和做同样与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);
}
解决方案
或者,更明确的呢。
如果您的抛物曲线是
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)中包含任何这两点。如果它们,只需更换相应的X在y =米×+ B方程得到的y坐标的交点
修改> 的
要获得最后一个方程,你刚才说的“Y”公式1等于在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)
HTH!
其他提示
径为曲线方程并把你的线为Y = mx + b中的形式。求解x和然后确定如果X是您的起点和结束点之间为您线段。
退房: http://mathcentral.uregina.ca/ QQ /数据库/ QQ.09.03 / senthil1.html
您这样做往往不够渴望独立的测试,看看在交叉点之前存在实际计算的交叉点?如果是这样,考虑的是,你的抛物线为函数f的水平集(X,Y)= Y - (B * X * X)/(A * A) - 具体地,一个用于其中F(X, Y)= 0。将你的两个端点为F(X,Y) - 如果他们有相同的符号,他们是在抛物线的同一侧,而如果他们有不同的标志,他们是在不同的侧面抛物线。
现在,你仍然可能有段贯穿着抛物线的两次的,而本次测试没有赶上。但是,一些关于你定义问题的方式让我觉得也许这就是确定你的应用程序。
换句话说,需要calulate方程式为每个线段y = Ax + B
比较它曲线方程y = Cx^2 + Dx + E
所以Ax + B - Cx^2 - Dx - E = 0
,看看是否有startX
和endX
值之间的溶液中。