Hi
I have this function which draws a quadratic bezier curve:
public static Vector3 Position(float t, Vector3 a, Vector3 b, Vector3 anchor)
{
var oneMinusT = 1 - t;
var sumA = oneMinusT * oneMinusT * a;
var sumB = 2 * oneMinusT * t * anchor;
var sumC = t * t * b;
return sumA + sumB + sumC;
}
And i want to now find the bounding box for it. But i can't seem to get it to work. I used this equation:
https://stackoverflow.com/a/1099291/5736835
But i don't get the correct result.
This is my code for it:
var p0 = waypointA.Position;
var p1 = waypointB.Position;
var p2 = anchorPoint;
var extrema = Vector3.zero;
var tx = (p0.x - p1.x) / (p0.x - 2 * p1.x + p2.x);
extrema.x = QuadraticBezier.Position(tx, waypointA.Position, waypointB.Position, anchorPoint).x;
var tz = (p0.z - p1.z) / (p0.z - 2 * p1.z + p2.z);
extrema.z = QuadraticBezier.Position(tz, waypointA.Position, waypointB.Position, anchorPoint).z;
Extrema = extrema;
min = Vector3.Min(waypointA.Position, waypointB.Position);
max = Vector3.Max(waypointA.Position, waypointB.Position);
min = Vector3.Min(min, Extrema);
max = Vector3.Max(max, Extrema);
var center = (min + max) / 2f;
var size = max - min;
Bounds = new Bounds(center,size);
This was the visual end result of it:
Definitely not correct. The extrema point is the yellow sphere at the bottom. which is clearly not in the correct place, since its not even on the curve let alone the furthest point.
Am hoping some one knows how to solve this - i am not the best at maths so am trying to figure this out but its quite a challenge. I also found this as a guide on how to calculate it but i don't know how i would do that mathematically:
QuoteComputing the bounding box for a Bézier curve:
- Find all t value(s) for the curve derivative's x- and y-roots.
- Discard any t value that's lower than 0 or higher than 1, because Bézier curves only use the interval [0,1].
- Determine the lowest and highest value when plugging the values t=0, t=1 and each of the found roots into the original functions: the lowest value is the lower bound, and the highest value is the upper bound for the bounding box we want to construct.
If any one is able to help with this i would be very grateful, have been stuck on this for some time now.
Edit: Just to note i use the X:Z plane so my Vector3's all have Y position equal to zero.