How to calculate the vertex of a parabola given three points - curve-fitting

I have three X/Y points that form a parabola. I simply need to calculate what the vertex of the parabola is that goes through these three points. Preferably a quick way as I have to do a LOT of these calculations!
The "Ask A Scientist" website provides this answer:
The general form of a parabola is given by the equation: A * x^2 + B * x + C = y where A, B, and C are arbitrary Real constants. You have three pairs of points that are (x,y) ordered pairs. Substitute the x and y values of each point into the equation for a parabola. You will get three LINEAR equations in three unknowns, the three constants. You can then easily solve this system of three equations for the values of A, B, and C, and you'll have the equation of the parabola that intersects your 3 points. The vertex is where the first derivative is 0, a little algebra gives: ( -B/2A , C - B^2/4A ) for the vertex.
It would be nice to see actual code that does this calculation in C# or C++. Anybody?

Thanks David, I converted your pseudocode to the following C# code:
public static void CalcParabolaVertex(int x1, int y1, int x2, int y2, int x3, int y3, out double xv, out double yv)
{
double denom = (x1 - x2) * (x1 - x3) * (x2 - x3);
double A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom;
double B = (x3*x3 * (y1 - y2) + x2*x2 * (y3 - y1) + x1*x1 * (y2 - y3)) / denom;
double C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom;
xv = -B / (2*A);
yv = C - B*B / (4*A);
}
This is what I wanted. A simple calculation of the parabola's vertex. I'll handle integer overflow later.

This is really just a simple linear algebra problem, so you can do the calculation symbolically. When you substitute in the x and y values of your three points, you'll get three linear equations in three unknowns.
A x1^2 + B x1 + C = y1
A x2^2 + B x2 + C = y2
A x3^2 + B x3 + C = y3
The straightforward way to solve this is to invert the matrix
x1^2 x1 1
x2^2 x2 1
x3^2 x3 1
and multiply it by the vector
y1
y2
y3
The result of this is... okay, not exactly all that simple ;-) I did it in Mathematica, and here are the formulas in pseudocode:
denom = (x1 - x2)(x1 - x3)(x2 - x3)
A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom
B = (x3^2 * (y1 - y2) + x2^2 * (y3 - y1) + x1^2 * (y2 - y3)) / denom
C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom
Alternatively, if you wanted to do the matrix math numerically, you'd typically turn to a linear algebra system (like ATLAS, though I'm not sure if it has C#/C++ bindings).
In any case, once you have the values of A, B, and C as calculated by these formulas, you just have to plug them into the expressions given in the question, -B / 2A and C - B^2/4A, to calculate the coordinates of the vertex.1
Note that if the original three points have coordinates that make denom a very large or very small number, doing the calculation directly might be susceptible to significant numerical error. In that case it might be better to modify it a bit, to avoid dividing by the denominators where they would cancel out anyway:
denom = (x1 - x2)(x1 - x3)(x2 - x3)
a = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2))
b = (x3^2 * (y1 - y2) + x2^2 * (y3 - y1) + x1^2 * (y2 - y3))
c = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3)
and then the coordinates of the vertex are -b / 2a and (c - b^2 / 4a) / denom. There are various other situations that might benefit from "tricks" like this, such as if A is very large or very small, or if C is nearly equal to B^2 / 4A so that their difference is very small, but I think those situations vary enough that a full discussion would be better left for case-by-case followup questions.
Converting all of this to code in the language of your choice is left as an exercise for the reader. (It should be pretty trivial in any language that uses standard infix operator syntax, e.g. as AZDean showed in C#.)
1In the initial version of the answer I thought this would be obvious, but it seems there are a lot of people who like having it mentioned explicitly.

You get the following three equations by direct substitution:
A*x1^2+B*x1+C=y1
A*x2^2+B*x2+C=y2
A*x3^2+B*x3+C=y3
You can solve this by noting that this is equivalent to the matrix product:
[x1^2 x1 1] [A] [y1]
|x2^2 x2 1|*|B| = |y2|
[x3^2 x3 1] [C] [y3]
So you can get A,B, and C by inverting the matrix and multiplying the inverse with the vector on the right.
I see that while I've been posting this John Rasch has linked to tutorial that goes into more depth on actually solving the matrix equation, so you can follow those instructions to get the answer. Inverting a 3x3 matrix is quite easy, so this shouldn't be too tough.

Here is a code in Fortran that implements #david-z and #AZDean's solution:
subroutine parabola_vertex(x1, y1, x2, y2, x3, y3, xv, yv)
real(dp), intent(in) :: x1, y1, x2, y2, x3, y3
real(dp), intent(out) :: xv, yv
real(dp) :: denom, A, B, C
denom = (x1 - x2) * (x1 - x3) * (x2 - x3)
A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom
B = (x3**2 * (y1 - y2) + x2**2 * (y3 - y1) + x1**2 * (y2 - y3)) / denom
C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + &
x1 * x2 * (x1 - x2) * y3) / denom
xv = -B / (2*A)
yv = C - B**2 / (4*A)
end subroutine

