314 // This does not always yield into usable results. If at some point r == r0 or |
314 // This does not always yield into usable results. If at some point r == r0 or |
315 // r == r1, there is no hope of finding the rings, at least with this algorithm, |
315 // r == r1, there is no hope of finding the rings, at least with this algorithm, |
316 // as it would fall into an infinite recursion. |
316 // as it would fall into an infinite recursion. |
317 // ----------------------------------------------------------------------------- |
317 // ----------------------------------------------------------------------------- |
318 bool RingFinder::findRings (double r0, double r1) |
318 bool RingFinder::findRings (double r0, double r1) |
|
319 { m_solution.clear(); |
|
320 return findRingsRecursor (r0, r1); |
|
321 } |
|
322 |
|
323 bool RingFinder::findRingsRecursor (double r0, double r1) |
319 { // Find the scale and number of a ring between r1 and r0. |
324 { // Find the scale and number of a ring between r1 and r0. |
320 double scale = r1 - r0; |
325 double scale = r1 - r0; |
321 double num = r0 / scale; |
326 double num = r0 / scale; |
|
327 print ("r0: %1, r1: %2, scale: %3, num: %4\n", r0, r1, scale, num); |
322 |
328 |
323 // If the ring number is integral, we have found a fitting ring to r0 -> r1! |
329 // If the ring number is integral, we have found a fitting ring to r0 -> r1! |
324 if (isInteger (num)) |
330 if (isInteger (num)) |
325 { SolutionComponent cmp; |
331 { SolutionComponent cmp; |
326 cmp.scale = scale; |
332 cmp.scale = scale; |
328 m_solution << cmp; |
334 m_solution << cmp; |
329 } |
335 } |
330 else |
336 else |
331 { // If not, find an intermediate <r> between the radii |
337 { // If not, find an intermediate <r> between the radii |
332 double r = ceil (num) * scale; |
338 double r = ceil (num) * scale; |
|
339 print ("\tr: %1\n", r); |
333 |
340 |
334 // If r is the same as r0 or r1, we simply cannot find any rings between |
341 // If r is the same as r0 or r1, we simply cannot find any rings between |
335 // r0 and r1. Stop and return failure. |
342 // r0 and r1. Stop and return failure. |
336 if (isZero (r0 - r) || isZero (r1 - r)) |
343 if (isZero (r0 - r) || isZero (r1 - r)) |
|
344 { print ("failure!\n"); |
337 return false; |
345 return false; |
|
346 } |
338 |
347 |
339 // Split this ring into r0 -> r and r -> r1. Recurse to possibly find |
348 // Split this ring into r0 -> r and r -> r1. Recurse to possibly find |
340 // the rings for these. If either recurse fails, the entire algorithm |
349 // the rings for these. If either recurse fails, the entire algorithm |
341 // fails as well. |
350 // fails as well. |
342 if (!findRings (r0, r) || !findRings (r, r1)) |
351 if (!findRingsRecursor (r0, r) || !findRingsRecursor (r, r1)) |
343 return false; |
352 return false; |
344 } |
353 } |
345 |
354 |
346 // The algorithm did not fail, thus we succeeded! |
355 // The algorithm did not fail, thus we succeeded! |
347 return true; |
356 return true; |