solve y cube equation in runtime [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionI am doing cropping a object in real time using c++. Here I know 2 coordinates of points A and B. I want to find 3rd point Z which is perpendicular to AB line. Z(x3,y3). It mean ABZ angle is 90. I gained 2 huge equations when I have used BZ slope and BZ distance. I have simplified it and gain following equation.
y3 ( y3 (k1 + y3) + k) = k ;
here k1 , k2 , k3 are Constance which has given. But k1 , k2 , k3
are not equals to eac开发者_JS百科h other.
I want to find y3 here. Please help me.
This sounds overly complicated, at least if I've understood your problem. If there are no other constraints on Z (other than it be on a line perpendicular to AB at B), then:
Z.x = B.x - (A.y - B.y);
Z.y = B.y + (A.x - B.x);
solves the problem. If there are additional constraints on Z, the above expression still gives a point that, with B, defines the line. Depending on the constraints, calculating the actual Z may be more or less complicated, but it still shouldn't involve cubic equations.
If you do need to take a cube root, of course, the simplest solution is to use the standard function cbrt
.
Generally, if you want to solve a cubic equation
x^3 + ax^2 + bx + c = 0
and you already have two solutions x1, x2 and are looking for the third solution x3 then you can do this by observing that
x^3 + ax^2 + bx + c = (x-x1)(x-x2)(x-x3),
and hence that
-a = x1 + x2 + x3
or
x3 = -a -x1 -x2
Hence finding the third solution of a cubic given the other two solutions is trivial.
The cubic equation has an exact solution. It's ugly but there you go.
Bear in mind that there will be pathalogical cases where numeric instabilities show up. If accuracy is important you should use more than one method.
This guy has solution code, and Dr. Math has some explanation.
精彩评论