7

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!

Nicros
  • 5,031
  • 12
  • 57
  • 101
  • 2
    You might want to throw this up on http://mathoverflow.net/ and then come back here for implementation advice. It is a good question..when you get the solution; file it away...it will come up again in a few years and like me you will have forgotten the math. Now...what did I file that under ?..curve ?...graphics ?....crap. – Rusty May 21 '10 at 19:02
  • 1
    When you say you want a "curve", it sounds like you don't just want a straight-line between your two points but you want a curvy line that connects your endpoints and some other points in between. Maybe look into Splines and NURBS as a start. – FrustratedWithFormsDesigner May 21 '10 at 19:07
  • You should really specify what type of curve you want: there is an infinite number of curves passing through 2 points. EDIT: if you want a spline you need at least 3 points. – nico May 21 '10 at 19:09
  • 2
    @Rusty: mathoverflow.net is intended for professional mathematicians and advanced students; this question would likely be considered off-topic over there because it's too elementary. – Jim Lewis May 21 '10 at 19:19
  • @Jim: thanks for the info....I should have known....math snobs. – Rusty May 21 '10 at 19:38
  • 1
    Wpf has a Bezier curve construct. Bezier curves are fairly easy for the lay person to understand and are used extensively as the curve of choice for animation in many graphics applications. As such you will find a wealth of information and code examples via a Google search. Have a look at: http://stackoverflow.com/questions/2656166/calculating-parameters-for-defining-subsections-of-quadratic-bezier-curves. – Rusty May 21 '10 at 19:50
  • Sweet! I will ask this on mathoverflow.net as well with some additional info- and take the flames if I must. The concept I had was that these two points existed on some surface surrounding my origin. If the points were 0,0,5 and 5,0,0 I would know this is a sphere and my line must follow the path between these two points on a sphere. But the 2 points could really be anywhere. – Nicros May 21 '10 at 21:02
  • @Rusty It has nothing to do with snobbery. MO is professional mathematicians trying to get on with their job and there are other web sites for questions like this. – sigfpe May 21 '10 at 22:55
  • @user207442: I'm totally hip....its just that I was raised by engineers and always told to be leery of mathematicians as possibly not human, but a necessary evil.. hehe :) – Rusty May 21 '10 at 23:14
  • @Nicros: As I said before, there are INFINITE spheres that connect **two** points in space. See the picture here for instance: http://en.wikipedia.org/wiki/Family_of_curves You need a third point if you want to define *ONE* curve in space – nico May 22 '10 at 08:54

3 Answers3

5

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
  • 134,786
  • 31
  • 255
  • 325
  • 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 May 21 '10 at 21:04
0

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
  • 43,505
  • 7
  • 82
  • 96
  • 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 May 21 '10 at 21:09
0

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
  • 5,031
  • 12
  • 57
  • 101