blob: 935a168f06149a6614b44db77a18e7d5a95792a0 [file] [log] [blame]
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001#include "CurveUtilities.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00002#include "Intersection_Tests.h"
caryclark@google.com639df892012-01-10 21:46:10 +00003#include "LineIntersection.h"
4
caryclark@google.com27accef2012-01-25 18:57:23 +00005// FIXME: add tests for intersecting, non-intersecting, degenerate, coincident
caryclark@google.com639df892012-01-10 21:46:10 +00006const _Line tests[][2] = {
caryclark@google.comc6825902012-02-03 22:07:47 +00007 {{{0, 0}, {1, 0}}, {{1, 0}, {0, 0}}},
8 {{{0, 0}, {0, 0}}, {{0, 0}, {1, 0}}},
9 {{{0, 1}, {0, 1}}, {{0, 0}, {0, 2}}},
10 {{{0, 0}, {1, 0}}, {{0, 0}, {2, 0}}},
11 {{{1, 1}, {2, 2}}, {{0, 0}, {3, 3}}},
rmistry@google.comd6176b02012-08-23 18:14:13 +000012 {{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}},
caryclark@google.comc6825902012-02-03 22:07:47 +000013 {{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}}
caryclark@google.com639df892012-01-10 21:46:10 +000014};
15
16const size_t tests_count = sizeof(tests) / sizeof(tests[0]);
17
caryclark@google.comc6825902012-02-03 22:07:47 +000018const _Line noIntersect[][2] = {
19 {{{0, 0}, {1, 0}}, {{3, 0}, {2, 0}}},
20 {{{0, 0}, {0, 0}}, {{1, 0}, {2, 0}}},
21 {{{0, 1}, {0, 1}}, {{0, 3}, {0, 2}}},
22 {{{0, 0}, {1, 0}}, {{2, 0}, {3, 0}}},
23 {{{1, 1}, {2, 2}}, {{4, 4}, {3, 3}}},
24};
25
26const size_t noIntersect_count = sizeof(noIntersect) / sizeof(noIntersect[0]);
27
caryclark@google.com639df892012-01-10 21:46:10 +000028static size_t firstLineIntersectionTest = 0;
caryclark@google.comc6825902012-02-03 22:07:47 +000029static size_t firstNoIntersectionTest = 0;
caryclark@google.com639df892012-01-10 21:46:10 +000030
31void LineIntersection_Test() {
caryclark@google.comc6825902012-02-03 22:07:47 +000032 size_t index;
33 for (index = firstLineIntersectionTest; index < tests_count; ++index) {
caryclark@google.com639df892012-01-10 21:46:10 +000034 const _Line& line1 = tests[index][0];
35 const _Line& line2 = tests[index][1];
caryclark@google.comc6825902012-02-03 22:07:47 +000036 double t1[2], t2[2];
37 int pts = intersect(line1, line2, t1, t2);
38 if (!pts) {
39 printf("%s [%zu] no intersection found\n", __FUNCTION__, index);
40 }
41 for (int i = 0; i < pts; ++i) {
42 _Point result1, result2;
43 xy_at_t(line1, t1[i], result1.x, result1.y);
44 xy_at_t(line2, t2[i], result2.x, result2.y);
45 if (!result1.approximatelyEqual(result2)) {
46 if (pts == 1) {
47 printf("%s [%zu] not equal\n", __FUNCTION__, index);
48 } else {
49 xy_at_t(line2, t2[i ^ 1], result2.x, result2.y);
50 if (!result1.approximatelyEqual(result2)) {
51 printf("%s [%zu] not equal\n", __FUNCTION__, index);
52 }
53 }
54 }
55 }
56 }
57 for (index = firstNoIntersectionTest; index < noIntersect_count; ++index) {
58 const _Line& line1 = noIntersect[index][0];
59 const _Line& line2 = noIntersect[index][1];
60 double t1[2], t2[2];
61 int pts = intersect(line1, line2, t1, t2);
62 if (pts) {
63 printf("%s [%zu] no intersection expected\n", __FUNCTION__, index);
64 }
caryclark@google.com639df892012-01-10 21:46:10 +000065 }
66}