BBC Basic, 300 ASCII characters, tokenised filesize 260
INPUTr,s,u,v,l:r*=8s*=8u*=8v*=8l*=8z=0REPEATz+=1E-3UNTILFNs(z)/z>=SQR(l^2-(v-s)^2)/(u-r)a=(u-r)/2/z
p=(r+u-a*LN((l+v-s)/(l-v+s)))/2q=(v+s-l*FNc(z)/FNs(z))/2MOVE800,0DRAW0,0DRAW0,800CIRCLEu,v,8CIRCLEr,s,8FORx=r TOu
DRAW x,a*FNc((x-p)/a)+q
NEXT
DEFFNs(t)=(EXP(t)-EXP(-t))/2
DEFFNc(t)=(EXP(t)+EXP(-t))/2
Emulator at http://www.bbcbasic.co.uk/bbcwin/bbcwin.html
This has obviously been solved before, so the first thing I did was look what others have done.
The equation of a catenary centred at the origin is simply y=a*cosh(x/a)
. It becomes slightly more complicated if it is not centred at the origin.
Various sources say that if the length and endpoints are known the value for a
must be determined numerically. There is an unspecified parameter h
in the wikipedia article. So I found another site and basically followed the method here: http://www.math.niu.edu/~rusin/known-math/99_incoming/catenary
BBC Basic does not have sinh
and cosh
built in, so I defined two functions at the end of the program to calculate them using EXP
coordinates for lefthand point must be supplied before righthand point, OP confirmed this is OK. Length is given last. Values can be separated by commas or newlines.
Ungolfed code
INPUT r,s,u,v,l
REM convert input in range 0-100 to graphic coordinates in range 0-800
r*=8 s*=8 u*=8 v*=8 l*=8
REM solve for z numerically
z=0
REPEAT
z+=1E-3
UNTIL FNs(z)/z>=SQR(l^2-(v-s)^2)/(u-r)
REM calculate the curve parameters
a=(u-r)/2/z
p=(r+u-a*LN((l+v-s)/(l-v+s)))/2
q=(v+s-l*FNc(z)/FNs(z))/2
REM draw axes, 800 graphics units long = 400 pixels long (2 graphics units per pixel)
MOVE 800,0
DRAW 0,0
DRAW 0,800
REM draw markers at end and beginning of curve (beginning last, so that cursor is in right place for next step)
CIRCLE u,v,8
CIRCLE r,s,8
REM draw curve from beginning to end
FORx=r TOu
DRAW x,a*FNc((x-p)/a)+q
NEXT
REM definitions of sinh and cosh
DEF FNs(t)=(EXP(t)-EXP(-t))/2
DEF FNc(t)=(EXP(t)+EXP(-t))/2
