Curve distance equation

Blitz3D Forums/Blitz3D Programming/Curve distance equation

Ross C(Posted 2004) [#1]
Hi, i'm wondering if any of you smart folks have an equation to work out the distance of a curve, from point A to point B with angle A.


Ross C(Posted 2004) [#2]
The angle is the difference basically between the way point A is facing and the way point B is facing.


Stevie G(Posted 2004) [#3]
Ross,

I think it's quite difficult to determine the length of the curve - it really depend on how accurate you want it.

Just a thought but by using a 4 pt bezier you can set the direction vectors of points 2 & 3 and simply add the distances between each interpolated point. Probably quite accurate if you use a small timestep.

.. then again, I'm sure there is a quicker and better approximation but in order to determine the length you need to know the curve equation and that's where the bezier stuff somes in.

Stevie


aab(Posted 2004) [#4]
its Interesting how after all the Volumes of 3 dimensional Spline Revolution, Integration to find area under graph and um..other stuff
That isnt covered in high school Maths.
There must be some way to work it out exactly..
All i can think of is in equation terms, where if i was to approximate the length of a curved graph (between closed intervals) i would calculate the gradient(eg in 2d:(yincrease/xincrease)) for each x co-ordinate (ie 0,1,2) (==>XINCREASE WOULD BE 1) store the length (pythagoras OR ==>ratios i prefer) in a type or array or bank and then add them up at the end through a loop for each index.

but then u need to find the actualpoint at which the spline is at for each co-ordinate...

scene as its made up of vertex and line combinations, i suppose in the end i would just find the distance between each vertex that is connected to every other vertex and combine those values (I'm sure that could be done in Blitz=although you'll be using your own distance formula)
The Vertex relatred commands that ivenever really looked at:
http://www.blitzbasic.com/b3ddocs/command_list_3d_cat.php?show=Surface
so just countvertices, do a loop for them storing the x,y and z into an array, then use the distance formula from school to find and add the differnce between each related vertex in the spline.
Wouldnt be curve orientated mind me...

ALTERNATIVELY
edit: if theres a path following algorithm out there, you could use that, move an entity along the spline at a constant vectoral speed and then find then time through the millisecs. and then multiply to find the distance in co-ord's/Millisec


_PJ_(Posted 2004) [#5]
I think you differentiate the curve equation. Or integrate... cant remember now :*(


Damien Sturdy(Posted 2004) [#6]
1970
i just thought id waste 2 mins by saying this:
im crap at maths... and i couldnt do this.. but at the age of 12 i made a worms game for my BBC and accidentally created a math formula that worked.....

[/time wating]


BODYPRINT(Posted 2004) [#7]
Well, circumference = 2*pi*r
I assume we know the radius?

The decimal of the circumference that we need is A/360

So we calculate the total circumference and multiply it by A/360

therefore

Distance=(2*pi*r)*(A/360)
where r=radius and A=angle.

Correct me if I am wrong.


Damien Sturdy(Posted 2004) [#8]
sbout what i came up with :/


_PJ_(Posted 2004) [#9]
That would work, Phil, if the curve was cirumferencial (i.e. an Arc of a circle)

Do you have an equation for the curve???


aab(Posted 2004) [#10]
There should eb an equation to calculate an ovals circumference as well mind you. I'd assume it was the average between the min and max lengths. Then you could use this between each set of points, assuming you knew ow to calculate the ovals height which wont be vertex joined


Ross C(Posted 2004) [#11]
Thanks for your suggestions :o) I'll think over them and post if a get an answer :o) Thanks again!


Matty(Posted 2004) [#12]
Two ways of finding the length of a curve:

1.If the curve is represented by a function of form

y=f(x)

Perform the following operation:

(I will use words as I cannot enter symbols in here)

Integrate between Xfinal and Xinitial the following
function with respect to x:

(Square Root of (1 + f'(x)^2))dx



2.If the curve is represented by a parametric equation
like y=y(s), x=x(s)

Integrate between sfinal and sinitial the following function
with respect to s:

(Square Root of (y'(s)^2 + x'(s)^2))ds


Performing that operation will give you the length of
a line.


BODYPRINT(Posted 2004) [#13]
Distance=(2*pi*r)*(A/360)
where r=radius and A=angle.


radius could be calculated as distance between the 2 points divided by 2

angle would be 180 because we are taking the radius from the centre of the distance between the 2 points


BlackD(Posted 2004) [#14]
Type SIN in BlitzIDE and hit F1. Copy and paste the example and run it. Left/right arrow keys to control. If you want to work out angles between points, you're going to have to go back to school maths - sine, cosine, and tangent. I know it was a long time ago, but maybe this will help refresh your memory. :)


Ross C(Posted 2004) [#15]
I can do the angles between points, is just getting the distance of a curved line. I can then pull the the x and y cords based on how far i've travelled along the curve :) And i can interpolate the angle based on this distance too :o)


_PJ_(Posted 2004) [#16]
Ah yeah, thanks, Matty -

Integrate to find the length.

Am I right in remembering Integration is raising the 'power' by 1 then dividing by this new 'power'?


Matty(Posted 2004) [#17]
Using the information you have supplied above I think you are saying you have this situation:

You know the coordinates of both point A, point B and the angle that the curve makes with the horizontal at point A (in other words the gradient at A).

That is 3 pieces of information. If you are using a cubic spline you need one more piece of information, the angle the curve makes at point B as well. But you have not specified that so I assume you have only 3 pieces of information which means we can use a quadratic rather than a cubic spline.

So we set up the following:

y = y (x) - ie our y coordinate is some function of x.

we know the following:

Coordinates of A we shall write as (0,A) - ie x=0, y=A
Coordinates of B we shall write as (L,B) - ie X=L, y=B
Angle curve makes at A is Theta => Gradient at this point = Tan(Theta).

So:

Y(0)=A
Y(L)=B
Y'(0)=Tan(Theta) - which we will just call "C"
so Y'(0)=C

then, since we have 3 points and are using a quadratic to fit the curve we do this:

Y(x) = ax^2+bx+c (a,b,c not the same as A,B,C)

Y(0)=A = a*0^2 + b*0 + c, therefore c=A
Y(L)=B = a*L^2 + b*L + A
Y'(0) = C = b, therefore b=C

then solving for "a" we get this:

a = (B-CL-A)/(L^2)

Okay so our equation for your curve that passes through the points and with the initial angle as specified is:

Y(x)=((B-CL-A)/(L^2))*x^2 + Cx + A

Now use this equation for Y and calculate the length of the line using the method I outlined above.

I will write it anyway:

Okay - you now want to find the length of the curve I specified:

Length = Integrate Between X=0 and X=L the following function:

(Square Root of (1 + Y'(X)^2)) dX

You could do this analytically but it would probably be just as easy and not too inaccurate to do so with a numerical technique ranging from something simple like the midpoint method to something more complex like the Runge Kutta method


Matty(Posted 2004) [#18]
@Malice - yes you integrate, but you don't integrate the equation for the curve itself - that would just give the area, you have to create a new function as I outlined above and integrate that one. Basically the integration of this new function splits the curve into tiny segments and by using the slope of the curve at each point over a small distance as the hypotenuse of many tiny right angled triangles it works out the total distance of the curve.