MCPcopy Create free account
hub / github.com/Kitware/VTK / QuadraticRoot

Method QuadraticRoot

Common/Core/vtkMath.cxx:3252–3311  ·  view source on GitHub ↗

------------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

3250
3251//------------------------------------------------------------------------------
3252int vtkMath::QuadraticRoot(double a, double b, double c, double min, double max, double* u)
3253{
3254 if (a == 0.0) // then its close to 0
3255 {
3256 if (b != 0.0) // not close to 0
3257 {
3258 u[0] = -c / b;
3259 if (u[0] > min && u[0] < max) // its in the interval
3260 {
3261 return 1; // 1 soln found
3262 }
3263 else // its not in the interval
3264 {
3265 return 0;
3266 }
3267 }
3268 else
3269 {
3270 return 0;
3271 }
3272 }
3273 double d = b * b - 4 * a * c; // discriminant
3274 if (d <= 0.0) // single or no root
3275 {
3276 if (d == 0.0) // close to 0
3277 {
3278 u[0] = -b / a;
3279 if (u[0] > min && u[0] < max) // its in the interval
3280 {
3281 return 1;
3282 }
3283 else // its not in the interval
3284 {
3285 return 0;
3286 }
3287 }
3288 else // no root d must be below 0
3289 {
3290 return 0;
3291 }
3292 }
3293 double q = -0.5 * (b + copysign(sqrt(d), b));
3294 u[0] = c / q;
3295 u[1] = q / a;
3296
3297 if ((u[0] > min && u[0] < max) && (u[1] > min && u[1] < max))
3298 {
3299 return 2;
3300 }
3301 else if (u[0] > min && u[0] < max) // then one wasn't in interval
3302 {
3303 return 1;
3304 }
3305 else if (u[1] > min && u[1] < max)
3306 { // make it easier, make u[0] be the valid one always
3307 std::swap(u[0], u[1]);
3308 return 1;
3309 }

Callers

nothing calls this directly

Calls 2

swapFunction · 0.70
sqrtFunction · 0.50

Tested by

no test coverage detected