views:

323

answers:

4

Cheers,

I like strict typing in C. Therefore, I don't want to store a 2D vector of floats if I specifically need integers. Is there an Apple-provided equivalent of CGPoint which stores data as integers?

I've implemented my type Vector2i and its companion function Vector2iMake() à la CGPoint, but something deep in me screams that Apple was there already.


Updating to explain.

I need a datatype that will store coordinates in a board game. These are most definitely integers. Same would be if I were to implement a tile-based turn based strategy, or a tile-based RPG.

+2  A: 

According to iPhone Application Programming Guide, all provided points are float-based. And when you use them to work with the screen (eventually expecting integers), you should anyway use floats for independence from screen resolution and etc.

kpower
+1 Floats are used expressly because integers will not work across devices and throughout transforms. A CGPoint is less a fixed point than it is more like a two dimensional percentage of the display area. When you start transforming objects by scaling, rotating, skewing, etc the idea of a fixed point representing a single pixel on the screen disappears completely. You're left with nothing but relative location within the transformed region.
TechZen
While your answer is valid, it does not address my question, since I do NOT intend to use this for graphical points -- I need integer vectors for things like coordinates in a board game. I certainly need `float`-based vectors for graphics, and use them extensively there. Updating question.
Ivan Vučica
Ok. The answer is: "probably, not". And answer above is the explanation why.Finally you can either define your own struct (as you made) or use something like NSIndexSet.
kpower
Sadly, NSIndexSet contains a set of unique integers.
Ivan Vučica
+3  A: 

If you happen to be representing your game board as objects stored in non-sparse nested arrays, then you may want to consider subclassing NSIndexPath, or using it directly.

From the class reference:

The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path.

Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node.

cduhn
Nice. I didn't think of this. With the `initWithIndexes:length:` method, it looks quite usable. I have to wait 37 more minutes before accept, but if nothing more Obj-C-ish comes around, I think this is the best way to move on.
Ivan Vučica
Enjoy the bounty :)
Ivan Vučica
+3  A: 

(to directly answer the question...)

I am not aware of a "NSIntegerPoint", but it wouldn't be difficult to make one:

struct NSIntegerPoint {
   NSInteger x;
   NSInteger y;
};

Along with stuff like:

CG_INLINE NSIntegerPoint
NSIntegerPointMake(NSInteger x, NSInteger y)
{
  NSIntegerPoint p; p.x = x; p.y = y; return p;
}

CG_INLINE bool
__NSIntegerPointEqualToPoint(NSIntegerPoint point1, NSIntegerPoint point2)
{
  return point1.x == point2.x && point1.y == point2.y;
}
#define NSIntegerPointEqualToPoint __NSIntegerPointEqualToPoint
Dave DeLong
Hi,I have already implemented something along these lines. Thanks, though!
Ivan Vučica
A: 

You could use int vectors ?

http://developer.apple.com/mac/library/documentation/Performance/Conceptual/vecLib/Reference/reference.html#//apple_ref/doc/uid/TP40002498-CH1g-TPXREF103

T.

tomdemuyt
While it (obviously) exists on Mac OS X, the only relevant reference to iPhone I found does not come from Apple and in fact mentions veclib.framework being a private Apple framework.
Ivan Vučica