How can I find the Y axis of a graph?

I have 2 tables that represent the X and Y axis of a graph, but how do I check the Y axis of “xPlayer”?

local xPlayer= math.random(xTable[1],xTable[#xTable])
local xTable = {0,2,4,4,6,9} --X axis of a graph
local yTable = {1,3,5,5,2,4} --Y axis of a graph

This may seem like a simple question, but I can’t find a way to do it no matter what.

I misread your post a bit, I rewrote it for you, this should work:

 local xPlayer= math.random(1, xTable[#xTable])
 local xTable = {0,2,4,4,6,9} --X axis of a graph
 local yTable = {1,3,5,5,2,4} --Y axis of a graph
 local yValue = yTable[xPlayer]

I’m not exactly sure what you’re trying to achieve though.

1 Like

Are you trying to create a 2-Dimensional Array?

I can try to explain it a little bit more.

I’m trying to make a graph like this:


The green dots represent a numbers inside the xTable and yTable, for example xTable[5] and yTable[5] (6,2).
The black dotted line represents a random xPlayer value, 7 on this example.
I’m trying to find where the dotted black line intercepts with the green line.

These might help with the math part of finding where lines on a 2D Graph intersect:(if that’s what you’re trying to do)

http://zonalandeducation.com/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.html

1 Like
local xTable = {0,2,4,4,6,9} --X axis of a graph
local yTable = {1,3,5,5,2,4} --Y axis of a graph
local xPlayer= math.random(xTable[1],xTable[#xTable])

-- First get nearest hardcoded points
local lowerX, higherX = xTable[1], xTable[#xTable]
local lowerIndex, higherIndex
-- What we're trying to do here is compare all values first with the given x to see if it's above or below the number 
-- And then if it is indeed the closest number above/below
for index, x in pairs(xTable) do
    if x < xPlayer and x > lowerX then
        lowerX = x
        lowerIndex = index
    elseif x > xPlayer and x< higherX then
        higherX = x
        higherIndex = index
    end
end
local lowerY, higherY = yTable[lowerIndex], yTable[higherIndex]

-- Get slope from points: Slope = (y2-y1)/(x2-x1)
local slope = (higherY - lowerY) / (higherX - lowerX)
-- Line formula is y = mx + b where m is the slope. 
-- We need to find b and we already have two points on the line
-- Meaning we can put in its x and y values
-- b = y - mx
b = lowerY - slope * lowerX
-- b = higherY - slope * higherX would work as well
-- Now in y = mx + b, we have b and a random x, we just need to solve for y
yPlayer = slope * xPlayer + b
print(yPlayer)  -- Done!
2 Likes

I had to change this

for index, x in pairs(xTable) do
    if x < xPlayer and x > lowerX then
        lowerX = x
        lowerIndex = index
    elseif x > xPlayer and x< higherX then
        higherX = x
        higherIndex = index
    end
end

to this

for index, x in pairs(xTable) do
    if x < xPlayer and x > lowerX then
        lowerX = x
        lowerIndex = index
    else
        higherX = x
        higherIndex = index
    end
end

But it works now, thank you so much!

I’m not sure if that would work as intended, I found the problem in my previous code:

local lowerIndex, higherIndex
-- Should be
local lowerIndex, higherIndex = 1, #xTable
1 Like

Yeah, that seems to work better than the change I made, since the change wouldn’t work in some specific numbers.
Still, thank you.

1 Like