I have a graph like this:
(sorry if it’s too bright )
How do i make a function where i plug X and it gives me Y, or if i plug Y it gives me X, i dont need to get the X value from Y but it’d be nice to know.
I have a graph like this:
(sorry if it’s too bright )
How do i make a function where i plug X and it gives me Y, or if i plug Y it gives me X, i dont need to get the X value from Y but it’d be nice to know.
Are you implementing zoom, too? Because I’ve programmed things like this in other languages before, and it takes a little maths.
If not, it’s pretty simple. Define yourself a function, and the inverse of the function. When you want to figure out the Y from the X, the position will look like this:
local position = Vector2.new(x, func(x))
local position = Vector2.new(invrs(y), y)
What is X? What is Y? What does that graph mean?
Are you trying to draw on the graph?
No i am not implementing zoom, i almost figured out the camera clipping issue you replied to earlier today, ill explain further on that other post after i think i solved it.
So for now i just need to figure out how to plug an X value in a function and get the corresponding Y value. Also X is clamped between 0-180
It’s so easy. Do you have a function that determines what y will be? Because that’s pretty much it.
Nope, it’s what i am trying to make, all i have is that graph.
So you’re trying to get a function? Ah, okay. Could you explain what the x and y mean?
So the X is a dot product in degrees between camera lookVector and character’s pivot lookVector. The Y is a multiplier i want to use somewhere which scales depending on that dot product aka the X value.
Heh, I’m not a master at dot products, but if you already have the dot product in radians OR degrees, all you really need to do use some simple multiplication/division.
function f(x)
return x / 60 -- returns 3 if the angle is 180, and 1 is the angle is 60.
end
If you want to clamp it under 180, which now that I think about it, it is physically impossible in non-euclidean space to be looking at an angle greater than 180 away from something, unless I’m wrong. Use math.min()
to be safe.
That wouldnt work, because if I plug 90, i expect to get 4 but i get 1.5
Then divide by 22.5. You’ll get 4 if you plug in 90, and 8 if you plug in 180. I think I realized what you’re trying to do. Make abstract functions. Instead of doing this, if you’re only dealing with integers, using a dictionary. Like this:
local answers = {
["90"] = 4,
["180"] = 3
...
}
And so on. ORRRRRR watch this video:
And remember, INVERSES!!! if you want to reverse the process with the function you already have, you need to inverse the function.
Assuming the graph is exactly what you want it would require using a piecewise function which is just a group of functions that have conditionals attached to them to when each function can be used.
You can just get the equations for the positive slope segment and negative slope segments and use one or the other depending on the relation X has to 90.
Example of what I’m trying to get across…
if x < 90 then
y = 2x + 1
else
y = -1x + 6
end
that’s what i was about to do, ill just get the slope of those two lines and do it conditionally like you said.
-- Returns two functions getY and getX
-- plug x in getY and get where the Y would be
-- plug y in getX and get where the X would be
local function getGraphEquations(point1: Vector2, point2: Vector2)
local slope = (point1.Y - point2.Y) / (point1.X - point2.X)
local yIntercept = -slope * point1.X + point1.Y
local function getX(y: number)
return (y - yIntercept) / slope
end
local function getY(x: number)
return slope * x + yIntercept
end
return getX, getY
end
local x2 = 90 -- The point we'll use for if statment
local _, getY1 = getGraphEquations(Vector2.new(0, 2), Vector2.new(x2, 4))
local _, getY2 = getGraphEquations(Vector2.new(x2, 4), Vector2.new(180, 3))
local function getY(x)
if x > 90 then
return getY2(x)
else
return getY1(x)
end
end
That would be different depending on how many points you have in your graph, but the idea is we use getGraphEquations
to get how any point will plot in a graphed line, we then make an if statement to switch between the two lines when necessary to get the correct answer.
This is an even better function:
local function yGetterConstructor(...: Vector2)
local points = { ... }
local slopes, yIntercepts = {}, {}
local iter = ipairs(points)
local p2, m
for i, p1: Vector2 in iter, points, 0 do
p2 = points[i + 1]
if p2 then
m = (p1.Y - p2.Y) / (p1.X - p2.X)
table.insert(slopes, m)
table.insert(yIntercepts, -m * p1.X + p1.Y)
end
end
slopes[0] = slopes[1]
yIntercepts[0] = yIntercepts[1]
local function getY(x: number)
local m, b
for i, p1: Vector2 in iter, points, 0 do
if x < p1.X then
m = slopes[i - 1]
b = yIntercepts[i - 1]
break
end
end
if not m then
m = slopes[#slopes]
b = yIntercepts[#yIntercepts]
end
return m * x + b
end
return getY
end
You feed the function your graph points from left to right and it returns a getY
function where you plug your X
number and get what Y
would be.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.