What data structures can efficiently store 2-d “grid” data?

Here are a few approaches. I’ll (try to) illustrate these examples with a representation of a 3×3 grid. The flat array +—+—+—+—+—+—+—+—+—+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | +—+—+—+—+—+—+—+—+—+ a[row*width + column] To access elements on the left or right, subtract or … Read more

Why do trigonometric functions give a seemingly incorrect result? [duplicate]

Why is this happening? Trigonometric functions generally expect units in radians, while 90 is in degrees. This also applies for other functions such as cosine and tangent, not just the sine function. Most programming languages require trigonometric functions to provide their arguments in radians. This simplifies many things, especially on the implementation side as code … Read more

Programming style: should you return early if a guard condition is not satisfied?

I prefer the first style, except that I wouldn’t create a variable when there is no need for it. I’d do this: // Style 3 public SomeType aMethod() { if (!guardCondition()) { return null; } SomeType result = new SomeType(); doStuffToResult(result); doMoreStuffToResult(result); return result; }

Should I always/ever/never initialize object fields to default values?

Think long term maintenance. Keep the code as explicit as possible. Don’t rely on language specific ways to initialize if you don’t have to. Maybe a newer version of the language will work differently? Future programmers will thank you. Management will thank you. Why obfuscate things even the slightest? Update: Future maintainers may come from … Read more

Is there an algorithm for converting quaternion rotations to Euler angle rotations?

This looks like a classic case of old technology being overlooked – I managed to dig out a copy of Graphics Gems IV from the garage and it looks like Ken Shoemake has not only an algorithm for converting from Euler angles of arbitrary rotation order, but also answers most of my other questions on … Read more

How do I assess the hash collision probability?

Equal hash means equal file, unless someone malicious is messing around with your files and injecting collisions. (this could be the case if they are downloading stuff from the internet) If that is the case go for a SHA2 based function. There are no accidental MD5 collisions, 1,47×10-29 is a really really really small number. … Read more

How exactly do “Objects communicate with each other by passing messages”?

If we are talking about OOP than the term “message passing” comes from Smalltalk. In a few words the Smalltalk basic principles are: Object is the basic unit of object-oriented system. Objects have their own state. Objects communicate by sending and receiving messages. If you are interested in Smalltalk take a look at Pharo or … Read more