Monado OpenXR Runtime
Loading...
Searching...
No Matches
quadratic.h
Go to the documentation of this file.
1// Copyright 2019, Philipp Zabel
2// Copyright 2026, Beyley Cardellio
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Quadratic root finder, specialized for P3P solving.
7 * @author Philipp Zabel <philipp.zabel@gmail.com>
8 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
9 * @ingroup xrt_iface
10 */
11
12#pragma once
13
14#include <math.h>
15#include <stdbool.h>
16
17
18/*
19 * Store the real roots of the reduced quadratic equation x² + bx + c = 0 in
20 * r1, r2. Returns true if roots are found, false otherwise.
21 */
22static inline bool
23reduced_quadratic_real_roots(double b, double c, double *r1, double *r2)
24{
25 const double half_b = 0.5 * b;
26 const double quarter_v = half_b * half_b - c;
27
28 if (quarter_v < 0) {
29 return false;
30 }
31
32 const double half_y = sqrt(quarter_v);
33
34 *r1 = -half_b + half_y;
35 *r2 = -half_b - half_y;
36
37 return true;
38}
39
40/*
41 * Store the real roots of the quadratic equation ax² + bx + c = 0 in
42 * r1, r2. Returns true if roots are found, false otherwise.
43 */
44static inline bool
45quadratic_real_roots(double a, double b, double c, double *r1, double *r2)
46{
47 const double a_inv = 1.0 / a;
48
49 return reduced_quadratic_real_roots(b * a_inv, c * a_inv, r1, r2);
50}