I've done something similar to #piSHOCK's answer, also based on #AZDean's code. If you need to run it heavily (or to use it in Matlab like me), this might be the fastest one.
My assumption is that x1 == -1, x2 == 0, x3 == 1.
a = y2 - ( y1 + y3) / 2 % opposite signal compared to the original definition of A
b = (y3 - y1) / 4 % half of the originally defined B
xExtr = b / a
yExtr = y2 + b * xExtr % which is equal to y2 + b*b / a

def vertex(x1,x2,x3,y1,y2,y3):
'''Given three pairs of (x,y) points return the vertex of the
parabola passing through the points. Vectorized and common expression reduced.'''
#Define a sequence of sub expressions to reduce redundant flops
x0 = 1/x2
x4 = x1 - x2
x5 = 1/x4
x6 = x1**2
x7 = 1/x6
x8 = x2**2
x9 = -x7*x8 + 1
x10 = x0*x1*x5*x9
x11 = 1/x1
x12 = x3**2
x13 = x11*x12
x14 = 1/(x0*x13 - x0*x3 - x11*x3 + 1)
x15 = x14*y3
x16 = x10*x15
x17 = x0*x5
x18 = -x13 + x3
x19 = y2*(x1*x17 + x14*x18*x6*x9/(x4**2*x8))
x20 = x2*x5
x21 = x11*x20
x22 = x14*(-x12*x7 + x18*x21)
x23 = y1*(-x10*x22 - x21)
x24 = x16/2 - x19/2 - x23/2
x25 = -x17*x9 + x7
x26 = x0*x1*x14*x18*x5
x27 = 1/(-x15*x25 + y1*(x20*x7 - x22*x25 + x7) + y2*(-x17 + x25*x26))
x28 = x24*x27
return x28,x15 + x22*y1 + x24**2*x27 - x26*y2 + x28*(-x16 + x19 + x23)

This smells like homework. "Ask a Scientist" is right on. Say your 3 points are (x1, y1), (x2, y2), and (x3, y3). Then, you get three linear equations:
| M11 M12 M13 | | A | | Z1 |
| M21 M22 M23 | * | B | = | Z2 |
| M31 M32 M33 | | C | | Z3 |
Where M11 = x12, M12 = x1, M13 = 1, Z1 = y1, and similarly for the other two rows using (x2, y2) and (x3, y3) in place of (x1, y1).
Solving this system of 3 equations will give you a solution for A, B, and C.

