blob: 04b55be884a76a55d4a1caea7704d6a3494df4a0 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "Intersection_Tests.h"
caryclark@google.com639df892012-01-10 21:46:10 +00002#include "LineIntersection.h"
3
caryclark@google.com27accef2012-01-25 18:57:23 +00004// FIXME: add tests for intersecting, non-intersecting, degenerate, coincident
caryclark@google.com639df892012-01-10 21:46:10 +00005const _Line tests[][2] = {
caryclark@google.comc6825902012-02-03 22:07:47 +00006 {{{0, 0}, {1, 0}}, {{1, 0}, {0, 0}}},
7 {{{0, 0}, {0, 0}}, {{0, 0}, {1, 0}}},
8 {{{0, 1}, {0, 1}}, {{0, 0}, {0, 2}}},
9 {{{0, 0}, {1, 0}}, {{0, 0}, {2, 0}}},
10 {{{1, 1}, {2, 2}}, {{0, 0}, {3, 3}}},
caryclark@google.com639df892012-01-10 21:46:10 +000011 {{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}},
caryclark@google.comc6825902012-02-03 22:07:47 +000012 {{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}}
caryclark@google.com639df892012-01-10 21:46:10 +000013};
14
15const size_t tests_count = sizeof(tests) / sizeof(tests[0]);
16
caryclark@google.comc6825902012-02-03 22:07:47 +000017const _Line noIntersect[][2] = {
18 {{{0, 0}, {1, 0}}, {{3, 0}, {2, 0}}},
19 {{{0, 0}, {0, 0}}, {{1, 0}, {2, 0}}},
20 {{{0, 1}, {0, 1}}, {{0, 3}, {0, 2}}},
21 {{{0, 0}, {1, 0}}, {{2, 0}, {3, 0}}},
22 {{{1, 1}, {2, 2}}, {{4, 4}, {3, 3}}},
23};
24
25const size_t noIntersect_count = sizeof(noIntersect) / sizeof(noIntersect[0]);
26
caryclark@google.com639df892012-01-10 21:46:10 +000027static size_t firstLineIntersectionTest = 0;
caryclark@google.comc6825902012-02-03 22:07:47 +000028static size_t firstNoIntersectionTest = 0;
caryclark@google.com639df892012-01-10 21:46:10 +000029
30void LineIntersection_Test() {
caryclark@google.comc6825902012-02-03 22:07:47 +000031 size_t index;
32 for (index = firstLineIntersectionTest; index < tests_count; ++index) {
caryclark@google.com639df892012-01-10 21:46:10 +000033 const _Line& line1 = tests[index][0];
34 const _Line& line2 = tests[index][1];
caryclark@google.comc6825902012-02-03 22:07:47 +000035 double t1[2], t2[2];
36 int pts = intersect(line1, line2, t1, t2);
37 if (!pts) {
38 printf("%s [%zu] no intersection found\n", __FUNCTION__, index);
39 }
40 for (int i = 0; i < pts; ++i) {
41 _Point result1, result2;
42 xy_at_t(line1, t1[i], result1.x, result1.y);
43 xy_at_t(line2, t2[i], result2.x, result2.y);
44 if (!result1.approximatelyEqual(result2)) {
45 if (pts == 1) {
46 printf("%s [%zu] not equal\n", __FUNCTION__, index);
47 } else {
48 xy_at_t(line2, t2[i ^ 1], result2.x, result2.y);
49 if (!result1.approximatelyEqual(result2)) {
50 printf("%s [%zu] not equal\n", __FUNCTION__, index);
51 }
52 }
53 }
54 }
55 }
56 for (index = firstNoIntersectionTest; index < noIntersect_count; ++index) {
57 const _Line& line1 = noIntersect[index][0];
58 const _Line& line2 = noIntersect[index][1];
59 double t1[2], t2[2];
60 int pts = intersect(line1, line2, t1, t2);
61 if (pts) {
62 printf("%s [%zu] no intersection expected\n", __FUNCTION__, index);
63 }
caryclark@google.com639df892012-01-10 21:46:10 +000064 }
65}