从3个点找到所有可能的立方方程

时间:2015-08-14 04:08:47

标签: javascript math

我正在尝试找到可以从特定场景中找到的所有立方方程

在这种情况下,有3个点可以动态移动,但是应该假设这种情况是静态的。所有符合条件的立方方程必须通过这三个点。还有一个滑块可以增强当前功能以改变它的形状,同时保持它通过三个点。

我的意思的一个例子可以在这里看到(sorta):https://jsfiddle.net/3281qebw/

我已经尝试找到方程式,这是我的工作:

ax1^3+bx1^2+cx1+d=y1
ax2^3+bx2^2+cx2+d=y2
ax3^3+bx3^2+cx3+d=y3

ax1^3+bx1^2+cx-y1 = ax2^3+bx2^2+cx2-y2

a=y1-y2-d*(x1-x2)-b*(x1^2-x2^2)
  -----------------------------
          x1^3-x2^3

从那里,我试图找到另外两个,导致这个代码:

xone = (slider - 50) / 50;

//variables for use in equation
var varK = (firsty - secondy) / (Math.pow(firstx, 3) - Math.pow(secondx, 3));
var varL = (firstx - secondx) / (Math.pow(firstx, 3) - Math.pow(secondx, 3));
var varM = (Math.pow(firstx, 2) - Math.pow(secondx, 2)) / (Math.pow(firstx, 3) - Math.pow(secondx, 3));

var varAlpha = (firsty - (varK * Math.pow(firstx, 3))) / (Math.pow(firstx, 2) - Math.pow(firstx, 3) * varM);

var varBeta = 1 / (Math.pow(firstx, 2) - Math.pow(firstx, 3) * varM);

var varDelta = (firstx - Math.pow(firstx, 3) * varL) / (Math.pow(firstx, 2) - Math.pow(firstx, 3) * varM);

//variables for equation
xzero = ((varK - xone * varL - varAlpha + varDelta * xone) * Math.pow(thirdx, 3) + (varAlpha - varDelta * xone) * Math.pow(thirdx, 2) + xone * thirdx) / (varBeta * Math.pow(thirdx, 3) - varBeta * Math.pow(thirdx, 2) + 1);
xtwo = varAlpha - varBeta * xzero - varDelta * xone;
xthree = varK - xone * varL - xtwo * varM;

我确信这会起作用,但是,正如在jsfiddle中可以看到的那样,当移动滑块时,第三个点会因某种原因被排除。

我想找到我的算法中可能存在的任何错误,或者如果可能的话,请找出任何更好的通用方程式。

我决定采用不同的方法:

d=ax^3+bx^2+cx-y

使用这个,我得到了:

y2=y1+a(x2^3-x1^3)+b(x2^2-x1^2)+c(x2-x1)

      y₂-y₁
 c  = ───── - b(x₁+x₂)-a(x2^2+x2x1+x1^2)
      x₂-x₁

但这就是我能得到的。

我已经花了几个小时的时间来完成它并制作了这些代码块:

    xzero = (thirdy - (firsty * Math.pow(thirdx, 3) / Math.pow(firstx, 3)) + (secondy * Math.pow(thirdx, 3) / firstx) - (Math.pow(thirdx, 3) * (firsty * Math.pow(secondx, 3) / Math.pow(firstx, 3)) / firstx) + (Math.pow(thirdx, 3) * (xone * (secondx, 3) / Math.pow(xone, 2)) / firstx) + (xone * Math.pow(thirdx, 3) / Math.pow(firstx, 2)) - secondy * Math.pow(thirdx, 2) + (firsty * Math.pow(secondx, 3) * Math.pow(thirdx, 2) / Math.pow(firstx, 3)) - (xone * Math.pow(secondx, 3) * Math.pow(thirdx, 2) / Math.pow(firstx, 2)) + (xone * secondx * Math.pow(thirdx, 2)) - xone * thirdx) / (1 + (Math.pow(secondx, 3) * Math.pow(thirdx, 2) / Math.pow(firstx, 3)) + (Math.pow(thirdx, 3) / firstx) - Math.pow(thirdx, 2) - (Math.pow(secondx, 3) * Math.pow(thirdx, 3) * (1 / Math.pow(firstx, 3)) / firstx))

xtwo = (secondy - (firsty * Math.pow(secondx, 3) / Math.pow(firstx, 3)) + (xone * Math.pow(secondx, 3) / Math.pow(firstx, 2)) + (xzero * Math.pow(secondx, 3) / Math.pow(firstx, 3)) - xone * secondx - xzero) / (Math.pow(secondx, 2) - (Math.pow(secondx, 3) / firstx));
xthree = (firsty / Math.pow(firstx, 3)) - (xtwo / firstx) - (xone / Math.pow(firstx, 2)) - (xzero / Math.pow(firstx, 3));

}

但从这里可以看出:https://jsfiddle.net/v6bLu80w/ 它由于某种原因不起作用。我可以帮忙解决这个问题吗?

更正:只要变量不是零,它就会起作用,但似乎还有另一个问题,我之前不会经历第三点,如下所示:{{ 3}}

0 个答案:

没有答案