Running at https://ideone.com/y0SxKU
#include <iostream>
using namespace std;
// calculate the vertex of a parabola given three points
// https://stackoverflow.com/q/717762/16582
// #AZDean implementation with given x values
void CalcParabolaVertex(int x1, int y1, int x2, int y2, int x3, int y3, double& xv, double& yv)
{
double denom = (x1 - x2) * (x1 - x3) * (x2 - x3);
double A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom;
double B = (x3*x3 * (y1 - y2) + x2*x2 * (y3 - y1) + x1*x1 * (y2 - y3)) / denom;
double C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom;
xv = -B / (2*A);
yv = C - B*B / (4*A);
}
// #piSHOCK immplementation assuming regular x values ( wrong!!! )
void CalcParabolaVertex2( int y1, int y2, int y3, double& xv, double& yv)
{
double d1 = y1 - y2;
double d2 = y1 - y3;
double a = -d1 + 0.5 * d2;
double b = 2 * d1 - 0.5 * d2;
double c = -y1;
xv = -0.5 * b / a;
yv = c - 0.25 * b * b / a;
}
// corrected immplementation assuming regular x values
void CalcParabolaVertex3( int y1, int y2, int y3, double& xv, double& yv)
{
double d1 = y1 - y2;
double d2 = y1 - y3;
double a = d1 - 0.5 * d2;
double b = -2 * d1 + 0.5 * d2;
double c = y1;
xv = -0.5 * b / a;
yv = c - 0.25 * b * b / a;
}
int main() {
double xv, yv;
CalcParabolaVertex( 0, 100, 1, 500, 2, 200, xv, yv );
cout << xv <<" "<< yv << "\n";
CalcParabolaVertex2( 100, 500, 200, xv, yv );
cout << xv <<" "<< yv << "\n";
CalcParabolaVertex3( 100, 500, 200, xv, yv );
cout << xv <<" "<< yv << "\n";
return 0;
}
I have added a couple of unit tests for negative going peaks: running live at https://ideone.com/WGK90S

Related

Closed form solution for affine transformation matrix between 2 set of 2D points

