So I'm in a weird situation where I need to be able to find the intersection point of two function equations. What would be the best way to do this in MV? Lowkey stuck in a pickle here and i fear this problem is so niche i cant find a solution.
How to find intersection of two functions.
RPG Maker MV SupportYou can try inputting both equations in a graphing calculator and see where the intersection point is.
www.wolframalpha.com
y=3x, y=x^2 - Wolfram|Alpha
Wolfram|Alpha brings expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels.
probably should have mentioned that one of the functions changes whenever you have an input. basically i have a line that changes depending on an angle variable and i need to find where that line intersects the ellipse given the two functions
Then the function itself would be a variable and you would have to rerun the calculation every time you changed it.
yeah, you want to return the calculation as a variable to the other function -- pretty sure that would work (i'm an amateur... but still think you could try it)
It sounds like you want to solve a system of equations? To do this in a completely general case would require an algebra library, but I'm guessing you actually have two specific equations in mind, even if there might be an unknown variable mixed in. So, you might be able to plug the equations into Wolfram Alpha to get a formula for the solution in terms of the unknown variable. (Click here for a random example – not how it says there's no solution if q is equal to 7/20, but otherwise there's a nice formula for a solution for any given q.)
Without knowing anything about the specific equations, there's not much more we can do to help.
Without knowing anything about the specific equations, there's not much more we can do to help.
OK, I gotchu. Hopefully I don't make any mistakes in my maths here! (Even if I do, hopefully you get the idea.)
- Straight line:
y = mx + k - Ellipse (centred on 0, 0):
(x/a)² + (y/b)² = 1- Therefore
y = ± sqrt(a² - x²) b/a
c,d), replacex->x - candy->y - d.) - Therefore
y values are equal:mx + k = ± sqrt(a² - x²) b/a- Square both sides:
(mx + k)² = (a² - x²) (b/a)² - Expand brackets:
m²x² + 2mkx + k² = b² - b²x²/a² - Collect like terms:
(m² + b²/a²) x² + 2mkx + (k² - b²) = 0 Solve quadratic:x = -2mk ± sqrt(4m²k² - 4(m² + b²/a²)(k² - b²)) / 2(m² + b²/a²)Cancel sqrt(4)/2:x = -2mk ± sqrt(m²k² - (m² + b²/a²)(k² - b²)) / (m² + b²/a²)Expand brackets:x = -2mk ± sqrt(m²k² - m²k² - b²k²/a² + m²b² + b⁴/a²) / (m² + b²/a²)Simplify:x = -2mk ± sqrt(b⁴/a² + m²b² - b²k²/a²) / (m² + b²/a²)Prettify:x = -2mk ± ab sqrt(b² + m²a² - a²k²) / (a²m² + b²)
I forgot to put brackets around the
-2mk in the "Solve quadratic" step, and I accidentally "prettified" an extra factor a² in there. That's what I get for trying to rush it! I see you found a solution, and you could just feed it into a CAS instead of solving by hand, but here's a revised version of my approach:- Solve quadratic:
x = (-2mk ± sqrt(4m²k² - 4(m² + b²/a²)(k² - b²)) / 2(m² + b²/a²) - Cancel 2/2:
x = (-mk ± sqrt(m²k² - (m² + b²/a²)(k² - b²)) / (m² + b²/a²) - Expand brackets:
(-mk ± sqrt(m²k² - m²k² + m²b² - b²k²/a² + b⁴/a²)) / (m² + b²/a²) - Simplify:
x = (-mk ± sqrt(m²b² - b²k²/a² + b⁴/a²)) / (m² + b²/a²) - Extract
b/afrom sqrt:x = (-mk ± b/a sqrt(a²m² + b² - k²)) / (m² + b²/a²) - Multiply through by
a²:x = (-mka² ± ab sqrt(a²m² + b² - k²)) / (a²m² + b²))
Then you can substitute back into
y = mx + k to get the corresponding y value.Note that the square root only produces a real result if the input,
b² + m²a² - a²k², is non-negative:- Positive: 2 intersections.
- Zero: 1 intersection (line is a tangent to the ellipse).
- Negative: 0 intersections.
You can use
m = tan(θ) to convert θ (angle from positive x axis to line) to a y/x gradient. In JavaScript (e.g. Conditional Branch -> Script):- Tangent:
Math.tan(value) - Square root:
Math.sqrt(value) - Square:
value * valueorMath.pow(value, 2)
I just checked, but Conditional Branch doesn't seem to have a
value variable, so you'd need to substitute it with the actual value. But Control Variables does have a value variable, so there you could just paste these scripts in as-is.Yes, I should've specified that you need to replace that, e.g. with stuff like
$gameVariables.value(123) (value of variable ID 123).this is great! only thing is my equations look more like
y=mxand
y=.554 * sqrt(187056.25 - x^2)and im not quite sure where to go. So far with doing the math I've gotten to
mx^2/.307 = 187056.25-x^2and after that im blanking
Heya!
What functions are you trying to use? Is it constrained to line and ellipse only?
How would you do this in MV? Do you want to read it from game variables as part of player interaction or is it for a mechanics you're trying to code?
What functions are you trying to use? Is it constrained to line and ellipse only?
How would you do this in MV? Do you want to read it from game variables as part of player interaction or is it for a mechanics you're trying to code?
Okay i actually did it. only slightly make me cry
That's awesome! What is it for? :hhappy:
I was watching an ellipse-line refresher on youtube since I was curious. I figured out I could share what I found: I realized we might be able to use discriminant to optimize this. (I might be wrong hehe)
So:
Since the goal is to find intersection points, we want to find (x,y) that satisfies both equations, right? Which means we could take y value from the line equation and just plug it into the ellipse. Like so: x²/a² + (mx + c)²/b² = 1
Then you can rearrange it to be like so: (b² + a²m²)x² + 2a²mcx + (a²c² - a²b²) = 0
It is a quadratic equation, which means you can calculate the discriminant.
After series of algebraic manipulation you can get this: D = 4a²b²[(a²m² + b²) - c²]
You can see the discriminant is actually relying on the things in the square brackets: (a²m² + b²) - c²
We can think of it as two terms:
I was watching an ellipse-line refresher on youtube since I was curious. I figured out I could share what I found: I realized we might be able to use discriminant to optimize this. (I might be wrong hehe)
So:
- Line equation: y = mx + c
- Ellipse equation: x²/a² + y²/b² = 1
Since the goal is to find intersection points, we want to find (x,y) that satisfies both equations, right? Which means we could take y value from the line equation and just plug it into the ellipse. Like so: x²/a² + (mx + c)²/b² = 1
Then you can rearrange it to be like so: (b² + a²m²)x² + 2a²mcx + (a²c² - a²b²) = 0
It is a quadratic equation, which means you can calculate the discriminant.
After series of algebraic manipulation you can get this: D = 4a²b²[(a²m² + b²) - c²]
You can see the discriminant is actually relying on the things in the square brackets: (a²m² + b²) - c²
We can think of it as two terms:
- term 1: (a²m² + b²)
- term 2: c²
- if term 1 < term2, it means no intersection point
- if term 1 == term 2, it means one intersection point
- if term 1 > term 2, it means two intersection points
- no intersection point = returns [] or {} depends on you
- one intersection point = calculate x with quadratic formula but the D is zero so you can skip Math.sqrt, then calculate y by plugging x back into y = mx + c
- two intersection point = calculate both x values with quadratic formula, then calculate y for each x by plugging into y = mx + c
JavaScript:
function lineEllipseIntersection(line, ellipse){
const { m, c } = line;
const { a, b } = ellipse;
const term1 = (a**2 * m**2) + b**2; // (a²m² + b²)
const term2 = c**2; // c²
const discriminant = term1 - term2; // We just need to know if its negative, zero, or positive
if(discriminant < 0){
return []; // No intersection
}
// From this: (b² + a²m²)x² + 2a²mcx + (a²c² - a²b²) = 0
const A = term1; // A is the same as term1 anyway
const B = 2 * a**2 * m * c;
// √D = 2ab√(term1 - term2)
const sqrtD = 2 * a * b * Math.sqrt(discriminant);
if(discriminant === 0){
// One intersection (tangent)
const x = -B / (2 * A);
const y = m * x + c;
return [{x, y}];
}
// Two intersections
const x1 = (-B + sqrtD) / (2 * A);
const x2 = (-B - sqrtD) / (2 * A);
return [
{x: x1, y: m * x1 + c},
{x: x2, y: m * x2 + c}
];
}
I plugged those into Wolfram Alpha for you. It claims your intersections are given by:
x = -239605/(2*sqrt(250000*m^2 + 76729)), y = -(239605*m)/(2*sqrt(250000*m^2 + 76729))
and
x = 239605/(2*sqrt(250000*m^2 + 76729)), y = (239605*m)/(2*sqrt(250000*m^2 + 76729))
It does look a little strange to have such large numbers, though…
😢 😂
Just for reference, this would mean:
- The intercept of the line (
kin my example,cin LuxFerra's) is0; a²is 187056.25 =>a = 432.5. (I assume it's in screen px.)b/ais 0.554 =>b = 239.605.
It's just because the CAS scaled everything up to integers. Big numbers on both the top and bottom of the fraction cancel out, e.g. try substituting
m = 0.5. 👋Original thread: https://forums.rpgmakerweb.com/threads/how-to-find-intersection-of-two-functions.181486/