Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate point along a curve?

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!

like image 315
Nicros Avatar asked May 21 '10 18:05

Nicros


1 Answers

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.)

like image 134
ChrisF Avatar answered Sep 30 '22 06:09

ChrisF