
Originally Posted by
jemidiah
@loocas_duber: It's probably best for you to get very basic functionality working first, and then add on other stuff. In particular, you should get something that can just plot y = cosh(x) in 3D. After that you can make things fancier by allowing the user to specify a pair of points and draw the catenary through them. The above algebra and discussion gives the vast majority of the ideas needed for that, though.
Hey, jemidiah, thanks for jumping in!
So, I've managed, from a few various discussions on the internet as well as with the Wikipedia help, to write a function in my 3D app, that plots a catenary using point helper objects (simple crosses in the 3D viewport). The problem I'm having now is the accuracy of the curve. It seems there's something wrong.
First things first:
I define a Line Length (the catenary rope length), s, then I derive the horizontal distance from the two points (P1 and P2) the catenary should be hung on, d = P2.x - P1.x, assuming P2 is farther on the X axis than P1. Then, I define the vertical distance between the points, h = P2.z - P1.z (assuming the P2 is higher on the Z axis, than P1. Also note that my 3D software considers the Z axis as the "up" axis). Then I have to find the parameter a. I wrote a function that takes the a, d, s, h, epsilon (for error margin checking) and iterationSteps and computationally try to find the a, based on the Newton-Raphson method:
Code:
i = 0
tempResult = a
result = squareRoot(s^2 - h^2)
while i <= iterationSteps do
(
fx = 2 * tempResult * sinh(d/(2 * tempResult))
dFx = 2 * sinh * (d/(2 * tempResult)) - (d * cosh(d/(2 * tempResult))/tempResult) #basically just a derivative of fx
if abs(fx) - result <= epsilon do exit #this is a test to see if the end result of the calculation is close enough to the actual result of the function based on an error margin epsilon
tempResult -= fx/dfx
i += 1
)
This function should return an approximately very close value of a for the catenary function.
Now, for the actual calculation of the Catenary curve between ponts P1 and P2, which, as I read, has to be calculated with a specific offset in order to fit the curve between the points. Here's my approach:
Code:
x1 = (a * log((s + h)/(s-h)) - d)/2
for the actual X offset:
xOffset = x1 - P1.x
zOffset = P1.z - a * cosh(x1/a)
This is all I need in order to calculate the actual Catenary curve. All seems to be working fine. Here's the last piece of the puzzle, the actual plotting of points that lay on the Catenary curve:
Code:
for x = 0 to numberOfPoints do
(
pointXPosition = ((P2.x - P1.x)/numberOfPoints) * x
pointZPosition = a * (cosh(pointXPosition + xOffset)/a) + zOffset
)
So far so good. The function really does plot the catenary and for the test example I was drawing on, it works flawlessly. Here are the parameters for my text example: P1=(0,10), P2=(20,15) d=20, h=5, s=20.7, the equation 2a*sinh(d/2a) = sqrt(s^2-h^2) yields a value for parameter a = 61.9388. In the end, the curve looks just like the guy's from the forums I drew on:

But as soon as I change any of the input parameters (position of points P1 and P2 or the lenght of the catenary rope) the result is just wrong!
Here, the P2 was moved slightly up the Z axis and the length was changed to 30.7, this is what I got:

Here, the P2 was moved closer to P1 and the rope length was changed to 60.0, this is the result:

Is there anything I've overlooked? Or what could be the cause for this errornous behavior?
Thanks a lot in advance, any help will be much appretiated!