Solutions for y for a rotated ellipse - math

I wish to plot an ellipse by scanline finding the values for y for each value of x.
For a plain ellipse the formula is trivial to find: y = Sqrt[b^2 - (b^2 x^2)/a^2]
But when the axes of the ellipse are rotated I've never been able to figure out how to compute y (and possibly the extents of x)

In parametric form
x[t]= a Cos[t] Cos[psi] - b Sin[t] Sin[psi]
y[t]= b Cos[psi] Sin[t] + a Cos[t] Sin[psi]
Where psi is the rotation angle, and a and b the semi-axes.
The parameter t goes from 0 to 2 Pi.
Or if you prefer in Cartesian non-parametric form:
(a x^2+b y^2) Cos[psi]^2 + (b x^2 +a y^2) Sin[psi]^2 + (a-b) x y Sin[2 psi]==1
Which yields to the two possible solutions for y[x], equivalent to the two solutions for the square root in the non-rotated case:
y -> (-(Sqrt[2]*Sqrt[a + b - 2*a*b*x^2 + (-a + b)*Cos[2*psi]]) +
(-a + b)*x*Sin[2*psi]) / (2*(b*Cos[psi]^2 + a*Sin[psi]^2))
y -> (Sqrt[2]*Sqrt[a + b - 2*a*b*x^2 + (-a + b)*Cos[2*psi]] +
(-a + b)*x*Sin[2*psi])/ (2*(b*Cos[psi]^2 + a*Sin[psi]^2))
Well, you asked for it :)
Those functions give:
And the limits for x are:
LimitX= +/- Sqrt[a + b + (-a + b)*Cos[2*psi]]/(Sqrt[2]*Sqrt[a]*Sqrt[b])

Related

Boolean algebra simplification exercise

