I have a quick question. I need a way to represent orientation in degrees. I made a class which automatically changes negative angles into positive ones (adds 360 until >0) and makes sure they are under 360 (subtracts 360 until <360).
Now I realized I also need a class to represent angular movement, which must be in the range ]-360;360[
I don't think custom types with the unsigned keyword is a thing, but I'm just looking for a better way to do this then making two classes. Or would you just create a class which inherits from angle and overloads a few methods?
Or I could just use floats to represent rotation, because testing if an object does more than 1 turn per second isn't really required.
Inheritance is the clean and by-the-book way to do it.
You could, if you would prefer to avoid making two different types, add a constructor which takes a boolean as input that defaults to false. If true, its bounds will be (-360,360), if false, [0,360).
> create a class which inherits from angle and overloads a few methods
>> Inheritance is the clean and by-the-book way to do it.
I'm not sure to follow. `angle' is a class that has a value clamped to the range [0;360]
Then you want `angular movement' that has a range that is not included in the range of `angle'
¿wouldn't that violate LSP? `Postconditions cannot be weakened in a subtype.'
as you can have an `angular movement' with a negative value, which is forbidden for an `angle'
> in the range ]-360;360[
excuse my ignorance, ¿valid range is outside that region, i.e. (-infty; -360] [360; infty) ? (so 0 is invalid)
I don't think this is what Albatross meant (I could be wrong though).
if you would prefer to avoid making two different types
This implies (to me at least) that inheritance is applied in a way where you have two subclasses of
an Angle class, each one imposing different constraints on the contained value. Something like this:
]-360;360[ means between -360 and +360, but excluding -360 and +360. Sorry for that. I didn't say from 0 to 359 because I'm using floats, not ints.
I think I'll use the angle class where I need it to represent an orientation and I'll makes something like typedef float rotation for the angles which can be negative. The engine just won't check if a rotation is more than one turn per tick. It's not that big of a deal since you usually add an additional limit for each object of your game. Thanks for your ideas though!
I love posting, especially when I make the effort to be logical, succinct, and useful, carefully observing underlying mathematical properties, because it is clear that such effort isn't wasted.
I paid attention when I studied modular arithmetic.
@Duoas: I don't understand what you are trying to say, please be more clearer.
The operations in `angle' are resolved and good, nobody complains about that.