diff options
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/bezierplot/bezierplot.lua')
-rwxr-xr-x | Master/texmf-dist/tex/lualatex/bezierplot/bezierplot.lua | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/Master/texmf-dist/tex/lualatex/bezierplot/bezierplot.lua b/Master/texmf-dist/tex/lualatex/bezierplot/bezierplot.lua index d023923d455..d8d18441403 100755 --- a/Master/texmf-dist/tex/lualatex/bezierplot/bezierplot.lua +++ b/Master/texmf-dist/tex/lualatex/bezierplot/bezierplot.lua @@ -1,19 +1,24 @@ #!/usr/bin/env lua -- Linus Romer, published 2018 under LPPL Version 1.3c --- version 1.5 2024-03-31 +-- version 1.6 2024-11-02 abs = math.abs acos = math.acos asin = math.asin atan = math.atan cos = math.cos +cosh = math.cosh +deg = math.deg exp = math.exp e = math.exp(1) +huge = math.huge log = math.log pi = math.pi +rad = math.rad sin = math.sin +sinh = math.sinh sqrt = math.sqrt tan = math.tan -huge = math.huge +tanh = math.tanh -- just a helper for debugging: local function printdifftable(t) @@ -54,8 +59,8 @@ end local function round(num, decimals) local result = tonumber(string.format("%." .. (decimals or 0) .. "f", num)) - if abs(result) == 0 then - return 0 + if math.floor(result) == result then + return math.floor(result) else return result end @@ -275,7 +280,7 @@ local function do_parameters_fit(a,b,c,d,funcstring,funcgraph,maxerror,isinverse return true end --- f(x)=a*x^3+b*x+c +-- f(x)=a*x^3+b*x^2+c*x +d local function parameters_cubic(xp,yp,xq,yq,xr,yr,xs,ys) return (((xq-xp)*xr^2+(xp^2-xq^2)*xr+xp*xq^2-xp^2*xq)*ys+((xp-xq) *xs^2+(xq^2-xp^2)*xs-xp*xq^2+xp^2*xq)*yr+((xr-xp)*xs^2+(xp^2-xr^2) @@ -437,7 +442,7 @@ end -- and try to approximate it with a cubic bezier curve -- (round to rndx and rndy when printing) -- if maxerror <= 0, the function will not be recursive anymore -local function graphtobezierapprox(f,g,starti,endi,maxerror) +local function graphtobezierapprox(f,g,starti,endi,maxerror,recursiondepth) local px = g[starti][1] local py = g[starti][2] local dp = g[starti][3] @@ -496,7 +501,7 @@ local function graphtobezierapprox(f,g,starti,endi,maxerror) end end end - if maxerror > 0 then + if maxerror > 0 and recursiondepth > 0 then -- check if it is close enough: (recycling err, xa, ya) err = 0 for t = .1, .9, .1 do @@ -526,8 +531,8 @@ local function graphtobezierapprox(f,g,starti,endi,maxerror) interindex = i end end - local left = graphtobezierapprox(f,g,starti,interindex,maxerror) - local right = graphtobezierapprox(f,g,interindex,endi,maxerror) + local left = graphtobezierapprox(f,g,starti,interindex,maxerror,recursiondepth-1) + local right = graphtobezierapprox(f,g,interindex,endi,maxerror,recursiondepth-1) for i=1, #right do --now append the right to the left: left[#left+1] = right[i] end @@ -803,7 +808,12 @@ function bezierplot(functionstring,xminstring,xmaxstring,yminstring,ymaxstring,s else ---------- generic case (no special function) ---------------- if arbitrary_samples then - -- go through the connected parts + -- go through the connected parts... + -- due to numerical errors we have to use a maximal + -- recursion depth, which is hard wired here + -- (a small number should suffice since there are + -- no extrema nor inflection points inbetween) + local maxrecursiondepth = 2 for part = 1, #graphs do local dg = diffgraph(f,graphs[part],xstep) --printdifftable(dg) -- for debugging @@ -812,9 +822,9 @@ function bezierplot(functionstring,xminstring,xmaxstring,yminstring,ymaxstring,s for k = 2, #dg do if dg[k][5] or dg[k][6] then -- extrema and inflection points local tobeadded = graphtobezierapprox( - f,dg,startindex,k,10*yerror) + f,dg,startindex,k,10*yerror,maxrecursiondepth) -- tobeadded may contain a multiple of 6 entries - -- e.g. {1,2,3,4,5,6,7,8,9,10,11,12} + -- e.g. {1,2,3,4,5,6,7,8,9,10,11,12} for i = 1, math.floor(#tobeadded/6) do bezierpoints[#bezierpoints+1] = {} for j = 1, 6 do @@ -826,7 +836,7 @@ function bezierplot(functionstring,xminstring,xmaxstring,yminstring,ymaxstring,s end if startindex ~= #dg then -- if no special points inbetween local tobeadded = graphtobezierapprox(f,dg, - startindex,#dg,10*yerror) + startindex,#dg,10*yerror,maxrecursiondepth) -- tobeadded may contain a multiple of 6 entries -- e.g. {1,2,3,4,5,6,7,8,9,10,11,12} for i = 1, math.floor(#tobeadded/6) do |