Wed, 01 Jan 2014 00:25:01 +0200
- updated copyright year. Best wishes for 2014!
596
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
600
209e3f1f7b2c
- updated copyright year. Best wishes for 2014!
Santeri Piippo <crimsondusk64@gmail.com>
parents:
597
diff
changeset
|
3 | * Copyright (C) 2013, 2014 Santeri Piippo |
596
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | */ |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | #include "ringFinder.h" |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include "../misc.h" |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | RingFinder g_RingFinder; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | // ============================================================================= |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | // This is the main algorithm of the ring finder. It tries to use math to find |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | // the one ring between r0 and r1. If it fails (the ring number is non-integral), |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | // it finds an intermediate radius (ceil of the ring number times scale) and |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | // splits the radius at this point, calling this function again to try find the |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | // rings between r0 - r and r - r1. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | // |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | // This does not always yield into usable results. If at some point r == r0 or |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | // r == r1, there is no hope of finding the rings, at least with this algorithm, |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | // as it would fall into an infinite recursion. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | // ----------------------------------------------------------------------------- |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | bool RingFinder::findRingsRecursor (double r0, double r1, Solution& currentSolution) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | { // Don't recurse too deep. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | if (m_stack >= 5) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | return false; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | // Find the scale and number of a ring between r1 and r0. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | assert (r1 >= r0); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | double scale = r1 - r0; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | double num = r0 / scale; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | // If the ring number is integral, we have found a fitting ring to r0 -> r1! |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | if (isInteger (num)) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | { Component cmp; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | cmp.scale = scale; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | cmp.num = (int) round (num); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | currentSolution.addComponent (cmp); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | // If we're still at the first recursion, this is the only |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | // ring and there's nothing left to do. Guess we found the winner. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | if (m_stack == 0) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | { m_solutions.push_back (currentSolution); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | return true; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | else |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | { // Try find solutions by splitting the ring in various positions. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | if (isZero (r1 - r0)) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | return false; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | double interval; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | // Determine interval. The smaller delta between radii, the more precise |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | // interval should be used. We can't really use a 0.5 increment when |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | // calculating rings to 10 -> 105... that would take ages to process! |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | if (r1 - r0 < 0.5) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | interval = 0.1; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | else if (r1 - r0 < 10) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | interval = 0.5; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | else if (r1 - r0 < 50) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | interval = 1; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | else |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | interval = 5; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | // Now go through possible splits and try find rings for both segments. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | for (double r = r0 + interval; r < r1; r += interval) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | { Solution sol = currentSolution; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | m_stack++; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | bool res = findRingsRecursor (r0, r, sol) && findRingsRecursor (r, r1, sol); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | m_stack--; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | if (res) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | { // We succeeded in finding radii for this segment. If the stack is 0, this |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | // is the first recursion to this function. Thus there are no more ring segments |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | // to process and we can add the solution. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | // |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | // If not, when this function ends, it will be called again with more arguments. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | // Accept the solution to this segment by setting currentSolution to sol, and |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | // return true to continue processing. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | if (m_stack == 0) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | m_solutions.push_back (sol); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | else |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | { currentSolution = sol; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | return true; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | return false; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | return true; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | // ============================================================================= |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | // Main function. Call this with r0 and r1. If this returns true, use bestSolution |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | // for the solution that was presented. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | // ----------------------------------------------------------------------------- |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | bool RingFinder::findRings (double r0, double r1) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | { m_solutions.clear(); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | Solution sol; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | // Recurse in and try find solutions. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | findRingsRecursor (r0, r1, sol); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | // Compare the solutions and find the best one. The solution class has an operator> |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | // overload to compare two solutions. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | m_bestSolution = null; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | for (QVector<Solution>::iterator solp = m_solutions.begin(); solp != m_solutions.end(); ++solp) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | { const Solution& sol = *solp; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | if (m_bestSolution == null || sol > *m_bestSolution) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | m_bestSolution = / |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | return (m_bestSolution != null); |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
134 | // ============================================================================= |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | // ----------------------------------------------------------------------------- |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | bool RingFinder::Solution::operator> (const RingFinder::Solution& other) const |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | { // If this solution has less components than the other one, this one |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | // is definitely better. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | if (getComponents().size() < other.getComponents().size()) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | return true; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | // vice versa |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | if (other.getComponents().size() < getComponents().size()) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | return false; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | // Calculate the maximum ring number. Since the solutions have equal |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | // ring counts, the solutions with lesser maximum rings should result |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | // in cleaner code and less new primitives, right? |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | int maxA = 0, |
597 | 150 | maxB = 0; |
596
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | for (int i = 0; i < getComponents().size(); ++i) |
597 | 153 | { maxA = max (getComponents()[i].num, maxA); |
154 | maxB = max (other.getComponents()[i].num, maxB); | |
596
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | } |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | if (maxA < maxB) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | return true; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | if (maxB < maxA) |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | return false; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | // Solutions have equal rings and equal maximum ring numbers. Let's |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | // just say this one is better, at this point it does not matter which |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | // one is chosen. |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
166 | return true; |
43c233e91447
- split the ring finder to separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
167 | } |