# Plot ellipse in cartesian coordinates

I am working with a computer program that needs to draw objects in a 3d field. Over time, these objects will move (at different rates) on an elliptical path around the 0,0,0 point.

I've read the article http://en.wikipedia.org/wiki/Ecliptic_coordinate_system, and that seems to be exactly what I want to do. But, I'm afraid that boiling it down to a simple form and translating this into step-by-step math equations that I understand enough to express in computer code is beyond me.

So I ask, can anyone relate this in terms of procedural mathematics such as one has access to in a programming language (like PHP or javascript): given the x,y,z position of an object (and any other variables needed for the equations like "speed"), how do I calculate it's new x,y,z after T (time)?

EDIT

A drawing to (hopefully) illustrate my intention. I am looking for the method of calculating the x,y,z of point H when point G is known. The box in figure 1 is a cube divided into a grid, giving me the x,y,z points. The direction one views it from should not affect the path that the ellipse takes, merely its visual appearance. I am concerned only with how to arrive at the x,y,z of H if I know G (and if I know any other aspects that would play a role in crafting the requisite equation[s])

-
That wiki article's for something else entirely. Anyway, the ellipse is planar, so presumably you can rotate things such that the ellipse is on the $x-y$ plane and use $(a\cos\,t,b\sin\,t,0)$... –  Ｊ. Ｍ. Sep 16 '11 at 18:20
The ellipse is drawn in a 3d field, so while I am yes drawing a planar ellipse, the plane can be tilted within the 3d field which would affect the x,y,z position. Also, I don't know what to do with (acost,bsint,0) when I have x,y,z - I need it explained in non-mathematician language, I need the process :) –  Chris Sep 16 '11 at 18:23
Right, so you need to rotate the plane. How would you specify the tilt of your ellipses? –  Ｊ. Ｍ. Sep 16 '11 at 18:27
In whatever manner is necessary to get the outcome - I have a database that can track whatever variables related to each object that are necessary to calculate it's position in the 3d field. –  Chris Sep 16 '11 at 18:34
Think of it this way: something tilted would have an angle of some sort about the horizontal or some other plane. Usually one requires three angles for the purpose... where do you think might those angles come from? –  Ｊ. Ｍ. Sep 16 '11 at 18:37

One good approach is to do this:

1. use 2d coordinates (x,y) to describe your ellipse, for example in case of circle you would use (x,y)=(r*cos t, r*sin t)
2. then define a plane using 2 vectors like v1=(x1,y1,z1) and v2=(x2,y2,z2) and then center of the plane as point p1 = (x3,y3,z3)
3. then convert from 2d coordinates to 3d coordinates using the following: x*v1+y*v2+p1, you need to use scalar and vector multiplication x*(a,b,c) = (x*a,x*b,x*c) and vector-vector addition ($x_1$,$y_1$,$z_1$)+($x_2$,$y_2$,$z_2$) = ($x_1+x_2,y_1+y_2,z_1+z_2$).
-

You say that you need to keep track of the variables x,y,z (position in space) in terms of t (time), and you're saying about other parameters like speed, so it reminds me about parametric equations. These equations make a function of t for x,y,z, that is:

x(t) = some formula with t
y(t) = some formula with t
z(t) = some formula with t


which is best expressed as a vector function: the coordinates in space are pointed by a vector from the origin to the point (x,y,z) and this vector changes as t changes (it's a function of time): r(t).

Now you need to find these formulas connecting coordinates with t, which might not be obvious at first. But it could be simplified by using the notion of velocity vector, v. This vector will be always tangent to the path your point r follow in space. So it's a matter of updating the position vector r(t) by adding to it the v vector to find a new position r(t+dt):

r(t+dt) = r(t) + v(t)


You only need to make the (time) step dt sufficiently small to get more accuracy. This way allows you to track any curves in space: not only ellipses, but also lines, spirals, or anything else.

OK, but you want ellipses, right?

So now your problem moved to finding the velocity vector at each moment of time (t). But this problem has already been solved in history, by Johannes Kepler and Isaac Newton, for elliptical orbits of planets in a central gravity field. If you know a bit of physics, you can use these facts to derive proper equations for velocity from acceleration in central gravity field, which is related to distance from one of the ellipse's centers.

But if you don't want to get into details of physics, you can also use the fact that all ellipses lie in a plane, and no parts of it stick out from that plane. So you can get a formula for the ellipse in 2D planar coordinates (polar or rectangular, whichever you like more) and transform them into 3D by rotating around proper angles.

Usually this transformation can be made with matrix multiplication: you get a coordinate in 2D as a vector, extend it with zeros for other coordinates, and multiply it by a matrice which describes rotation transformation, and you'll get another vector, in a rotated coordinate system. The only thing you need is to prepare the matrix (once is enough if the plane of your ellipse doesn't change). Such transformation matrices have standard forms, which you can find over the Net (search phrase: "rotation matrix", for example). You simply insert the sines & cosines of rotation angles in the proper places and lo! a matrix could be used for transforming coordinates readily. Usually you'll find matrices for rotating around separate axes of coordinate system, X, Y, Z. But you can join these transformations together by multiplying these matrices together. You can also multiply them with the translation matrix, which can move the center of the ellipse to some other place. This is how it's usually made in 3D computer games or vector graphics/modelling.

But there's also another way of doing rotations in space, which is by use of quaternions. It needs less coordinates and factors to keep track of, but it's a bit harder to understand if you've never have any experience with them before. But it has an advantage of avoiding the problem of so called "gimbal lock", which usually makes problems with typical coordinate matrices using those three Euler angles.

-
Ah, and you've probably noticed already that every ellipse seen from the side is just a line, on which a pont moves sinusoidally (with a proper phase offset and frequency). It has been described by Lissajous (search phrase: Lissajous figures). So you can make any circular/elliptical/spiral motion by joining sines&cosines with linear motions in a proper way. You only need to find a proper factors, but these could be found by projecting axial vectors onto coordinate planes XY, YZ, ZX. –  SasQ Sep 16 '11 at 20:08