Given affine transform equation with 6 unknown variables a_11, a_12, a_21, a_22, b_1, b_2 and corresponding 3 pairs of points x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3:
u = a_11 * x + a_12 * y + b_1
v = a_21 * x + a_22 * y + b_2
Is it possible to get solution for a_11, a_12, a_21, a_22, b_1, b_2 in the this form?
a_11 = f1(x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3)
a_12 = f2(x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3)
a_21 = f3(x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3)
a_22 = f4(x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3)
b_1 = f5(x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3)
b_2 = f6(x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3)
I know that this system can be solved via linear system solver like numpy.linalg.solve, but I can't use it and I need f1, f2, f3, f4, f5, f6 to contain common math operations.
Is it possible in general? Which tools can be used to solve such system of equations? Seems maple and mathematica have equation solver, is it suitable for this task? is there any alternatives?
I assume you mean the points (x1,y1,u1,v1), (x2,y2,u2,v2), and (x3,y3,u3,v3) satisfy the equations. In such case, substituting those in, yields six equations with six unknowns. Many of the coefficients are zero or one. Hence with a bit of canceling we get your requested solutions.
Your solutions (assuming you have linear independence) are:
a11 = ((u1-u2)(y2-y3)-(u2-u3)(y1-y2))/((x1-x2)(y2-y3)-((x2-x3)(y1-y2))
a12 = ((u1-u2)(x2-x3)-(u2-u3)(x1-x2))/((y1-y2)(x2-x3)-((y2-y3)(x1-x2))
a21 = ((v1-v2)(y2-y3)-(v2-v3)(y1-y2))/((x1-x2)(y2-y3)-((x2-x3)(y1-y2))
a22 = ((v1-v2)(x2-x3)-(v2-v3)(x1-x2))/((y1-y2)(x2-x3)-((y2-y3)(x1-x2))
b1 = u1-a11×x1-a12×y2
b2 = v1-a21×x1-a22×y2
I should point out. This question is more for mathoverflow. Really has nothing to do with programming.
Finally I used sympy to solve this system of equations:
from sympy.solvers.solveset import linsolve
from sympy import *
#u1 = a_11 * x1 + a_12 * y1 + b_1
#v1 = a_21 * x1 + a_22 * y1 + b_2
#u2 = a_11 * x2 + a_12 * y2 + b_1
#v2 = a_21 * x2 + a_22 * y2 + b_2
#u3 = a_11 * x3 + a_12 * y3 + b_1
#v3 = a_21 * x3 + a_22 * y3 + b_2
a_11, a_12, a_21, a_22, b_1, b_2, x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3 = \
symbols('a_11, a_12, a_21, a_22, b_1, b_2, x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3')
s = linsolve([a_11 * x1 + a_12 * y1 + b_1-u1,
a_21 * x1 + a_22 * y1 + b_2-v1,
a_11 * x2 + a_12 * y2 + b_1-u2,
a_21 * x2 + a_22 * y2 + b_2-v2,
a_11 * x3 + a_12 * y3 + b_1-u3,
a_21 * x3 + a_22 * y3 + b_2-v3], (a_11, a_12, a_21, a_22, b_1, b_2))
print(s)
The answer is:
a_11 = (u1 * y2 - u1 * y3 - u2 * y1 + u2 * y3 + u3 * y1 - u3 * y2) / (
x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2)
a_12 = (-u1 * x2 + u1 * x3 + u2 * x1 - u2 * x3 - u3 * x1 + u3 * x2) / (
x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2)
a_21 = (v1 * y2 - v1 * y3 - v2 * y1 + v2 * y3 + v3 * y1 - v3 * y2) / (
x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2)
a_22 = (-v1 * x2 + v1 * x3 + v2 * x1 - v2 * x3 - v3 * x1 + v3 * x2) / (
x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2)
b_1 = (u1 * x2 * y3 - u1 * x3 * y2 - u2 * x1 * y3 + u2 * x3 * y1 + u3 * x1 * y2 - u3 * x2 * y1) / (
x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2)
b_2 = (v1 * x2 * y3 - v1 * x3 * y2 - v2 * x1 * y3 + v2 * x3 * y1 + v3 * x1 * y2 - v3 * x2 * y1) / (
x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2)
But it looks like it can be simplified.

Rotation of a point around another point in pitch axis in C

I use the following code (from stackoverflow) to rotate point around another point.
It work great for the yaw rotation axis.
How do I modifiy this code for a pitch axis rotation?
double x1 = point.x - center.x;
double y1 = point.y - center.y;
double x2 = x1 * Math.cos(angle) - y1 * Math.sin(angle));
double y2 = x1 * Math.sin(angle) + y1 * Math.cos(angle));
point.x = x2 + center.x;
point.y = y2 + center.y;

Efficiently list every unique pair of points in a grid

Seemingly an easy problem, I am looking for an algorithm to list every unique pair of points in a grid that are not collinear with the origin. The trick is to somehow avoid all the different symmetries and coincidences. This is what I have so far:
for( int x1 = 0; x1 <= s; x1++ ){
for( int y1 = 0; y1 <= s; y1++ ){
if( x1 == 0 && y1 == 0 ) continue; // P1 is coincident with origin
for( int x2 = x1; x2 <= s; x2++ ){
for( int y2 = 0; y2 <= s; y2++ ){
if( x1 == x2 && y2 >= y1 ) continue;
if( x1 * y2 == x2 * y1 ) continue; // points are collinear with origin
// this is a valid point
}
}
}
}
Enumerate all unique pairs (by defining an order on the grid points) and disregard those that are aligned with the origin.
def Pair(X0, Y0, X1, Y1):
if X0 * Y1 != X1 * Y0:
# Non collinear with the origin
Accept(X0, Y0, X1, Y1)
# Enumerate all (X0, Y0)
for X0 in range(NX):
for Y0 in range(NY):
# Enumerate all (X1, Y1) > (X0, Y0) in the lexicographical sense
# 1) X1 == X0, Y1 > Y0
X1= X0
for Y1 in range(Y0 + 1, NY):
Pair(X0, Y0, X1, Y1)
# 2) X1 > X0
for X1 in range(X0 + 1, NX):
for Y1 in range(NY):
Pair(X0, Y0, X1, Y1)
Or, better:
# Enumerate all (X0, Y0)
for X0 in range(NX):
for Y0 in range(NY):
# Enumerate all (X1, Y1) > (X0, Y0) in the lexicographical sense
for X1 in range(X0, NX):
for Y1 in range(0 if X1 > X0 else Y0 + 1, NY):
if X0 * Y1 != X1 * Y0:
# Non collinear with the origin
Accept(X0, Y0, X1, Y1)

Algorithm to find local minimum by parabolic interpolation of three points [duplicate]

I have three X/Y points that form a parabola. I simply need to calculate what the vertex of the parabola is that goes through these three points. Preferably a quick way as I have to do a LOT of these calculations!
The "Ask A Scientist" website provides this answer:
The general form of a parabola is given by the equation: A * x^2 + B * x + C = y where A, B, and C are arbitrary Real constants. You have three pairs of points that are (x,y) ordered pairs. Substitute the x and y values of each point into the equation for a parabola. You will get three LINEAR equations in three unknowns, the three constants. You can then easily solve this system of three equations for the values of A, B, and C, and you'll have the equation of the parabola that intersects your 3 points. The vertex is where the first derivative is 0, a little algebra gives: ( -B/2A , C - B^2/4A ) for the vertex.
It would be nice to see actual code that does this calculation in C# or C++. Anybody?
Thanks David, I converted your pseudocode to the following C# code:
public static void CalcParabolaVertex(int x1, int y1, int x2, int y2, int x3, int y3, out double xv, out double yv)
{
double denom = (x1 - x2) * (x1 - x3) * (x2 - x3);
double A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom;
double B = (x3*x3 * (y1 - y2) + x2*x2 * (y3 - y1) + x1*x1 * (y2 - y3)) / denom;
double C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom;
xv = -B / (2*A);
yv = C - B*B / (4*A);
}
This is what I wanted. A simple calculation of the parabola's vertex. I'll handle integer overflow later.
This is really just a simple linear algebra problem, so you can do the calculation symbolically. When you substitute in the x and y values of your three points, you'll get three linear equations in three unknowns.
A x1^2 + B x1 + C = y1
A x2^2 + B x2 + C = y2
A x3^2 + B x3 + C = y3
The straightforward way to solve this is to invert the matrix
x1^2 x1 1
x2^2 x2 1
x3^2 x3 1
and multiply it by the vector
y1
y2
y3
The result of this is... okay, not exactly all that simple ;-) I did it in Mathematica, and here are the formulas in pseudocode:
denom = (x1 - x2)(x1 - x3)(x2 - x3)
A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom
B = (x3^2 * (y1 - y2) + x2^2 * (y3 - y1) + x1^2 * (y2 - y3)) / denom
C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom
Alternatively, if you wanted to do the matrix math numerically, you'd typically turn to a linear algebra system (like ATLAS, though I'm not sure if it has C#/C++ bindings).
In any case, once you have the values of A, B, and C as calculated by these formulas, you just have to plug them into the expressions given in the question, -B / 2A and C - B^2/4A, to calculate the coordinates of the vertex.1
Note that if the original three points have coordinates that make denom a very large or very small number, doing the calculation directly might be susceptible to significant numerical error. In that case it might be better to modify it a bit, to avoid dividing by the denominators where they would cancel out anyway:
denom = (x1 - x2)(x1 - x3)(x2 - x3)
a = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2))
b = (x3^2 * (y1 - y2) + x2^2 * (y3 - y1) + x1^2 * (y2 - y3))
c = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3)
and then the coordinates of the vertex are -b / 2a and (c - b^2 / 4a) / denom. There are various other situations that might benefit from "tricks" like this, such as if A is very large or very small, or if C is nearly equal to B^2 / 4A so that their difference is very small, but I think those situations vary enough that a full discussion would be better left for case-by-case followup questions.
Converting all of this to code in the language of your choice is left as an exercise for the reader. (It should be pretty trivial in any language that uses standard infix operator syntax, e.g. as AZDean showed in C#.)
1In the initial version of the answer I thought this would be obvious, but it seems there are a lot of people who like having it mentioned explicitly.
You get the following three equations by direct substitution:
A*x1^2+B*x1+C=y1
A*x2^2+B*x2+C=y2
A*x3^2+B*x3+C=y3
You can solve this by noting that this is equivalent to the matrix product:
[x1^2 x1 1] [A] [y1]
|x2^2 x2 1|*|B| = |y2|
[x3^2 x3 1] [C] [y3]
So you can get A,B, and C by inverting the matrix and multiplying the inverse with the vector on the right.
I see that while I've been posting this John Rasch has linked to tutorial that goes into more depth on actually solving the matrix equation, so you can follow those instructions to get the answer. Inverting a 3x3 matrix is quite easy, so this shouldn't be too tough.
Here is a code in Fortran that implements #david-z and #AZDean's solution:
subroutine parabola_vertex(x1, y1, x2, y2, x3, y3, xv, yv)
real(dp), intent(in) :: x1, y1, x2, y2, x3, y3
real(dp), intent(out) :: xv, yv
real(dp) :: denom, A, B, C
denom = (x1 - x2) * (x1 - x3) * (x2 - x3)
A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom
B = (x3**2 * (y1 - y2) + x2**2 * (y3 - y1) + x1**2 * (y2 - y3)) / denom
C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + &
x1 * x2 * (x1 - x2) * y3) / denom
xv = -B / (2*A)
yv = C - B**2 / (4*A)
end subroutine
I've done something similar to #piSHOCK's answer, also based on #AZDean's code. If you need to run it heavily (or to use it in Matlab like me), this might be the fastest one.
My assumption is that x1 == -1, x2 == 0, x3 == 1.
a = y2 - ( y1 + y3) / 2 % opposite signal compared to the original definition of A
b = (y3 - y1) / 4 % half of the originally defined B
xExtr = b / a
yExtr = y2 + b * xExtr % which is equal to y2 + b*b / a
def vertex(x1,x2,x3,y1,y2,y3):
'''Given three pairs of (x,y) points return the vertex of the
parabola passing through the points. Vectorized and common expression reduced.'''
#Define a sequence of sub expressions to reduce redundant flops
x0 = 1/x2
x4 = x1 - x2
x5 = 1/x4
x6 = x1**2
x7 = 1/x6
x8 = x2**2
x9 = -x7*x8 + 1
x10 = x0*x1*x5*x9
x11 = 1/x1
x12 = x3**2
x13 = x11*x12
x14 = 1/(x0*x13 - x0*x3 - x11*x3 + 1)
x15 = x14*y3
x16 = x10*x15
x17 = x0*x5
x18 = -x13 + x3
x19 = y2*(x1*x17 + x14*x18*x6*x9/(x4**2*x8))
x20 = x2*x5
x21 = x11*x20
x22 = x14*(-x12*x7 + x18*x21)
x23 = y1*(-x10*x22 - x21)
x24 = x16/2 - x19/2 - x23/2
x25 = -x17*x9 + x7
x26 = x0*x1*x14*x18*x5
x27 = 1/(-x15*x25 + y1*(x20*x7 - x22*x25 + x7) + y2*(-x17 + x25*x26))
x28 = x24*x27
return x28,x15 + x22*y1 + x24**2*x27 - x26*y2 + x28*(-x16 + x19 + x23)
This smells like homework. "Ask a Scientist" is right on. Say your 3 points are (x1, y1), (x2, y2), and (x3, y3). Then, you get three linear equations:
| M11 M12 M13 | | A | | Z1 |
| M21 M22 M23 | * | B | = | Z2 |
| M31 M32 M33 | | C | | Z3 |
Where M11 = x12, M12 = x1, M13 = 1, Z1 = y1, and similarly for the other two rows using (x2, y2) and (x3, y3) in place of (x1, y1).
Solving this system of 3 equations will give you a solution for A, B, and C.
Running at https://ideone.com/y0SxKU
#include <iostream>
using namespace std;
// calculate the vertex of a parabola given three points
// https://stackoverflow.com/q/717762/16582
// #AZDean implementation with given x values
void CalcParabolaVertex(int x1, int y1, int x2, int y2, int x3, int y3, double& xv, double& yv)
{
double denom = (x1 - x2) * (x1 - x3) * (x2 - x3);
double A = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom;
double B = (x3*x3 * (y1 - y2) + x2*x2 * (y3 - y1) + x1*x1 * (y2 - y3)) / denom;
double C = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom;
xv = -B / (2*A);
yv = C - B*B / (4*A);
}
// #piSHOCK immplementation assuming regular x values ( wrong!!! )
void CalcParabolaVertex2( int y1, int y2, int y3, double& xv, double& yv)
{
double d1 = y1 - y2;
double d2 = y1 - y3;
double a = -d1 + 0.5 * d2;
double b = 2 * d1 - 0.5 * d2;
double c = -y1;
xv = -0.5 * b / a;
yv = c - 0.25 * b * b / a;
}
// corrected immplementation assuming regular x values
void CalcParabolaVertex3( int y1, int y2, int y3, double& xv, double& yv)
{
double d1 = y1 - y2;
double d2 = y1 - y3;
double a = d1 - 0.5 * d2;
double b = -2 * d1 + 0.5 * d2;
double c = y1;
xv = -0.5 * b / a;
yv = c - 0.25 * b * b / a;
}
int main() {
double xv, yv;
CalcParabolaVertex( 0, 100, 1, 500, 2, 200, xv, yv );
cout << xv <<" "<< yv << "\n";
CalcParabolaVertex2( 100, 500, 200, xv, yv );
cout << xv <<" "<< yv << "\n";
CalcParabolaVertex3( 100, 500, 200, xv, yv );
cout << xv <<" "<< yv << "\n";
return 0;
}
I have added a couple of unit tests for negative going peaks: running live at https://ideone.com/WGK90S

