private List <Vector2> Roots(Segment s) { // Get the derivatives of this segment List <Vector2> Ders = Derivatives(s); // Calculate the co-efficients Vector2 a = Ders[0] - (2f * Ders[1]) + Ders[2]; Vector2 b = 2f * (Ders[1] - Ders[0]); Vector2 c = Ders[0]; // Get the initail boundary Vector2 Lower = s.P1; Vector2 Upper = s.P4; // Re-arrange the boundary so points are in the right spot if (Lower.x > Upper.x) { float temp = Lower.x; Lower.x = Upper.x; Upper.x = temp; } if (Lower.y > Upper.y) { float temp = Lower.y; Lower.y = Upper.y; Upper.y = temp; } // Calculate the square root of the quadratic formula for both axes float XSqe = (b.x * b.x) - (4f * a.x * c.x); float YSqe = (b.y * b.y) - (4f * a.y * c.y); // Assure the square root is positive if (!MathF.Signbit(XSqe)) { // Calculate the plus quadratic formula float Time = (-b.x + MathF.Sqrt(XSqe)) / (2f * a.x); // Is this time valid? if (Valid(Time)) { // Calculate the point using the time float Val = Bezier(s.P1.x, s.P2.x, s.P3.x, s.P4.x, Time); // Is point less than the current lowest? if (Val < Lower.x) { Lower.x = Val; } } // Calculate the minus quadratic formula Time = (-b.x - MathF.Sqrt(XSqe)) / (2f * a.x); // Is this time valid if (Valid(Time)) { // Calculate the point using the time float Val = Bezier(s.P1.x, s.P2.x, s.P3.x, s.P4.x, Time); // Is point greater than the current highest? if (Val > Upper.x) { Upper.x = Val; } } } // Assure the square root is positive if (!MathF.Signbit(YSqe)) { // Calculate the plus quadratic formula float Time = (-b.y + MathF.Sqrt(YSqe)) / (2f * a.y); // Is this time valid? if (Valid(Time)) { // Calculate the point using the time float Val = Bezier(s.P1.y, s.P2.y, s.P3.y, s.P4.y, Time); // Is point less than the current lowest? if (Val < Lower.y) { Lower.y = Val; } } // Calculate the minus quadratic formula Time = (-b.y - MathF.Sqrt(YSqe)) / (2f * a.y); // Is this time valid if (Valid(Time)) { // Calculate the point using the time float Val = Bezier(s.P1.y, s.P2.y, s.P3.y, s.P4.y, Time); // Is point greater than the current highest? if (Val > Upper.y) { Upper.y = Val; } } } // Create the list List <Vector2> Roo = new List <Vector2>(2); // Add the roots to the list Roo.Add(Lower); Roo.Add(Upper); // Return the roots return(Roo); }
// ----- FUNCTIONS ----- \\ public float Magnitude() { return(MathF.Sqrt(Squared())); }