blob: f855c4548220498952175635b5d737c5b1dce2cf [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
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.coma5764232012-03-28 16:20:21 +00007#ifndef Intersections_DEFINE
8#define Intersections_DEFINE
9
caryclark@google.com639df892012-01-10 21:46:10 +000010class Intersections {
11public:
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.comd6176b02012-08-23 18:14:13 +000045
caryclark@google.com639df892012-01-10 21:46:10 +000046 bool swapped() {
47 return fSwap;
48 }
49
50 int used() {
51 return fUsed;
52 }
53
54 double fT[2][9];
caryclark@google.com639df892012-01-10 21:46:10 +000055 int fUsed;
caryclark@google.coma5764232012-03-28 16:20:21 +000056private:
caryclark@google.com639df892012-01-10 21:46:10 +000057 int fSwap;
58};
caryclark@google.coma5764232012-03-28 16:20:21 +000059
60#endif
61