# Solve for an Ellipse Tangent to 2 Lines [duplicate]

I'm trying to automate creation of a curve in PowerPoint.
Here's an image of what I'm working towards:

I'm trying to show a diagram of a rocket trajectory from a launch site on Earth to a circular orbit. The math that I can't figure out is how to solve for an ellipse that connects the launch point (x1, y1) to the orbit insertion point (x2, y2) and is tangent to a vertical line at the launch point and tangent to the circular orbit. My approach has been to use (x-h)^2/a^2 + y^2 / b^2 = 1 because while I can set the horizontal axis of the ellipse at y=0, I don't know where the x coordinate of the center might fall, so I have an x-offset, h. I found an equation for the slope of a tangent line to an ellipse at a point as m = a^2*x/(b^2*y). I'm not sure if this is the right equation because sometimes the desired ellipse is longer horizontally and sometimes it might be longer vertically. If my insertion point is 45 degrees along the 1st quadrant of the orbit, then the tangent line has a slope of m = -1. Substituting the coordinates of the insertion point in to the slope equation, I can establish a relationship between a and b. Then I solved the ellipse equation for h as a function of x, y, and a (substituting for b with the slope relationship). Then I got 2 "h" equations by substituting the launch point, (x1, y1) and the insertion point, (x2, y2). Then I iterated to find an "a" value that minimized the difference between the 2 "h" equations. This successfully generated an ellipse that intersected the two points, but I think the slope at the insertion point is wrong (doesn't match the orbit), and it only appears to work for ellipses that are longer horizontally.

I apologize for this terribly formatted question. I've spent all day at work trying to figure this out and I'm stumped. Many of the solutions to similar problems online a just out of reach for me to understand how to apply it to my situation.

Thanks for any help anyone might can provide.

 Using a modification of alex.jordan's method I solved my problem. Here's my work: https://www.dropbox.com/s/61mmyuz4hmh2ry9/ellipses.pdf

And here's some Matlab code showing an application of the method and plotting a demonstration of the solution:

figure('Name','Ellipse Tangent to Orbit', ...
'Color',[1,1,1],'NumberTitle','off', ...
'Units','Normalized', 'Position',[0.1,0.1,0.8,0.8], ...
'Visible','on');
axes;

hold on;

earthX = 0;
earthY = 0;

plot(earthX,earthY,'bo');

r = 700;

theta = 45 * pi/180;

%Insertion Point
s = earthX + r * cos(theta);
t = earthY + r * sin(theta);

%orbit
theta_mat = linspace(0,pi/2,100);
x_orbit = earthX + r*cos(theta_mat);
y_orbit = earthY + r*sin(theta_mat);

plot(x_orbit, y_orbit,'b--')

%launch point
x0 = 50;
y0 = 100;

plot(x0,y0,'ro');

plot(s,t,'ro');

%Slope at tangency point
m = -s/t;

%Vertical offset of ellipse equals coordinate of launch point
k = y0;

%b polynomial

% b^2 term
a0 = 2*t^3*x0*s^3*(t-k)^3-2*t^3*s^4*(t-k)^3-t^4*s^2*(t-k)^4;
% b term
a1 = 0;
% constant term
a2 = t^2*x0^2*s^4*(t-k)^4-2*t^2*x0*s^5*(t-k)^4-2*t^3*x0*s^3*(t-k)^5 + ...
t^2*s^6*(t-k)^4+2*t^3*s^4*(t-k)^5+t^4*s^2*(t-k)^6;

%b = max(roots([a0,a1,a2]));

%Solving for b (reduced quadratic formula)
b = abs(sqrt(-a0*a2)/a0);

%Solve for a
a = sqrt((b^4*t^2-b^2*t^2*(t-k)^2)/(s^2*(t-k)^2));

%Solve for h
h = s-a^2*s*(t-k)/(b^2*t);

% X values for ellipse
x = linspace(h-a,h+a,100);

%y values for ellipse
y = b/a*sqrt(a^2-(x-h).^2)+k;

plot(x,y,'b-')

axis square


Finally here's the plot:

https://www.dropbox.com/s/71t4tuw3bqv0xr1/orbits_verified.png

## marked as duplicate by J. M. is a poor mathematician, Amzoti, Zev Chonoles, Ittay Weiss, Alex BeckerMay 24 '13 at 6:31

• I think this is a duplicate of this previous question. There are infinitely many ellipses through two points with prescribed tangents, as described in my answer. – Rahul May 16 '13 at 23:29
• If you really want it to be an orbit, the ellipse and the circle should have a common focus, which doesn't seem to be a condition in the previous question. On the other hand, it also doesn't seem to be the case in your example picture... – Micah May 17 '13 at 2:11
• Micah - yeah you're right - if this was a true orbital mechanics trajectory then there's a whole different set of rules to follow. Ironically, obtaining the actual trajectory for a launch vehicle to an orbit is the "easy" part for me because we have 3-DOF and 6-DOF trajectory codes for that purpose. What I want to create here is an automated notional diagram of a launch trajectory in which the launch site, number of stages (indicated with different colors along the trajectory), and orbit altitude can be changed. I'm tired of nudging shapes around in PowerPoint. – flutefreak7 May 17 '13 at 15:41

There is an equation for the circle $$x^2+y^2=1\tag{1}$$ (and if you desire a bigger radius, scale everything up once this is finished.) and at any point along it, $$2x+2yy'=0\tag{2}$$ Then there is an equation for the ellipse $$\frac{(x-h)^2}{a^2}+\frac{y^2}{b^2}=1\tag{3}$$ and at every point along it $$\frac{2(x-h)}{a^2}+\frac{2yy'}{b^2}=0\tag{4}$$

