Advertisement
rerere284

graphObjects.lua

Jan 14th, 2021
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. local ret = {}
  2.  
  3. --[[
  4.     graph.rangeX = {0,1}
  5.     graph.rangeY = {0,1}
  6.     graph.graphtiles = {0, 0.5, 1}
  7. ]]
  8.  
  9. ret.new = function (self, ...)
  10.     --[[ graphObjects:new(
  11.         0,1,
  12.         1,100,
  13.         "........##",
  14.         ".....###..",
  15.         "..###.....",
  16.         "##........")
  17.        
  18.         ".#",
  19.         "#.")
  20.     graphObjects:new("......oo",
  21.                      "....oo..",
  22.                      "..oo....",
  23.                      "oo......",
  24.                      0,1,
  25.                      1,4}
  26.     obj.graphTiles = {1,1,2,2,3,3,4,4}
  27.     ]]
  28.     local args = {...}
  29.     local obj = {}
  30.     obj.rangeX = {0, 1}
  31.     obj.rangeY = {0, 1}
  32.     obj.graphtiles = {}
  33.     obj.loops = false
  34.    
  35.     local numberArgs = 0
  36.     local stringArgList = {}
  37.     for i=1,#args do
  38.         if type(args[i]) == "number" then
  39.             if numberArgs < 2 then
  40.                 obj.rangeX[numberArgs + 1] = args[i]
  41.             elseif numberArgs < 4 then
  42.                 obj.rangeY[numberArgs + 1] = args[i]
  43.             end
  44.             numberArgs = numberArgs + 1
  45.         elseif type(args[i]) == "string" then
  46.             table.insert(stringArgList, i)
  47.         end
  48.     end
  49.    
  50.     --go through each collum of the graph top to bottom
  51.     if #stringArgList > 1 then
  52.         local graphX = string.length(args[stringArgList[1]])
  53.         local graphY = #stringArgList
  54.         for i=1,graphX do
  55.             for j=1,graphY do
  56.                 --if you find a char that is not "."
  57.                 --this collumn is done, break out of this loop
  58.                 --to continue the next collumn
  59.                 local graphchar = args[stringArgList[j]][i]
  60.                 if graphchar ~= "." then
  61.                     --((value-min) / magnitude) * magnitudeB + minB
  62.                     --((value-min) * magnitudeB) / magnitude + minB
  63.                     --((value-min) * (maxB-minB)) / (max-min) + minB
  64.                     --  -(j-1/gY-1) *magnitude +min
  65.                     --  ((graphY-1)-(j-1))*(magnitude)/(gY-1)+min
  66.                     obj.graphTiles[i] = ((graphY-1)-(j-1))*(obj.rangeY[2]-obj.rangeY[1])/(gY-1)+obj.rangeY[1]
  67.                     break
  68.                 end
  69.             end
  70.             --if it finds nothing in a collumn, whatever
  71.         end
  72.     else
  73.         --graph not included
  74.         obj.graphtiles[1] = obj.rangeY[1]
  75.         obj.graphtiles[2] = obj.rangeY[2]
  76.     end
  77.    
  78.     --create the metatable if it doesn't exist
  79.     if not self.graphMT then
  80.         self.graphMT = {}
  81.         --metatable contains one value
  82.         --graphs are used by doing graph(value)
  83.         self.graphMT.__call = function(graph, value)
  84.             local tempM = graph.rangeX[2]-graph.rangeX[1]
  85.             local tileCount = #graph.graphTiles
  86.             local tileWidth = (tileCount-1)/tempM
  87.             local tileValue = ((value-graph.rangeX[1])*(tileCount-1)/(tempM)+(1)) % 1
  88.             if graph.loops then
  89.                 local modvalue = tempM+tileWidth
  90.                 --realvalue range: 0 to one tileWidth above (max-min)
  91.                 local realValue = (value-graph.rangeX[1]) % modvalue
  92.                 --leftTile: tile left of realvalue
  93.                 --can be 1 to #graphTiles
  94.                 local leftTile = math.floor((realValue)*(tileCount-1)/(tempM)+(1))
  95.                 if leftTile == tileCount then
  96.                     --if the tile left of value is the final tile, other tile is 1
  97.                     return ((1-tileValue)*(graph.graphTiles[leftTile]))+((tileValue)*(graph.graphTiles[1]))
  98.                 else
  99.                     return ((1-tileValue)*(graph.graphTiles[leftTile]))+((tileValue)*(graph.graphTiles[leftTile+1]))
  100.                 end
  101.             else
  102.                 local leftTile = math.floor((value-graph.rangeX[1])*(tileCount-1)/(tempM)+(1))
  103.                 if value < graph.rangeX[1] then
  104.                     --[[get t value between first two graph tiles
  105.                     --from a value from outside of that range
  106.                     --map graph range to 0 to (last tile - 1)
  107.                     --just change new minimum from 1 to 0
  108.                     --1 2 3...
  109.                     --0 1 2...
  110.                    
  111.                     value is currently [min-tileWidth, min]
  112.                     want it to be [0, 1]
  113.                     ]]
  114.                     --local firstTileValue = (value-graph.rangeX[1])*(tileCount-1)/(tempM)+(0)
  115.                     local firstTileValue = (value-(graph.rangeX[1]-tileWidth))*(1)/(tileWidth)+(0)
  116.                     --lerp with first two graph tiles
  117.                     return ((1-firstTileValue)*(graph.graphTiles[1]))+((firstTileValue)*(graph.graphTiles[2]))
  118.                 elseif value < graph.rangeX[2] then
  119.                     return ((1-tileValue)*(graph.graphTiles[leftTile]))+((tileValue)*(graph.graphTiles[leftTile+1]))
  120.                 else
  121.                     --[[get t value between last two graph tiles
  122.                     --...5 6 7 8 9
  123.                     --..-3-2-1 0 1
  124.                     --subtract 8 from min
  125.                     --subtract (max-1) from min(1)
  126.                    
  127.                     --graph range 2-4
  128.                     --0 1[2 3 4]5 6 7
  129.                     --value is at ]
  130.                     --3 tiles
  131.                     --0[1 2 3]4 5 6
  132.                     --desired outcome
  133.                     --graph      0 1[2 3 4]5 6 7
  134.                     --tiles      0[1 2 3]4 5 6
  135.                     --tiles -2-1 0]1 2 3 4 5 6
  136.                     --subtract by 3, aka tileCount
  137.                    
  138.                     value is greater than max
  139.                     [max, max+tileWidth]
  140.                     want [tileCount, tileCount+1]
  141.                     ]]
  142.                     --local lastTileValue = (value-graph.rangeX[1])*(tileCount-1)/(tempM)+(-tileCount)
  143.                     local lastTileValue = (value-graph.rangeX[2])*(1)/(tileWidth)+(tileCount)
  144.                     --lerp with last two graph tiles
  145.                     return ((1-lastTileValue)*(graph.graphTiles[tileCount-1]))+((lastTileValue)*(graph.graphTiles[tileCount]))
  146.                 end
  147.             end
  148.         end
  149.     end
  150.     setmetatable(obj, self.graphMT)
  151.    
  152.     return obj
  153. end
  154.  
  155. return ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement