caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1 | #ifndef Intersections_DEFINE |
| 2 | #define Intersections_DEFINE |
| 3 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 4 | class Intersections { |
| 5 | public: |
| 6 | Intersections() |
| 7 | : fUsed(0) |
| 8 | , fSwap(0) |
| 9 | { |
| 10 | bzero(fT, sizeof(fT)); |
| 11 | } |
| 12 | |
| 13 | void add(double one, double two) { |
| 14 | if (fUsed > 0 && approximately_equal(fT[fSwap][fUsed - 1], one) |
| 15 | && approximately_equal(fT[fSwap ^ 1][fUsed - 1], two)) { |
| 16 | return; |
| 17 | } |
| 18 | fT[fSwap][fUsed] = one; |
| 19 | fT[fSwap ^ 1][fUsed] = two; |
| 20 | ++fUsed; |
| 21 | } |
| 22 | |
| 23 | void offset(int base, double start, double end) { |
| 24 | for (int index = base; index < fUsed; ++index) { |
| 25 | double val = fT[fSwap][index]; |
| 26 | val *= end - start; |
| 27 | val += start; |
| 28 | fT[fSwap][index] = val; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | bool intersected() { |
| 33 | return fUsed > 0; |
| 34 | } |
| 35 | |
| 36 | void swap() { |
| 37 | fSwap ^= 1; |
| 38 | } |
| 39 | |
| 40 | bool swapped() { |
| 41 | return fSwap; |
| 42 | } |
| 43 | |
| 44 | int used() { |
| 45 | return fUsed; |
| 46 | } |
| 47 | |
| 48 | double fT[2][9]; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 49 | int fUsed; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 50 | private: |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 51 | int fSwap; |
| 52 | }; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 53 | |
| 54 | #endif |
| 55 | |