Your aim is to find $a,b$, and $h$ that provide the desired tangency. Let's call the point of tangency $(s,t)$. Then you have a system of three equations: \begin{align} s^2+t^2&=1\tag{1b}\\ a^2s&=b^2(s-h)\tag{2b}\\ \frac{(s-h)^2}{a^2}+\frac{t^2}{b^2}&=1\tag{3b} \end{align} where (2b) equation has come from eliminating $yy'$ in equations (2) and (4). This is five unknowns in three equations, so the system is likely very underdetermined. You can throw in a fourth equation by specifying the coordinates of the launch point $$\frac{(x_0-h)^2}{a^2}=1\tag{4b}$$ but it makes sense that there will still remain one degree of freedom, when you think about elongating the ellipse vertically while contracting it horizontally and maintaining the tangency. So your task is to solve the system \begin{align} s^2+t^2&=1\tag{A}\\ a^2s&=b^2(s-h)\tag{B}\\ \frac{(s-h)^2}{a^2}+\frac{t^2}{b^2}&=1\tag{C}\\ \frac{(x_0-h)^2}{a^2}&=1\tag{D} \end{align}

I would use (B) to isolate $h$, and then substitute that into (C) and (D) and scale by $b^4$, resp $a^2b^4$: \begin{align} s^2+t^2&=1\tag{A}\\ a^2s^2+b^2{t^2}&=b^4\tag{C'}\\ (b^2x_0+a^2s-b^2s)^2&=a^2b^4\tag{D'} \end{align} (A) can be used to isolate $t^2$ and insert into (C'): \begin{align} a^2s^2+b^2-b^2s^2&=b^4\tag{C''}\\ (b^2x_0+a^2s-b^2s)^2&=a^2b^4\tag{D'} \end{align} $a^2$ can be isolated in (C'') and substituted into (D'), after which we rescale to keep denominators away: \begin{align} (sx_0-1+b^2)^2&=-b^2+b^2s^2+b^4\tag{D''} \end{align} This appears quartic in $b$, but once expanded, the $b^4$ terms cancel: \begin{align} (sx_0-1)^2+2sx_0b^2&=b^2(1+s^2)\tag{D'''} \end{align}

and you should be free to choose any $b$, solve for $s$ with the quadratic formula and work your way back to get the other values, or vice versa. You have specified an insertion point at 45 degrees, so that would make $s=\frac{1}{\sqrt{2}}$. If we put this in equation (D''') and solve for $b^2$ we have \begin{align} b^2&=\frac{(x_0-\sqrt{2})^2}{3-2\sqrt{2}x_0}\tag{E} \end{align}

• Thanks for this - it has really helped. I realized that since my ellipse is not centered with the circle, it needs both and x and y offset, so (x-h)^2/a^2 + (y-k)^2/b^2 = 1. I also changed the circle equation to x^2+y^2=r^2 to account for a varying orbit. An additional relation helps mitigate the "k" term because k = y0 since the launch point is on the ellipse's horizontal axis. This changed the process of the calculations though. Instead of eliminating yy' between (2) and (4), I isolated and eliminated y' -- makes sense because the slope is the same for circle and ellipse at tangent pt. – flutefreak7 May 17 '13 at 20:55
• When it came time to substitute t^2 from A into C', it made more sense to substitute s^2, since there are a number of (t-k)^2 terms in C'. However, because I incorporated r^2 instead of 1, all this does is complicated the math from there out because (r^2-t^2) is more convoluted than just s^2. In fact, I had to substitute s^2 back in later anyway to get the b^4 terms to cancel. Isolating s or t using equation A doesn't really matter since I can plug them both in as known or assumed values to solve for b. I'm still working on substituting real values, solving, and plotting it to verify. – flutefreak7 May 17 '13 at 21:02
• Hah! It worked! dropbox.com/s/71t4tuw3bqv0xr1/orbits_verified.png – flutefreak7 May 17 '13 at 21:53
• Great! So, just to be clear, you could compute everything with $r=1$: you'd find values for all of the coordinates and for $a$ and $b$. Then if you wanted to make the circle larger by a factor of $r$, you could just make everything larger by a factor of $r$. This is a common trick to simplify calculations, and it is applicable in this case. – alex.jordan May 18 '13 at 2:19
• @flutefreak7 Also, you might be interested in GeoGebra where you can make slider bars for some of your variables, have circles and ellipses that depend on those things and then manipulate the slider bars. Just for starters, you could make a slider bar called "r" and enter "x^2+y^2=r^2" and then manipulate the bar to grow or shrink the circle. – alex.jordan May 18 '13 at 2:22