caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 7 | #include "QuadraticUtilities.h" |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 8 | #include <math.h> |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 9 | |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 10 | /* |
| 11 | |
| 12 | Numeric Solutions (5.6) suggests to solve the quadratic by computing |
| 13 | |
| 14 | Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C)) |
| 15 | |
| 16 | and using the roots |
| 17 | |
| 18 | t1 = Q / A |
| 19 | t2 = C / Q |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 23 | // note: caller expects multiple results to be sorted smaller first |
| 24 | // note: http://en.wikipedia.org/wiki/Loss_of_significance has an interesting |
| 25 | // analysis of the quadratic equation, suggesting why the following looks at |
| 26 | // the sign of B -- and further suggesting that the greatest loss of precision |
| 27 | // is in b squared less two a c |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 28 | int quadraticRoots(double A, double B, double C, double t[2]) { |
| 29 | B *= 2; |
| 30 | double square = B * B - 4 * A * C; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame^] | 31 | if (approximately_negative(square)) { |
| 32 | if (!approximately_positive(square)) { |
| 33 | return 0; |
| 34 | } |
| 35 | square = 0; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 36 | } |
| 37 | double squareRt = sqrt(square); |
| 38 | double Q = (B + (B < 0 ? -squareRt : squareRt)) / -2; |
| 39 | int foundRoots = 0; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 40 | double ratio = Q / A; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 41 | if (approximately_zero_or_more(ratio) && approximately_one_or_less(ratio)) { |
| 42 | if (approximately_less_than_zero(ratio)) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 43 | ratio = 0; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 44 | } else if (approximately_greater_than_one(ratio)) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 45 | ratio = 1; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 46 | } |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 47 | t[0] = ratio; |
| 48 | ++foundRoots; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 49 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 50 | ratio = C / Q; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 51 | if (approximately_zero_or_more(ratio) && approximately_one_or_less(ratio)) { |
| 52 | if (approximately_less_than_zero(ratio)) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 53 | ratio = 0; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 54 | } else if (approximately_greater_than_one(ratio)) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 55 | ratio = 1; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 56 | } |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 57 | if (foundRoots == 0 || !approximately_negative(ratio - t[0])) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 58 | t[foundRoots++] = ratio; |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 59 | } else if (!approximately_negative(t[0] - ratio)) { |
| 60 | t[foundRoots++] = t[0]; |
| 61 | t[0] = ratio; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 62 | } |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 63 | } |
| 64 | return foundRoots; |
| 65 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 66 | |
| 67 | void dxdy_at_t(const Quadratic& quad, double t, double& x, double& y) { |
| 68 | double a = t - 1; |
| 69 | double b = 1 - 2 * t; |
| 70 | double c = t; |
| 71 | if (&x) { |
| 72 | x = a * quad[0].x + b * quad[1].x + c * quad[2].x; |
| 73 | } |
| 74 | if (&y) { |
| 75 | y = a * quad[0].y + b * quad[1].y + c * quad[2].y; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void xy_at_t(const Quadratic& quad, double t, double& x, double& y) { |
| 80 | double one_t = 1 - t; |
| 81 | double a = one_t * one_t; |
| 82 | double b = 2 * one_t * t; |
| 83 | double c = t * t; |
| 84 | if (&x) { |
| 85 | x = a * quad[0].x + b * quad[1].x + c * quad[2].x; |
| 86 | } |
| 87 | if (&y) { |
| 88 | y = a * quad[0].y + b * quad[1].y + c * quad[2].y; |
| 89 | } |
| 90 | } |