views:

92

answers:

3

I am writing a custom animation for wpf and as a non math guy I have a couple questions...

If I am given two Point3D's, the From and To, and assuming the origin is at 0,0,0 how do I calculate a curve between the two points?

And once I have the curve 'plotted' and I know its length (how to do that too?) how can I calculate the x,y,z coords at some given distance along the line?

Thanks!

+4  A: 

To get a straight line vector from point A to point B:

B - A

which would translate to:

vector.x = b.x - a.x;
vector.y = b.y - a.y;
vector.z = b.z - a.z;

The length is:

length = Math.Sqrt(vector.x * vector.x +
                   vector.y * vector.y +
                   vector.z * vector.z);

To get a point a certain distance along the vector you need to make the vector a unit vector (length 1):

 vector.x = vector.x / length;
 ...

and then multiply by your distance:

 vector.x = distance * vector.x;
 ...

This is all from memory so might not compile straight away.

There's A Vector Type for C# on CodeProject which will do a lot of this for you.

If you want a curve, then you'll need:

a) to define what type of curve you want (arc, spline, etc.)

b) more points (centres, control points etc.)

ChrisF
You can take a look at my comment above, but truthfully, I dont know what kind of curve I want- all I know is that these two points must exist on a surface that surrounds my origin- basically a sphere or ellipsoid are my only two choices....
Nicros
A: 

You'll probably want to express your curve as a set of parametric functions of some other variable:

x = f(t)
y = g(t)
z = h(t)

where 0 <= t <= 1, and

f(0) = from.x, f(1) = to.x
g(0) = from.y, g(1) = to.y
h(0) = from.z, h(1) = to.z

There are an infinite number of curves connecting any two points, so you'll need more information to decide what form f(t), g(t), and h(t) should take. To move a point along the curve, you just let t vary between 0 and 1 and calculate the x, y, and z coordinates. One approach is to define a set of control points that you'd like your curve to pass through (or near), then express your parametric equations in terms of spline functions. You won't need to know the arc length of the curve in order to do this.

Jim Lewis
Thanks for the reply! I posted a couple of comments above, but basically, Im thinking my two points are on a closed surface- a sphere or an ellipsoid. So given the origin and these two points I would need to get the xyz position at a given time (or distance along the line).
Nicros
A: 

So I just wanted to follow up with my solution- while it is true there are an infinite number of curves- my (poorly worded) question was how to plot between two points on a curve- the shortest distance, assuming an origin of 0,0,0 and two 3d points. What I did was to convert my points from cartesian to polar, calculate the spherical point at a given time and then convert that point back to cartesians. If anyone wants me to post the actual C# code let me know.

Nicros