Checking to see if 3 points are on the same line

I want to know a piece of a code which can actually tell me if 3 points in a 2D space are on the same line or not. A pseudo-code is also sufficient but Python is better.
You can check if the area of the ABC triangle is 0:
[ Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By) ] / 2
Of course, you don't actually need to divide by 2.
This is C++, but you can adapt it to python:
bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) {
return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
}
Basically, we are checking that the slopes between point 1 and point 2 and point 1 and point 3 match. Slope is change in y divided by change in x, so we have:
y1 - y2 y1 - y3
------- = --------
x1 - x2 x1 - x3
Cross multiplying gives (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
Note, if you are using doubles, you can check against an epsilon:
bool collinear(double x1, double y1, double x2, double y2, double x3, double y3) {
return fabs((y1 - y2) * (x1 - x3) - (y1 - y3) * (x1 - x2)) <= 1e-9;
}
y - y0 = a(x-x0) (1) while a = (y1 - y0)/(x1 - x0) and A(x0, y0) B(x1, y1) C(x2, y2). See whether C statisfies (1). You just replace the appropriate values.
Details
Read this, and use it to find the equation of a line through the first two points. Follow the instructions to find m and b. Then for your third point, calculate mx + b - y. If the result is zero, the third point is on the same line as the first two.
Rule 1: In any linear 2d space, two points are always on the same line.
Take 2 points and build an equation that represents a line through them.
Then check if the third point is also on that line.
Good luck.

Resources