1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
local G = love.graphics
local function atanh(x)
return (1/2) * math.log((1+x)/(1-x))
end
local function solve_for_A(r)
local A
if r < 3 then
A = math.sqrt(6 * (r-1))
else
A = math.log(2*r) + math.log(math.log(2*r))
end
for i=1,5 do
A = A - (math.sinh(A) - r*A)/(math.cosh(A)-r)
end
return A
end
local function plot(f,x0,xn)
local n = 30
x0 = x0 or G.inverseTransformPoint(0,0)
xn = xn or G.inverseTransformPoint(G.getDimensions(),0)
if xn < x0 then
xn,x0 = x0,xn
end
local h = (xn-x0)/n
local ps = {}
for i=0,n do
local x = x0 + i*h
local y = f(x)
table.insert(ps,x)
table.insert(ps,y)
end
return ps
end
local function catenary0(x1,y1,x2,y2, L)
-- https://math.stackexchange.com/a/3557768
y1 = -y1
y2 = -y2
local dx = x2-x1
local dy = y2-y1
local mx = (x1+x2)/2
local my = (y1+y2)/2
local D = math.sqrt(dx^2+dy^2)
L = L or D+(1/(D+0.3))
local r = math.sqrt(L^2 - dy^2)/dx
local A = solve_for_A(r)
local a = dx/(2*A)
local b = mx - a*atanh(dy/L)
local c = my - L/(2*math.tanh(A))
return function(x) return -(a * math.cosh((x-b)/a) + c) end
end
local function catenary(x1,y1, x2,y2, L)
local D = math.sqrt((x2-x1)^2 + (y2-y1)^2)
-- L = L or D+(1/(D+0.3))
L = L or D+(0.1/(D+0.1))
-- L = D + 0.1
if x2 < x1 then
x1,x2 = x2,x1
y1,y2 = y2,y1
end
if math.abs(x1-x2) < 0.0001 then
x1 = x1 - 0.01
x2 = x2 + 0.01
end
local f = catenary0(x1,y1,x2,y2,L)
return plot(f, x1,x2)
end
return {
catenary = catenary
}
--[[
local Ps = {{-1,1}, {1,1}}
function love.update(dt)
for b = 1,2 do
if love.mouse.isDown(b) then
Ps[b] = {G.inverseTransformPoint(love.mouse.getPosition())}
end
end
end
function love.draw()
local W,H = G.getDimensions()
G.clear(1,1,1)
G.setColor(0,0,0)
G.origin()
local dx = Ps[1][1] - Ps[2][1]
local dy = Ps[1][2] - Ps[2][2]
local D = math.sqrt(dx^2+dy^2)
local L = D+(1/(D+0.3))
-- local L = D+1
G.print(("%.2f\n%.2f\n%.2f"):format(L,D,L-D),10,10)
G.setLineWidth(0.01)
G.translate(W/2,H/2)
G.scale(50)
G.setColor(0.8,0.8,0.8)
for i=-10,10 do
G.line(-10,i,10,i)
G.line(i,-10,i,10)
end
G.circle('line',0,0,1)
-- G.setColor(0,0,0)
-- -- plot(function(x) return x^2 end)
-- plot(math.tanh)
-- G.setColor(0,1,0)
-- plot(atanh)
G.setColor(0,0,0)
G.line(catenary(Ps[1][1],Ps[1][2], Ps[2][1], Ps[2][2]))
G.setColor(1,0,0)
G.circle('fill',Ps[1][1],Ps[1][2],0.05)
G.setColor(0,0,1)
G.circle('fill',Ps[2][1],Ps[2][2],0.05)
end
--]]
|