solve y cube equation in runtime [closed] - c++

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I 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 each 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.

Related

Convert a Sage multivariate polynomial into a univariate one

I am trying to implement Schoof's algorithm in Sage.
I have multivariate polynomials f(x, y) with coefficients in
a finite field F_q, for which the variable y only appears
in even powers (for example f(x, y) = x * y^4 + x^3 * y^2 + x).
Furthermore, using an equation y^2 = x^3 + A * x + B,
I want to replace powers of y^2 in the polynomial with
corresponding powers of x^3 + A * x + B,
so that the polynomial depends only on x.
My idea was this:
J = ideal(f, y ** 2 - (x ** 3 + A * x + B))
f = R(J.elimination_ideal(y).gens()[0])
(R is a univariate polynomial ring).
My problem is, sometimes it works, sometimes it does not
(I don't know why).
Is there a better solution or standard solution?
For counting points on elliptic curves,
the following resources might be useful:
SageMath constructions: algebraic geometry: point couting on curves
SageMath thematic tutorial on Schoof-Elkies-Atkin point counting
Regarding polynomials, consider providing
an example that works and one that fails.
That might help analyse the problem and answer the question.
Note that Ask Sage
has more experts ready to answer SageMath questions.

Elliptic Curve with a secant line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Currently I was trying to solve a problem with the condition below:
1. giving an elliptic curve y^2 = x^3 + ax + b
2. the user will input a, b and two points that exactly on the curve.
To put it simply, what I really need to do is make a secant line of the graph with the two points P and Q and try to check that if there is any point of intersection existing. If it exists, get the x and y of this point. I'm so struggled to solve this problem. Could someone give me some hints?
I'd homogenize this to x^3 + axz^2 + bz^3 - y^2z = 0 and points P = [Px : Py : Pz] and Q = [Qx : Qy : Qz]. Then any point R = λP + μQ with (λ, μ) ≠ (0, 0) lies on the line spanned by P and Q. If you wanted to avoid homogenizations, you'd require λ+μ=1 but that usually leads to divisions I'd rather avoid until the very end.
Plug the resulting coordinates of R into the homogenized equation of the elliptic curve, and you obtain a homogeneous cubic equation in λ and μ, i.e. something like
αλ³ + βλ²μ + γλμ² + δμ³ = 0
with the α, β, γ and δ depending on your a, b, P, Q. For μ=0 you get a coordinate vector which is a multiple of P, and as homogeneous coordinates identify multiples, you get point P itself, which lies on the curve. So μ=0 must satisfy the equation, hence you know α=0 even before you compute it. Likewise λ=0 represents Q so δ=0 if that point lies on the curve. You are left with
(βλ + γμ)λμ = 0
The trailing two factors encode the two known intersections I just mentioned. The parenthesis is the third intersection, the one you need. Now simply pick λ=γ and μ=−β to obtain a simple expression for the third point of intersection.
If you want to dehomogenize at the end, simply divide the first two coordinates of the resulting homogeneous coordinate vector by the third.
If I didn't mess up my sympy computation, you have
β = 3*Px^2*Qx + 2*Px*Pz*Qz*a - Py^2*Qz - 2*Py*Pz*Qy + Pz^2*Qx*a + 3*Pz^2*Qz*b
γ = 3*Px*Qx^2 + 2*Pz*Qx*Qz*a - Pz*Qy^2 - 2*Py*Qy*Qz + Px*Qz^2*a + 3*Pz*Qz^2*b
Which is expectedly very symmetric in P and Q. So essentially you just need a single function, and then you get β=f(P,Q) and γ=f(Q,P).
In C++ and with the whole homogenization / dehomogenization in place:
inline double f(double Px, double Py, double Qx, double Qy, double a, double b) {
return 3*Px*Px*Qx + 2*Px*a - Py*Py - 2*Py*Qy + Qx*a + 3*b;
}
std::pair<double, double> third_intersection(double Px, double Py, double Qx, double Qy, double a, double b) {
double beta = f(Px, Py, Qx, Qy, a, b);
double gamma = f(Qx, Qy, Px, Py, a, b);
double denominator = gamma - beta; // Might be zero if line PQ is an asymptote!
double x = (gamma*Px - beta*Qx) / denominator;
double y = (gamma*Py - beta*Qy) / denominator;
return std::make_pair(x, y);
}

Algorithm for determine the Arc Mid Point

I am currently looking to implement an algorithm that will be able to compute the arc midpoint. From here on out, I will be referring to the diagram below. What is known are the start and end nodes (A and B respectively), the center (point C) and point P which is the intersection point of the line AB and CM (I am able to find this point without knowing point M because line AB is perpendicular to line CM and thus, the slope is -1/m). I also know the arc angle and the radius of the arc. I am looking to find point M.
I have been looking at different sources. Some suggest converting coordinates to polar, computing the mid point from the polar coordinates then reverting back to Cartesian. This involves sin and cos (and arctan) which I am a little reluctant to do since trig functions take computing time.
I have been looking to directly computing point M by treating the arc as a circle and having Line CP as a line that intersects the circle at Point M. I would then get two values and the value closest to point P would be the correct intersection point. However, this method, the algebra becomes long and complex. Then I would need to create special cases for when P = C and for when the line AB is horizontal and vertical. This method is ok but I am wondering if there are any better methods out there that can compute this point that are simpler?
Also, as a side note, I will be creating this algorithm in C++.
A circumference in polar form is expressed by
x = Cx + R cos(alpha)
y = Cy + R sin(alpha)
Where alpha is the angle from center C to point x,y. The goal now is how to get alpha without trigonometry.
The arc-midpoint M, the point S in the middle of the segment AB, and your already-calculated point P, all of them have the same alpha, they are on the same line from C.
Let's get vector vx,vy as C to S. Also calculate its length:
vx = Sx - Cx = (Ax + Bx)/2 - Cx
vy = Sy - Cy = (Ay + By)/2 - Cy
leV = sqrt(vx * vx + vy * vy)
I prefer S to P because we can avoid some issues like infinite CP slope or sign to apply to slope (towards M or its inverse).
By defintions of sin and cos we know that:
sin(alpha) = vy / leV
cos(alpha) = vx / leV
and finally we get
Mx = Cx + R * vx / leV
My = Cy + R * vy / leV
Note: To calculate Ryou need another sqrt function, which is not quick, but it's faster than sin or cos.
For better accuracy use the average of Ra= dist(AC) and Rb= dist(BC)
I would then get two values
This is algebraically unavoidable.
and the value closest to point P would be the correct intersection point.
Only if the arc covers less than 180°.
Then I would need to create special cases for when P = C
This is indeed the most tricky case. If A, B, C lie on a line, you don't know which arc is the arc, and won't be able to answer the question. Unless you have some additional information to start with, e.g. know that the arc goes from A to B in a counter-clockwise direction. In this case, you know the orientation of the triangle ABM and can use that to decide which solition to pick, instead of using the distance.
and for when the line AB is horizontal and vertical
Express a line as ax + by + c = 0 and you can treat all slopes the same. THese are homogeneous coordinates of the line, you can compute them e.g. using the cross product (a, b, c) = (Ax, Ay, 1) × (Bx, By, 1). But more detailed questions on how best to compute these lines or intersect it with the circle should probably go to the Math Stack Exchange.
if there are any better methods out there that can compute this point that are simpler?
Projective geometry and homogeneous coordinates can avoid a lot of nasty corner cases, like circles of infinite radius (also known as lines) or the intersection of parallel lines. But the problem of deciding between two solutions remains, so it probably doesn't make things as simple as you'd like them to be.

Vector Resultant Angle and Direction [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am working with set of points, and my goal is to add 4 vectors together and calculate the resultant between start and end point,
Since I already prepared the code to do the above part which seems to be working fine but am not to sure about it.
Anyway the real reason I am posting this question is to do with the resultant direction and angle.
I find it hard to either understand the concept of finding resultant angle and direction as well as PROGRAMMING wise.
Consider this scenario....Image
1st add vectors "Head 2 Tail"
From what I have learned about vector addition is to subtract x2 - x1, y2 -y1 this will dive me the misplacement difference and do the same calculation for all the points from A-E
To get the resultant I square root all the points x to power of 2 and add all the y position to power of 2.
This ideology seem to work fine.....
But the QUESTION here is how do i get the angle and direction of that resultant....?
the code I use to calculate resultant:
double Pta;
double Ptb;
Point vect;
float R1, R2;
float resultant;
for(vector<Point>::iterator iter_a = Left_Arm_xy.begin()+1; iter_a != Left_Arm_xy.end(); ++iter_a)
{
if(center.y <= 240)
{
vect.x = iter_a->x - (iter_a -1)->x;
vect.y = iter_a->y - (iter_a -1)->y;
vect_add.push_back(Point(vect.x,vect.y));
for(vector<Point>::iterator iter_v = vect_add.begin(); iter_v - vect_add.begin() + 4 < vect_add.size(); iter_v+=4)
{
R1 = iter_v->x + (iter_v +1)->x + (iter_v +2)->x + (iter_v +3)->x;
R2 = iter_v->y + (iter_v +1)->y + (iter_v +2)->y + (iter_v +3)->y;
resultant = sqrt(pow(R1,2) + pow(R2,2));
}
}
Consider this..............
Ok lets consider Points A[2,4], B[4,8], C[10,12], To add this vectors i add vectors/points I subtract point B x4 - A x2 and point B y8 - A y4 and point C x10 - B x4 and point C y12 - B y8 this will give me the the displacements between points....Now to get the Resultant i add all the Points X's and Y's x's 2+4+10 = 16 y's 4+8+12 = 24, Next i would square root 16 ^2 + 24^2 = 28.84. So based on these calculations where resultant is a number not and x and y value how can i get the direction and angle....?
It is a simple summation of vectors.
(x, y) = (x1, y1) + (x2, y2) + ... = (x1+x2+..., y1+y2+...)
When you have the final vector, it's angle is found by using
tan(angle) = y/x
The angle between two vectors is generally defined as:
Angle = arccos( DotProduct(v1, v2) / ( Length(v1) * Length(v2) ) );
The direction is simply subtraction of the two vectors:
Direction = v2 - v1;
Usually, you have to normalize this to get a unit vector:
Len = SquareRoot( direction.x * direction.x + direction.y * direction.y );
Direction.x /= Len;
Direction.y /= Len;
Thus, you'll have a unit direction vector and the angle of the vector.

2D Collision Detection Code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Does anyone know a very simple physics engine, or just a set of basic functions that could complete these tasks: Simple point, line, and rectangle collision detection? I looked at Box2D but it is way too advanced for what I am making. I just need some simple code. Thanks in advance!
Here's my shot at point/line collision detection. The important thing is to avoid trig functions, divisions, and other expensive operations so as not to slow things down too much.
As GMan's comment notes, you need to bear in mind that the point will be moving. So you'll have the current position of the point (let's call it A) and the possible new position of the point (B). You need to find out if, when the point moves from A to B, it would collide with a line.
Let's call the start and end points of the line C and D. A collision will occur only if the lines AB and CD intersect.
Let's describe the line AB using the standard equation Ux + Vy + W = 0. After some algebra, the equation comes out as:
(ay - by) x + (bx - ax) y + ax by - bx ay = 0
We can describe the line CD in terms of a parameter t and constants X Y U V:
x = X + Ut
y = Y + Vt
It's helpful if we set t=0 at point C, and t=1 at point D. By considering t=0 we can work out X and Y. And by considering t=1 we can work out U and V. This gives
x = cx + (dx - cx) t
y = cy + (dy - cy) t
To find the intersection point of the two lines, substitute these into the equation we found for AB, which gives
(ay - by) (cx + (dx - cx) t) + (bx - ax) (cy + (dy - cy) t) + ax by - bx ay = 0
This reduces to
t = (ax by - bx ay + bx cy - cx by + cx ay - ax cy) / q
where q = (ay - by)(cx - dx) - (ax - bx)(cy - dy)
If q is zero, the lines are parallel and do not meet.
If 0 < t < 1 then the line extrapolated from AB intersects CD.
But we still don't know that this intersection is actually between the points A and B. So we need to repeat all the previous steps, swapping AB and CD, and writing the line AB in terms of a parameter s. This gives:
s = (cx dy - dx cy + dx ay - ax dy + ax cy - cx ay) / q
If 0 < s < 1 then the line extrapolated from CD intersects AB.
That's it. So in your code you start by calculating q. If q is zero then the lines are parallel. Otherwise, go on to calculate t and s. If 0 < t < 1 and 0 < s < 1 then a collision is about to happen. To find the location of the collision, substitute t or s back into the original equations for CD.
For extra speed you can remove the divisions by q - it's possible to just check whether the top half of each fraction is in the correct range, and then each check should only need 10 multiplication operations.
Is there a reason you couldn't use basic geometry for this?
Two points collide when their coordinates are the same.
A point intersects with a line when its position is a solution to the equation for the line.
A point intersects a rectangle when the point is bounded by the rectangle.
More complex composite cases can be constructed by composing these cases. Were you specifically looking for a library for some reason?