michael@0: // Another approach is to start with the implicit form of one curve and solve michael@0: // (seek implicit coefficients in QuadraticParameter.cpp michael@0: // by substituting in the parametric form of the other. michael@0: // The downside of this approach is that early rejects are difficult to come by. michael@0: // http://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html#step michael@0: michael@0: michael@0: #include "SkDQuadImplicit.h" michael@0: #include "SkIntersections.h" michael@0: #include "SkPathOpsLine.h" michael@0: #include "SkQuarticRoot.h" michael@0: #include "SkTArray.h" michael@0: #include "SkTSort.h" michael@0: michael@0: /* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F michael@0: * and given x = at^2 + bt + c (the parameterized form) michael@0: * y = dt^2 + et + f michael@0: * then michael@0: * 0 = A(at^2+bt+c)(at^2+bt+c)+B(at^2+bt+c)(dt^2+et+f)+C(dt^2+et+f)(dt^2+et+f)+D(at^2+bt+c)+E(dt^2+et+f)+F michael@0: */ michael@0: michael@0: static int findRoots(const SkDQuadImplicit& i, const SkDQuad& quad, double roots[4], michael@0: bool oneHint, bool flip, int firstCubicRoot) { michael@0: SkDQuad flipped; michael@0: const SkDQuad& q = flip ? (flipped = quad.flip()) : quad; michael@0: double a, b, c; michael@0: SkDQuad::SetABC(&q[0].fX, &a, &b, &c); michael@0: double d, e, f; michael@0: SkDQuad::SetABC(&q[0].fY, &d, &e, &f); michael@0: const double t4 = i.x2() * a * a michael@0: + i.xy() * a * d michael@0: + i.y2() * d * d; michael@0: const double t3 = 2 * i.x2() * a * b michael@0: + i.xy() * (a * e + b * d) michael@0: + 2 * i.y2() * d * e; michael@0: const double t2 = i.x2() * (b * b + 2 * a * c) michael@0: + i.xy() * (c * d + b * e + a * f) michael@0: + i.y2() * (e * e + 2 * d * f) michael@0: + i.x() * a michael@0: + i.y() * d; michael@0: const double t1 = 2 * i.x2() * b * c michael@0: + i.xy() * (c * e + b * f) michael@0: + 2 * i.y2() * e * f michael@0: + i.x() * b michael@0: + i.y() * e; michael@0: const double t0 = i.x2() * c * c michael@0: + i.xy() * c * f michael@0: + i.y2() * f * f michael@0: + i.x() * c michael@0: + i.y() * f michael@0: + i.c(); michael@0: int rootCount = SkReducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots); michael@0: if (rootCount < 0) { michael@0: rootCount = SkQuarticRootsReal(firstCubicRoot, t4, t3, t2, t1, t0, roots); michael@0: } michael@0: if (flip) { michael@0: for (int index = 0; index < rootCount; ++index) { michael@0: roots[index] = 1 - roots[index]; michael@0: } michael@0: } michael@0: return rootCount; michael@0: } michael@0: michael@0: static int addValidRoots(const double roots[4], const int count, double valid[4]) { michael@0: int result = 0; michael@0: int index; michael@0: for (index = 0; index < count; ++index) { michael@0: if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) { michael@0: continue; michael@0: } michael@0: double t = 1 - roots[index]; michael@0: if (approximately_less_than_zero(t)) { michael@0: t = 0; michael@0: } else if (approximately_greater_than_one(t)) { michael@0: t = 1; michael@0: } michael@0: valid[result++] = t; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static bool only_end_pts_in_common(const SkDQuad& q1, const SkDQuad& q2) { michael@0: // the idea here is to see at minimum do a quick reject by rotating all points michael@0: // to either side of the line formed by connecting the endpoints michael@0: // if the opposite curves points are on the line or on the other side, the michael@0: // curves at most intersect at the endpoints michael@0: for (int oddMan = 0; oddMan < 3; ++oddMan) { michael@0: const SkDPoint* endPt[2]; michael@0: for (int opp = 1; opp < 3; ++opp) { michael@0: int end = oddMan ^ opp; // choose a value not equal to oddMan michael@0: if (3 == end) { // and correct so that largest value is 1 or 2 michael@0: end = opp; michael@0: } michael@0: endPt[opp - 1] = &q1[end]; michael@0: } michael@0: double origX = endPt[0]->fX; michael@0: double origY = endPt[0]->fY; michael@0: double adj = endPt[1]->fX - origX; michael@0: double opp = endPt[1]->fY - origY; michael@0: double sign = (q1[oddMan].fY - origY) * adj - (q1[oddMan].fX - origX) * opp; michael@0: if (approximately_zero(sign)) { michael@0: goto tryNextHalfPlane; michael@0: } michael@0: for (int n = 0; n < 3; ++n) { michael@0: double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp; michael@0: if (test * sign > 0 && !precisely_zero(test)) { michael@0: goto tryNextHalfPlane; michael@0: } michael@0: } michael@0: return true; michael@0: tryNextHalfPlane: michael@0: ; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // returns false if there's more than one intercept or the intercept doesn't match the point michael@0: // returns true if the intercept was successfully added or if the michael@0: // original quads need to be subdivided michael@0: static bool add_intercept(const SkDQuad& q1, const SkDQuad& q2, double tMin, double tMax, michael@0: SkIntersections* i, bool* subDivide) { michael@0: double tMid = (tMin + tMax) / 2; michael@0: SkDPoint mid = q2.ptAtT(tMid); michael@0: SkDLine line; michael@0: line[0] = line[1] = mid; michael@0: SkDVector dxdy = q2.dxdyAtT(tMid); michael@0: line[0] -= dxdy; michael@0: line[1] += dxdy; michael@0: SkIntersections rootTs; michael@0: rootTs.allowNear(false); michael@0: int roots = rootTs.intersect(q1, line); michael@0: if (roots == 0) { michael@0: if (subDivide) { michael@0: *subDivide = true; michael@0: } michael@0: return true; michael@0: } michael@0: if (roots == 2) { michael@0: return false; michael@0: } michael@0: SkDPoint pt2 = q1.ptAtT(rootTs[0][0]); michael@0: if (!pt2.approximatelyEqual(mid)) { michael@0: return false; michael@0: } michael@0: i->insertSwap(rootTs[0][0], tMid, pt2); michael@0: return true; michael@0: } michael@0: michael@0: static bool is_linear_inner(const SkDQuad& q1, double t1s, double t1e, const SkDQuad& q2, michael@0: double t2s, double t2e, SkIntersections* i, bool* subDivide) { michael@0: SkDQuad hull = q1.subDivide(t1s, t1e); michael@0: SkDLine line = {{hull[2], hull[0]}}; michael@0: const SkDLine* testLines[] = { &line, (const SkDLine*) &hull[0], (const SkDLine*) &hull[1] }; michael@0: const size_t kTestCount = SK_ARRAY_COUNT(testLines); michael@0: SkSTArray tsFound; michael@0: for (size_t index = 0; index < kTestCount; ++index) { michael@0: SkIntersections rootTs; michael@0: rootTs.allowNear(false); michael@0: int roots = rootTs.intersect(q2, *testLines[index]); michael@0: for (int idx2 = 0; idx2 < roots; ++idx2) { michael@0: double t = rootTs[0][idx2]; michael@0: #ifdef SK_DEBUG michael@0: SkDPoint qPt = q2.ptAtT(t); michael@0: SkDPoint lPt = testLines[index]->ptAtT(rootTs[1][idx2]); michael@0: SkASSERT(qPt.approximatelyPEqual(lPt)); michael@0: #endif michael@0: if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) { michael@0: continue; michael@0: } michael@0: tsFound.push_back(rootTs[0][idx2]); michael@0: } michael@0: } michael@0: int tCount = tsFound.count(); michael@0: if (tCount <= 0) { michael@0: return true; michael@0: } michael@0: double tMin, tMax; michael@0: if (tCount == 1) { michael@0: tMin = tMax = tsFound[0]; michael@0: } else { michael@0: SkASSERT(tCount > 1); michael@0: SkTQSort(tsFound.begin(), tsFound.end() - 1); michael@0: tMin = tsFound[0]; michael@0: tMax = tsFound[tsFound.count() - 1]; michael@0: } michael@0: SkDPoint end = q2.ptAtT(t2s); michael@0: bool startInTriangle = hull.pointInHull(end); michael@0: if (startInTriangle) { michael@0: tMin = t2s; michael@0: } michael@0: end = q2.ptAtT(t2e); michael@0: bool endInTriangle = hull.pointInHull(end); michael@0: if (endInTriangle) { michael@0: tMax = t2e; michael@0: } michael@0: int split = 0; michael@0: SkDVector dxy1, dxy2; michael@0: if (tMin != tMax || tCount > 2) { michael@0: dxy2 = q2.dxdyAtT(tMin); michael@0: for (int index = 1; index < tCount; ++index) { michael@0: dxy1 = dxy2; michael@0: dxy2 = q2.dxdyAtT(tsFound[index]); michael@0: double dot = dxy1.dot(dxy2); michael@0: if (dot < 0) { michael@0: split = index - 1; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (split == 0) { // there's one point michael@0: if (add_intercept(q1, q2, tMin, tMax, i, subDivide)) { michael@0: return true; michael@0: } michael@0: i->swap(); michael@0: return is_linear_inner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide); michael@0: } michael@0: // At this point, we have two ranges of t values -- treat each separately at the split michael@0: bool result; michael@0: if (add_intercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) { michael@0: result = true; michael@0: } else { michael@0: i->swap(); michael@0: result = is_linear_inner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide); michael@0: } michael@0: if (add_intercept(q1, q2, tsFound[split], tMax, i, subDivide)) { michael@0: result = true; michael@0: } else { michael@0: i->swap(); michael@0: result |= is_linear_inner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static double flat_measure(const SkDQuad& q) { michael@0: SkDVector mid = q[1] - q[0]; michael@0: SkDVector dxy = q[2] - q[0]; michael@0: double length = dxy.length(); // OPTIMIZE: get rid of sqrt michael@0: return fabs(mid.cross(dxy) / length); michael@0: } michael@0: michael@0: // FIXME ? should this measure both and then use the quad that is the flattest as the line? michael@0: static bool is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) { michael@0: double measure = flat_measure(q1); michael@0: // OPTIMIZE: (get rid of sqrt) use approximately_zero michael@0: if (!approximately_zero_sqrt(measure)) { michael@0: return false; michael@0: } michael@0: return is_linear_inner(q1, 0, 1, q2, 0, 1, i, NULL); michael@0: } michael@0: michael@0: // FIXME: if flat measure is sufficiently large, then probably the quartic solution failed michael@0: // avoid imprecision incurred with chopAt michael@0: static void relaxed_is_linear(const SkDQuad* q1, double s1, double e1, const SkDQuad* q2, michael@0: double s2, double e2, SkIntersections* i) { michael@0: double m1 = flat_measure(*q1); michael@0: double m2 = flat_measure(*q2); michael@0: i->reset(); michael@0: const SkDQuad* rounder, *flatter; michael@0: double sf, midf, ef, sr, er; michael@0: if (m2 < m1) { michael@0: rounder = q1; michael@0: sr = s1; michael@0: er = e1; michael@0: flatter = q2; michael@0: sf = s2; michael@0: midf = (s2 + e2) / 2; michael@0: ef = e2; michael@0: } else { michael@0: rounder = q2; michael@0: sr = s2; michael@0: er = e2; michael@0: flatter = q1; michael@0: sf = s1; michael@0: midf = (s1 + e1) / 2; michael@0: ef = e1; michael@0: } michael@0: bool subDivide = false; michael@0: is_linear_inner(*flatter, sf, ef, *rounder, sr, er, i, &subDivide); michael@0: if (subDivide) { michael@0: relaxed_is_linear(flatter, sf, midf, rounder, sr, er, i); michael@0: relaxed_is_linear(flatter, midf, ef, rounder, sr, er, i); michael@0: } michael@0: if (m2 < m1) { michael@0: i->swapPts(); michael@0: } michael@0: } michael@0: michael@0: // each time through the loop, this computes values it had from the last loop michael@0: // if i == j == 1, the center values are still good michael@0: // otherwise, for i != 1 or j != 1, four of the values are still good michael@0: // and if i == 1 ^ j == 1, an additional value is good michael@0: static bool binary_search(const SkDQuad& quad1, const SkDQuad& quad2, double* t1Seed, michael@0: double* t2Seed, SkDPoint* pt) { michael@0: double tStep = ROUGH_EPSILON; michael@0: SkDPoint t1[3], t2[3]; michael@0: int calcMask = ~0; michael@0: do { michael@0: if (calcMask & (1 << 1)) t1[1] = quad1.ptAtT(*t1Seed); michael@0: if (calcMask & (1 << 4)) t2[1] = quad2.ptAtT(*t2Seed); michael@0: if (t1[1].approximatelyEqual(t2[1])) { michael@0: *pt = t1[1]; michael@0: #if ONE_OFF_DEBUG michael@0: SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__, michael@0: t1Seed, t2Seed, t1[1].fX, t1[1].fY, t2[1].fX, t2[1].fY); michael@0: #endif michael@0: return true; michael@0: } michael@0: if (calcMask & (1 << 0)) t1[0] = quad1.ptAtT(*t1Seed - tStep); michael@0: if (calcMask & (1 << 2)) t1[2] = quad1.ptAtT(*t1Seed + tStep); michael@0: if (calcMask & (1 << 3)) t2[0] = quad2.ptAtT(*t2Seed - tStep); michael@0: if (calcMask & (1 << 5)) t2[2] = quad2.ptAtT(*t2Seed + tStep); michael@0: double dist[3][3]; michael@0: // OPTIMIZE: using calcMask value permits skipping some distance calcuations michael@0: // if prior loop's results are moved to correct slot for reuse michael@0: dist[1][1] = t1[1].distanceSquared(t2[1]); michael@0: int best_i = 1, best_j = 1; michael@0: for (int i = 0; i < 3; ++i) { michael@0: for (int j = 0; j < 3; ++j) { michael@0: if (i == 1 && j == 1) { michael@0: continue; michael@0: } michael@0: dist[i][j] = t1[i].distanceSquared(t2[j]); michael@0: if (dist[best_i][best_j] > dist[i][j]) { michael@0: best_i = i; michael@0: best_j = j; michael@0: } michael@0: } michael@0: } michael@0: if (best_i == 1 && best_j == 1) { michael@0: tStep /= 2; michael@0: if (tStep < FLT_EPSILON_HALF) { michael@0: break; michael@0: } michael@0: calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5); michael@0: continue; michael@0: } michael@0: if (best_i == 0) { michael@0: *t1Seed -= tStep; michael@0: t1[2] = t1[1]; michael@0: t1[1] = t1[0]; michael@0: calcMask = 1 << 0; michael@0: } else if (best_i == 2) { michael@0: *t1Seed += tStep; michael@0: t1[0] = t1[1]; michael@0: t1[1] = t1[2]; michael@0: calcMask = 1 << 2; michael@0: } else { michael@0: calcMask = 0; michael@0: } michael@0: if (best_j == 0) { michael@0: *t2Seed -= tStep; michael@0: t2[2] = t2[1]; michael@0: t2[1] = t2[0]; michael@0: calcMask |= 1 << 3; michael@0: } else if (best_j == 2) { michael@0: *t2Seed += tStep; michael@0: t2[0] = t2[1]; michael@0: t2[1] = t2[2]; michael@0: calcMask |= 1 << 5; michael@0: } michael@0: } while (true); michael@0: #if ONE_OFF_DEBUG michael@0: SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__, michael@0: t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY); michael@0: #endif michael@0: return false; michael@0: } michael@0: michael@0: static void lookNearEnd(const SkDQuad& q1, const SkDQuad& q2, int testT, michael@0: const SkIntersections& orig, bool swap, SkIntersections* i) { michael@0: if (orig.used() == 1 && orig[!swap][0] == testT) { michael@0: return; michael@0: } michael@0: if (orig.used() == 2 && orig[!swap][1] == testT) { michael@0: return; michael@0: } michael@0: SkDLine tmpLine; michael@0: int testTIndex = testT << 1; michael@0: tmpLine[0] = tmpLine[1] = q2[testTIndex]; michael@0: tmpLine[1].fX += q2[1].fY - q2[testTIndex].fY; michael@0: tmpLine[1].fY -= q2[1].fX - q2[testTIndex].fX; michael@0: SkIntersections impTs; michael@0: impTs.intersectRay(q1, tmpLine); michael@0: for (int index = 0; index < impTs.used(); ++index) { michael@0: SkDPoint realPt = impTs.pt(index); michael@0: if (!tmpLine[0].approximatelyEqual(realPt)) { michael@0: continue; michael@0: } michael@0: if (swap) { michael@0: i->insert(testT, impTs[0][index], tmpLine[0]); michael@0: } else { michael@0: i->insert(impTs[0][index], testT, tmpLine[0]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: int SkIntersections::intersect(const SkDQuad& q1, const SkDQuad& q2) { michael@0: fMax = 4; michael@0: // if the quads share an end point, check to see if they overlap michael@0: for (int i1 = 0; i1 < 3; i1 += 2) { michael@0: for (int i2 = 0; i2 < 3; i2 += 2) { michael@0: if (q1[i1].asSkPoint() == q2[i2].asSkPoint()) { michael@0: insert(i1 >> 1, i2 >> 1, q1[i1]); michael@0: } michael@0: } michael@0: } michael@0: SkASSERT(fUsed < 3); michael@0: if (only_end_pts_in_common(q1, q2)) { michael@0: return fUsed; michael@0: } michael@0: if (only_end_pts_in_common(q2, q1)) { michael@0: return fUsed; michael@0: } michael@0: // see if either quad is really a line michael@0: // FIXME: figure out why reduce step didn't find this earlier michael@0: if (is_linear(q1, q2, this)) { michael@0: return fUsed; michael@0: } michael@0: SkIntersections swapped; michael@0: swapped.setMax(fMax); michael@0: if (is_linear(q2, q1, &swapped)) { michael@0: swapped.swapPts(); michael@0: set(swapped); michael@0: return fUsed; michael@0: } michael@0: SkIntersections copyI(*this); michael@0: lookNearEnd(q1, q2, 0, *this, false, ©I); michael@0: lookNearEnd(q1, q2, 1, *this, false, ©I); michael@0: lookNearEnd(q2, q1, 0, *this, true, ©I); michael@0: lookNearEnd(q2, q1, 1, *this, true, ©I); michael@0: int innerEqual = 0; michael@0: if (copyI.fUsed >= 2) { michael@0: SkASSERT(copyI.fUsed <= 4); michael@0: double width = copyI[0][1] - copyI[0][0]; michael@0: int midEnd = 1; michael@0: for (int index = 2; index < copyI.fUsed; ++index) { michael@0: double testWidth = copyI[0][index] - copyI[0][index - 1]; michael@0: if (testWidth <= width) { michael@0: continue; michael@0: } michael@0: midEnd = index; michael@0: } michael@0: for (int index = 0; index < 2; ++index) { michael@0: double testT = (copyI[0][midEnd] * (index + 1) michael@0: + copyI[0][midEnd - 1] * (2 - index)) / 3; michael@0: SkDPoint testPt1 = q1.ptAtT(testT); michael@0: testT = (copyI[1][midEnd] * (index + 1) + copyI[1][midEnd - 1] * (2 - index)) / 3; michael@0: SkDPoint testPt2 = q2.ptAtT(testT); michael@0: innerEqual += testPt1.approximatelyEqual(testPt2); michael@0: } michael@0: } michael@0: bool expectCoincident = copyI.fUsed >= 2 && innerEqual == 2; michael@0: if (expectCoincident) { michael@0: reset(); michael@0: insertCoincident(copyI[0][0], copyI[1][0], copyI.fPt[0]); michael@0: int last = copyI.fUsed - 1; michael@0: insertCoincident(copyI[0][last], copyI[1][last], copyI.fPt[last]); michael@0: return fUsed; michael@0: } michael@0: SkDQuadImplicit i1(q1); michael@0: SkDQuadImplicit i2(q2); michael@0: int index; michael@0: bool flip1 = q1[2] == q2[0]; michael@0: bool flip2 = q1[0] == q2[2]; michael@0: bool useCubic = q1[0] == q2[0]; michael@0: double roots1[4]; michael@0: int rootCount = findRoots(i2, q1, roots1, useCubic, flip1, 0); michael@0: // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1 michael@0: double roots1Copy[4]; michael@0: int r1Count = addValidRoots(roots1, rootCount, roots1Copy); michael@0: SkDPoint pts1[4]; michael@0: for (index = 0; index < r1Count; ++index) { michael@0: pts1[index] = q1.ptAtT(roots1Copy[index]); michael@0: } michael@0: double roots2[4]; michael@0: int rootCount2 = findRoots(i1, q2, roots2, useCubic, flip2, 0); michael@0: double roots2Copy[4]; michael@0: int r2Count = addValidRoots(roots2, rootCount2, roots2Copy); michael@0: SkDPoint pts2[4]; michael@0: for (index = 0; index < r2Count; ++index) { michael@0: pts2[index] = q2.ptAtT(roots2Copy[index]); michael@0: } michael@0: if (r1Count == r2Count && r1Count <= 1) { michael@0: if (r1Count == 1 && used() == 0) { michael@0: if (pts1[0].approximatelyEqual(pts2[0])) { michael@0: insert(roots1Copy[0], roots2Copy[0], pts1[0]); michael@0: } else if (pts1[0].moreRoughlyEqual(pts2[0])) { michael@0: // experiment: try to find intersection by chasing t michael@0: if (binary_search(q1, q2, roots1Copy, roots2Copy, pts1)) { michael@0: insert(roots1Copy[0], roots2Copy[0], pts1[0]); michael@0: } michael@0: } michael@0: } michael@0: return fUsed; michael@0: } michael@0: int closest[4]; michael@0: double dist[4]; michael@0: bool foundSomething = false; michael@0: for (index = 0; index < r1Count; ++index) { michael@0: dist[index] = DBL_MAX; michael@0: closest[index] = -1; michael@0: for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) { michael@0: if (!pts2[ndex2].approximatelyEqual(pts1[index])) { michael@0: continue; michael@0: } michael@0: double dx = pts2[ndex2].fX - pts1[index].fX; michael@0: double dy = pts2[ndex2].fY - pts1[index].fY; michael@0: double distance = dx * dx + dy * dy; michael@0: if (dist[index] <= distance) { michael@0: continue; michael@0: } michael@0: for (int outer = 0; outer < index; ++outer) { michael@0: if (closest[outer] != ndex2) { michael@0: continue; michael@0: } michael@0: if (dist[outer] < distance) { michael@0: goto next; michael@0: } michael@0: closest[outer] = -1; michael@0: } michael@0: dist[index] = distance; michael@0: closest[index] = ndex2; michael@0: foundSomething = true; michael@0: next: michael@0: ; michael@0: } michael@0: } michael@0: if (r1Count && r2Count && !foundSomething) { michael@0: relaxed_is_linear(&q1, 0, 1, &q2, 0, 1, this); michael@0: return fUsed; michael@0: } michael@0: int used = 0; michael@0: do { michael@0: double lowest = DBL_MAX; michael@0: int lowestIndex = -1; michael@0: for (index = 0; index < r1Count; ++index) { michael@0: if (closest[index] < 0) { michael@0: continue; michael@0: } michael@0: if (roots1Copy[index] < lowest) { michael@0: lowestIndex = index; michael@0: lowest = roots1Copy[index]; michael@0: } michael@0: } michael@0: if (lowestIndex < 0) { michael@0: break; michael@0: } michael@0: insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]], michael@0: pts1[lowestIndex]); michael@0: closest[lowestIndex] = -1; michael@0: } while (++used < r1Count); michael@0: return fUsed; michael@0: }