patternjavaMinor
Improving readability of 3d math in Java
Viewed 0 times
readabilitymathimprovingjava
Problem
Doing graphics in Java, I find myself writing lines like these regularly:
It gets even worse when I'm reusing objects.
I've tried coping using a combination of these two methods:
Disadvantages: Extra noise. 'Translation' comments can look like actual code that's been commented out. There's no getting around the real code being hard to read.
Disadvantages: Some calculations don't chain up quite as nicely. When different return types are involved (e.g. matrices, quaternions, scalars), chaining is not always logical or possible.
Any other suggestions? Switching language is not really viable.
Vector3f b = mid.add( a.mult(yOffset + length / 2) ).add( up.cross(a).mult(xOffset) );It gets even worse when I'm reusing objects.
Vector3f vTemp = VarPool.getVector3f();
Vector3f b = VarPool.getVector3f();
b.set(mid).addMutable( a.multAndStore(yOffset + length / 2, vTemp) ).addMutable( up.crossAndStore(a, vTemp).multMutable(xOffset) );
//...do some useful computation...
VarPool.recycle(vTemp, b);I've tried coping using a combination of these two methods:
- Place 'translations' in comments above dense lines
//i.e. Vector3f b = ( mid + a * (yOffset + length / 2) + (up X a) * xOffset );
//this calculates such and such
b.set(mid).addMutable( a.multAndStore(yOffset + length / 2, vTemp) ).addMutable( up.crossAndStore(a, vTemp).multMutable(xOffset) );Disadvantages: Extra noise. 'Translation' comments can look like actual code that's been commented out. There's no getting around the real code being hard to read.
- Break dense chains into more readable lines
b
.set(mid)
.addMutable( a.multAndStore(yOffset + length / 2, vTemp) )
.addMutable( ( vTemp = up.crossAndStore(a, vTemp)
.multMutable(xOffset) ) );Disadvantages: Some calculations don't chain up quite as nicely. When different return types are involved (e.g. matrices, quaternions, scalars), chaining is not always logical or possible.
Any other suggestions? Switching language is not really viable.
Solution
@DaveNewton Not necessarily, sometimes it's Groovy :-)
Advantage
Disadvantage
Advantage
- code/syntax compatible with java.
- Operator overrides allow you to write your code so it looks more like your comment i.e. operators rather than methods
Disadvantage
- Groovy is slower than java (but this can be reduced with Groovy++)
Context
StackExchange Code Review Q#13275, answer score: 3
Revisions (0)
No revisions yet.