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