Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of CGPoint with integers?

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.

like image 389
Ivan Vučica Avatar asked Apr 10 '10 10:04

Ivan Vučica


3 Answers

(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
like image 122
Dave DeLong Avatar answered Sep 28 '22 04:09

Dave DeLong


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.

like image 41
cduhn Avatar answered Sep 28 '22 04:09

cduhn


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.

like image 29
kpower Avatar answered Sep 28 '22 05:09

kpower