So I have been given the following expression, but I cannot seem to solve it, can anyone do this and show the steps please?
Prove XY'Z + XYZ' + XYZ = XY + XZ
XY'Z + XYZ' + XYZ = XY + XZ
Notice X and Z are common factors between XY'Z and XYZ.
XZ(Y' + Y) + XYZ' =
Y' + Y is equal to 1 (if Y=0 then Y'=1 and so 0 + 1 = 1, that is 0 or 1 = 1. Similarly, if Y=1 then Y'=0 and so 1 + 0 = 1). Therefore, what you get is:
XZ·1 + XYZ' =
XZ·1 = XZ since A·1 = A (if A=0 then 0·1 is 0 and if A=1 then 1·1 = 1). Now the function is simplified to:
XZ + XYZ' =
Notice once again X is a common factor between XZ and XYZ'.
X(Z + YZ') =
Notice this time that Z + YZ' is a special case of the distributive law, which is A + A'B = A + B. This is because if we apply the general distributive law A + BC = (A + B)·(A + C) then we get A + A'B = (A + A')·(A + B) = 1·(A + B) = A + B. Following this reasoning we get to simplify the function even further:
X(Z + Y) =
All that's left is for us to use the distributive law and we finally arrive to the final result:
XY + XZ
Please note that nothing is written between variables, an AND operator (or "·" symbol) is assumed. It's just a way to save space.

Solving a system of linear equations

I've been trying to solve a system of linear equations , but I didn't get this done. I hope getting an answer if it's solvable .
Say we have an angle A and this system :
y - x = sin(A)
x - z = sin(A+100)
z - y = sin(A+200)
I'm looking for `x and y and z , the value of the angle isn't constant, but it's known !
If there is a numerical way to solve it I'll be intressed .
PS: this NOT a homework. and everything I've tried is written on papers that 's the reason I didn't put here.
If you add all equations you get
sin(A) + sin(A + 100) + sin(A + 200) = 0
So your system isn't solvable if the above sum is not zero. If it's zero then you have infinite solutions:
x = x, y = x + sin(A), z = x - sin(A + 100)
where x can have any value.
The system can be rearranged to
0 = sin(A) - sin(A+100) - sin(A+200)
x - z = sin(A+100)
- y + z = sin(A+200)
which means that there is no solution if sin(A) - sin(A+100) - sin(A+200) != 0 holds. Otherwise, chose y at will and setting x=sin(A+100)+y and z=sin(A)-y yields a solution.

Sphere center point and radius from 3 points on the surface

Is it possible to find he center of sphere and its radius from 3 points on the surface ?
I'm building a model for a segmented brain structure were the three points would be within the structure; head, tail and middle.
Thank you,
Express that the center of the sphere is equidistant to the three given points and coplanar with them (assuming that the three given points are on a great circle).
(X - Xa)² + (Y - Ya)² + (Z - Za)² = R²
(X - Xb)² + (Y - Yb)² + (Z - Zb)² = R²
(X - Xc)² + (Y - Yc)² + (Z - Zc)² = R²
|X Y Z 1|
|Xa Ya Za 1|
|Xb Yb Zb 1| = 0
|Xc Yc Zc 1|
Subtracting the first equation from the second and the third, you get rid of the quadratic terms.
(2X - Xb - Xa)(Xb - Xa) + (2Y - Yb - Ya)(Yb - Ya) + (2Z - Zb - Za)(Zb - Za) = 0
(2X - Xc - Xa)(Xc - Xa) + (2Y - Yc - Ya)(Yc - Ya) + (2Z - Zc - Za)(Zc - Za) = 0
Now you have an easy linear system of 3 equations in 3 unknowns.
For conciseness you can translate the three points so that Xa=Ya=Za=0, and the equations simplify as
|X Y Z |
|Xb Yb Zb| = 0
|Xc Yc Zc|
(2X - Xb) Xb + (2Y - Yb) Yb + (2Z - Zb) Zb = 0
(2X - Xc) Xc + (2Y - Yc) Yc + (2Z - Zc) Zc = 0
or
(Yb Zc - Yc Zb) X + (Zb Xc - Zc Xb) Y + (Xb Yc - Xc Yb) Z = 0
2 Xb X + 2 Yb Y + 2 Zb Z = Xb² + Yb² + Zb²
2 Xc X + 2 Yc Y + 2 Zc Z = Xc² + Yc² + Zc²
Then, R² = X² + Y² + Z², and don't forget to translate back.
It is indeed possible, but it will always give you a sphere which the surface input points will be on its equator (meaning there are other, larger spheres that have these 3 points on it's surface if there's a fourth not coplanar point given).
But I think the smallest sphere is what you want. The following code written in C# gives you a Vector3 with the center of the sphere. The distance can then be obtained with Vector3.Distance (or a simple pithagorean distance) between any of the input points and the resulting center.
static public Vector3 BarycentricToWorld3D(Vector3 p1, Vector3 p2, Vector3 p3, float u, float v, float w) {
return (u * p1 + v * p2 + w * p3) / (u + v + w);
}
static public Vector3 CircleBariCenter3D(Vector3 p1, Vector3 p2, Vector3 p3) {
Vector3 a = p3 - p2;
Vector3 b = p1 - p3;
Vector3 c = p2 - p1;
float u = Vector3.Dot(a, a) * Vector3.Dot(c, b);
float v = Vector3.Dot(b, b) * Vector3.Dot(c, a);
float w = Vector3.Dot(c, c) * Vector3.Dot(b, a);
return BarycentricToWorld3D(p1, p2, p3, u, v, w);
}
Notice there's no checking for colinear points, which will make this fail. It should be trivial to add such checking by simply feeding this colinear points and watching it fail in the debugger.
Credits: I got a maxscript source for this 10+ years ago and lost the original author and location, and translated it into C# quite recently.

Calculating circle through 2 known points and angle at one point

I know the angle at point A and that the circle goes through point A and point B. There should be a unique solution that gives me the circle center (C) and radius (R) from this information. I've tried to find a formula as follows.
R^2 = (Bx - Cx)^2 + (By - Cy)^2
Cx = Ax - R*dy
Cy = Ay + R*dx
(dx,dy) is a unit vector for the tangent to the circle at point A, which can be found from the angle at point A with sin,cos. The center of the circle is distance R from point A in the direction perpendicular to (dx,dy).
Putting this together gives me
R^2 = (Bx - Ax + R*dy)^2 + (By - Ay - R*dx)^2
Multiplying this out gives me a quadratic for R, but the denominator of the quadratic (the /2a part) is
dx^2 + dy^2 - 1
Since (dx,dy) is a unit vector, the denominator is always 0 and I get a divide by zero error. Where have I gone wrong?
The formulation seems fine to me. Lets expand the expression
R^2 = (Bx - Ax + R dy)^2 + (By - Ay - R dx)^2
this gives
R^2 = Bx^2 + Ax^2 + R^2 dy^2 + 2 Bx R dy - 2 Ax Bx - 2 Ax R dy
+ By^2 + Ay^2 + R^2 dx^2 - 2 By R dx - 2 Ay By + 2 Ay R dx
Rearrange
(1 - dx^2 - dy^2) R^2 + 2 (Ax dy - Ay dx - Bx dy + By dx) R + 2 (Ax Bx + Ay By) = 0
You are correct in that if (dx,dy) is a unit vector the R^2 term vanishes. This is not a problem it just means you have a linear equation to solve.
2 (Ax dy - Ay dx - Bx dy + By dx) R + 2 (Ax Bx + Ay By) = 0
which is easy to solve
R = - (Ax Bx + Ay By) / (Ax dy - Ay dx - Bx dy + By dx)
Things are a little simpler if you let U = (u,v) = (Bx-Ax,By-Ay) be the vector from A to B our equation becomes
R^2 = (u + R dy)^2 + (v - R dx)^2
= u^2 + 2 u dy R + dy^2 R^2 + v^2 - 2 v dx R + dx^2 R^2
Rearrange
(1-dx^2-dy^2) R^2 + 2 (v dx - u dy) R - u^2 - v^2 = 0
Let T=(dx,dy) be the tangent, and N=(dy,-dx) is normal. If they are unit length it simplifies to
2 (N . U) R - U . U = 0
giving
R = (U . U)/ 2 (N . U)
Assuming you mean the angle between the line AB and the tangent line. l, of the circle at A:
The key is to draw the perpendicular bisector of AB and the line perpendicular to l through A, and find the intersection. That will be the center of your circle.
I have solved from my theory.
The slope of the circle is the radius equal 1.
Therefore: the slope=(rise^2+run^2)^(1/2)=1,
so dy^2+dx^2=1^2 and (dy^2+dx^2)^(1/2)=1.

Code or formula for intersection of two parabolas in any rotation

I am working on a geometry problem that requires finding the intersection of two parabolic arcs in any rotation. I was able to intesect a line and a parabolic arc by rotating the plane to align the arc with an axis, but two parabolas cannot both align with an axis. I am working on deriving the formulas, but I would like to know if there is a resource already available for this.
I'd first define the equation for the parabolic arc in 2D without rotations:
x(t) = ax² + bx + c
y(t) = t;
You can now apply the rotation by building a rotation matrix:
s = sin(angle)
c = cos(angle)
matrix = | c -s |
| s c |
Apply that matrix and you'll get the rotated parametric equation:
x' (t) = x(t) * c - s*t;
y' (t) = x(t) * s + c*t;
This will give you two equations (for x and y) of your parabolic arcs.
Do that for both of your rotated arcs and subtract them. This gives you an equation like this:
xa'(t) = rotated equation of arc1 in x
ya'(t) = rotated equation of arc1 in y.
xb'(t) = rotated equation of arc2 in x
yb'(t) = rotated equation of arc2 in y.
t1 = parametric value of arc1
t2 = parametric value of arc2
0 = xa'(t1) - xb'(t2)
0 = ya'(t1) - yb'(t2)
Each of these equation is just a order 2 polynomial. These are easy to solve.
To find the intersection points you solve the above equation (e.g. find the roots).
You'll get up to two roots for each axis. Any root that is equal on x and y is an intersection point between the curves.
Getting the position is easy now: Just plug the root into your parametric equation and you can directly get x and y.
Unfortunately, the general answer requires solution of a fourth-order polynomial. If we transform coordinates so one of the two parabolas is in the standard form y=x^2, then the second parabola satisfies (ax+by)^2+cx+dy+e==0. To find the intersection, solve both simultaneously. Substituting in y=x^2 we see that the result is a fourth-order polynomial: (ax+bx^2)^2+cx+dx^2+e==0. Nils solution therefore won't work (his mistake: each one is a 2nd order polynomial in each variable separately, but together they're not).
It's easy if you have a CAS at hand.
See the solution in Mathematica.
Choose one parabola and change coordinates so its equation becomes y(x)=a x^2 (Normal form).
The other parabola will have the general form:
A x^2 + B x y + CC y^2 + DD x + EE y + F == 0
where B^2-4 A C ==0 (so it's a parabola)
Let's solve a numeric case:
p = {a -> 1, A -> 1, B -> 2, CC -> 1, DD -> 1, EE -> -1, F -> 1};
p1 = {ToRules#N#Reduce[
(A x^2 + B x y + CC y^2 + DD x + EE y +F /. {y -> a x^2 } /. p) == 0, x]}
{{x -> -2.11769}, {x -> -0.641445},
{x -> 0.379567- 0.76948 I},
{x -> 0.379567+ 0.76948 I}}
Let's plot it:
Show[{
Plot[a x^2 /. p, {x, -10, 10}, PlotRange -> {{-10, 10}, {-5, 5}}],
ContourPlot[(A x^2 + B x y + CC y^2 + DD x + EE y + F /. p) ==
0, {x, -10, 10}, {y, -10, 10}],
Graphics[{
PointSize[Large], Pink, Point[{x, x^2} /. p /. p1[[1]]],
PointSize[Large], Pink, Point[{x, x^2} /. p /. p1[[2]]]
}]}]
The general solution involves calculating the roots of:
4 A F + 4 A DD x + (4 A^2 + 4 a A EE) x^2 + 4 a A B x^3 + a^2 B^2 x^4 == 0
Which is done easily in any CAS.

Resources