Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ret = {}
- --[[
- graph.rangeX = {0,1}
- graph.rangeY = {0,1}
- graph.graphtiles = {0, 0.5, 1}
- ]]
- ret.new = function (self, ...)
- --[[ graphObjects:new(
- 0,1,
- 1,100,
- "........##",
- ".....###..",
- "..###.....",
- "##........")
- ".#",
- "#.")
- graphObjects:new("......oo",
- "....oo..",
- "..oo....",
- "oo......",
- 0,1,
- 1,4}
- obj.graphTiles = {1,1,2,2,3,3,4,4}
- ]]
- local args = {...}
- local obj = {}
- obj.rangeX = {0, 1}
- obj.rangeY = {0, 1}
- obj.graphtiles = {}
- obj.loops = false
- local numberArgs = 0
- local stringArgList = {}
- for i=1,#args do
- if type(args[i]) == "number" then
- if numberArgs < 2 then
- obj.rangeX[numberArgs + 1] = args[i]
- elseif numberArgs < 4 then
- obj.rangeY[numberArgs + 1] = args[i]
- end
- numberArgs = numberArgs + 1
- elseif type(args[i]) == "string" then
- table.insert(stringArgList, i)
- end
- end
- --go through each collum of the graph top to bottom
- if #stringArgList > 1 then
- local graphX = string.length(args[stringArgList[1]])
- local graphY = #stringArgList
- for i=1,graphX do
- for j=1,graphY do
- --if you find a char that is not "."
- --this collumn is done, break out of this loop
- --to continue the next collumn
- local graphchar = args[stringArgList[j]][i]
- if graphchar ~= "." then
- --((value-min) / magnitude) * magnitudeB + minB
- --((value-min) * magnitudeB) / magnitude + minB
- --((value-min) * (maxB-minB)) / (max-min) + minB
- -- -(j-1/gY-1) *magnitude +min
- -- ((graphY-1)-(j-1))*(magnitude)/(gY-1)+min
- obj.graphTiles[i] = ((graphY-1)-(j-1))*(obj.rangeY[2]-obj.rangeY[1])/(gY-1)+obj.rangeY[1]
- break
- end
- end
- --if it finds nothing in a collumn, whatever
- end
- else
- --graph not included
- obj.graphtiles[1] = obj.rangeY[1]
- obj.graphtiles[2] = obj.rangeY[2]
- end
- --create the metatable if it doesn't exist
- if not self.graphMT then
- self.graphMT = {}
- --metatable contains one value
- --graphs are used by doing graph(value)
- self.graphMT.__call = function(graph, value)
- local tempM = graph.rangeX[2]-graph.rangeX[1]
- local tileCount = #graph.graphTiles
- local tileWidth = (tileCount-1)/tempM
- local tileValue = ((value-graph.rangeX[1])*(tileCount-1)/(tempM)+(1)) % 1
- if graph.loops then
- local modvalue = tempM+tileWidth
- --realvalue range: 0 to one tileWidth above (max-min)
- local realValue = (value-graph.rangeX[1]) % modvalue
- --leftTile: tile left of realvalue
- --can be 1 to #graphTiles
- local leftTile = math.floor((realValue)*(tileCount-1)/(tempM)+(1))
- if leftTile == tileCount then
- --if the tile left of value is the final tile, other tile is 1
- return ((1-tileValue)*(graph.graphTiles[leftTile]))+((tileValue)*(graph.graphTiles[1]))
- else
- return ((1-tileValue)*(graph.graphTiles[leftTile]))+((tileValue)*(graph.graphTiles[leftTile+1]))
- end
- else
- local leftTile = math.floor((value-graph.rangeX[1])*(tileCount-1)/(tempM)+(1))
- if value < graph.rangeX[1] then
- --[[get t value between first two graph tiles
- --from a value from outside of that range
- --map graph range to 0 to (last tile - 1)
- --just change new minimum from 1 to 0
- --1 2 3...
- --0 1 2...
- value is currently [min-tileWidth, min]
- want it to be [0, 1]
- ]]
- --local firstTileValue = (value-graph.rangeX[1])*(tileCount-1)/(tempM)+(0)
- local firstTileValue = (value-(graph.rangeX[1]-tileWidth))*(1)/(tileWidth)+(0)
- --lerp with first two graph tiles
- return ((1-firstTileValue)*(graph.graphTiles[1]))+((firstTileValue)*(graph.graphTiles[2]))
- elseif value < graph.rangeX[2] then
- return ((1-tileValue)*(graph.graphTiles[leftTile]))+((tileValue)*(graph.graphTiles[leftTile+1]))
- else
- --[[get t value between last two graph tiles
- --...5 6 7 8 9
- --..-3-2-1 0 1
- --subtract 8 from min
- --subtract (max-1) from min(1)
- --graph range 2-4
- --0 1[2 3 4]5 6 7
- --value is at ]
- --3 tiles
- --0[1 2 3]4 5 6
- --desired outcome
- --graph 0 1[2 3 4]5 6 7
- --tiles 0[1 2 3]4 5 6
- --tiles -2-1 0]1 2 3 4 5 6
- --subtract by 3, aka tileCount
- value is greater than max
- [max, max+tileWidth]
- want [tileCount, tileCount+1]
- ]]
- --local lastTileValue = (value-graph.rangeX[1])*(tileCount-1)/(tempM)+(-tileCount)
- local lastTileValue = (value-graph.rangeX[2])*(1)/(tileWidth)+(tileCount)
- --lerp with last two graph tiles
- return ((1-lastTileValue)*(graph.graphTiles[tileCount-1]))+((lastTileValue)*(graph.graphTiles[tileCount]))
- end
- end
- end
- end
- setmetatable(obj, self.graphMT)
- return obj
- end
- return ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement