Conditions | 10 |
Total Lines | 58 |
Lines | 36 |
Ratio | 62.07 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like GJK.processTetrehedron() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from gem import vector |
||
157 | def processTetrehedron(self, simplex): |
||
158 | '''Determines which Veronoi region of a tetrehedron the origin is in, utilizing the preserved winding of the simplex to eliminate certain regions''' |
||
159 | a = simplex[3] |
||
160 | b = simplex[2] |
||
161 | c = simplex[1] |
||
162 | d = simplex[0] |
||
163 | |||
164 | ac = c - a |
||
165 | ad = d - a |
||
166 | ab = b - a |
||
167 | #bc = c - b |
||
168 | #bd = d - b |
||
169 | |||
170 | acd = vector.cross(ad, ac) |
||
171 | abd = vector.cross(ab, ad) |
||
172 | abc = vector.cross(ac, ab) |
||
173 | |||
174 | aO = -a |
||
175 | |||
176 | if (abc.isInSameDirection(aO)): |
||
177 | View Code Duplication | if (vector.cross(abc, ac).isInSameDirection(aO)): |
|
178 | simplex.remove(b) |
||
179 | simplex.remove(d) |
||
180 | self.direction = vector.cross(vector.cross(ac, aO), ac) |
||
181 | elif(vector.cross(ab, abc).isInSameDirection(aO)): |
||
182 | simplex.remove(c) |
||
183 | simplex.remove(d) |
||
184 | self.direction = vector.cross(vector.cross(ab, aO), ab) |
||
185 | else: |
||
186 | simplex.remove(d) |
||
187 | self.direction = abc |
||
188 | elif (acd.isInSameDirection(aO)): |
||
189 | View Code Duplication | if (vector.cross(acd, ad).isInSameDirection(aO)): |
|
190 | simplex.remove(b) |
||
191 | simplex.remove(c) |
||
192 | self.direction = vector.cross(vector.cross(ad, aO), ad) |
||
193 | elif(vector.cross(ac, acd).isInSameDirection(aO)): |
||
194 | simplex.remove(b) |
||
195 | simplex.remove(d) |
||
196 | self.direction = vector.cross(vector.cross(ac, aO), ac) |
||
197 | else: |
||
198 | simplex.remove(b) |
||
199 | self.direction = acd |
||
200 | View Code Duplication | elif(abd.isInSameDirection(aO)): |
|
201 | if(vector.cross(abd, ab).isInSameDirection(aO)): |
||
202 | simplex.remove(b) |
||
203 | simplex.remove(d) |
||
204 | self.direction = vector.cross(vector.cross(ab, aO), ab) |
||
205 | elif(vector.cross(ab, abd).isInSameDirection(aO)): |
||
206 | simplex.remove(b) |
||
207 | simplex.remove(c) |
||
208 | self.direction = vector.cross(vector.cross(ad, aO), ad) |
||
209 | else: |
||
210 | simplex.remove(c) |
||
211 | self.direction = abd |
||
212 | else: |
||
213 | return True |
||
214 | return False |
||
215 | |||
221 |