开发者

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?

A开发者_Python百科nd 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!


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