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 */ 7 #include "CurveUtilities.h" 8 #include "Intersection_Tests.h" 9 #include "LineIntersection.h" 10 11 // FIXME: add tests for intersecting, non-intersecting, degenerate, coincident 12 const _Line tests[][2] = { 13 {{{0, 0}, {1, 0}}, {{1, 0}, {0, 0}}}, 14 {{{0, 0}, {0, 0}}, {{0, 0}, {1, 0}}}, 15 {{{0, 1}, {0, 1}}, {{0, 0}, {0, 2}}}, 16 {{{0, 0}, {1, 0}}, {{0, 0}, {2, 0}}}, 17 {{{1, 1}, {2, 2}}, {{0, 0}, {3, 3}}}, 18 {{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}}, 19 {{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}} 20 }; 21 22 const size_t tests_count = sizeof(tests) / sizeof(tests[0]); 23 24 const _Line noIntersect[][2] = { 25 {{{0, 0}, {1, 0}}, {{3, 0}, {2, 0}}}, 26 {{{0, 0}, {0, 0}}, {{1, 0}, {2, 0}}}, 27 {{{0, 1}, {0, 1}}, {{0, 3}, {0, 2}}}, 28 {{{0, 0}, {1, 0}}, {{2, 0}, {3, 0}}}, 29 {{{1, 1}, {2, 2}}, {{4, 4}, {3, 3}}}, 30 }; 31 32 const size_t noIntersect_count = sizeof(noIntersect) / sizeof(noIntersect[0]); 33 34 static size_t firstLineIntersectionTest = 0; 35 static size_t firstNoIntersectionTest = 0; 36 37 void LineIntersection_Test() { 38 size_t index; 39 for (index = firstLineIntersectionTest; index < tests_count; ++index) { 40 const _Line& line1 = tests[index][0]; 41 const _Line& line2 = tests[index][1]; 42 Intersections ts; 43 int pts = intersect(line1, line2, ts); 44 if (!pts) { 45 printf("%s [%zu] no intersection found\n", __FUNCTION__, index); 46 } 47 for (int i = 0; i < pts; ++i) { 48 _Point result1, result2; 49 xy_at_t(line1, ts.fT[0][i], result1.x, result1.y); 50 xy_at_t(line2, ts.fT[1][i], result2.x, result2.y); 51 if (!result1.approximatelyEqual(result2)) { 52 if (pts == 1) { 53 printf("%s [%zu] not equal\n", __FUNCTION__, index); 54 } else { 55 xy_at_t(line2, ts.fT[1][i ^ 1], result2.x, result2.y); 56 if (!result1.approximatelyEqual(result2)) { 57 printf("%s [%zu] not equal\n", __FUNCTION__, index); 58 } 59 } 60 } 61 } 62 } 63 for (index = firstNoIntersectionTest; index < noIntersect_count; ++index) { 64 const _Line& line1 = noIntersect[index][0]; 65 const _Line& line2 = noIntersect[index][1]; 66 Intersections ts; 67 int pts = intersect(line1, line2, ts); 68 if (pts) { 69 printf("%s [%zu] no intersection expected\n", __FUNCTION__, index); 70 } 71 } 72 } 73