So I have this struct composed of three NSPoint structures.
typedef struct AOTriangle_ {
NSPoint a;
NSPoint b;
NSPoint c;
} AOTriangle;
I would like in some cases to reference the points as a,b,c and other cases as indices into an array.
Like so,
AOTriangle t;
t.a = NSMakePoint(0,0);
t.b = NSMakePoint(3,0);
t.c = NSMakePoint(0,4);
for(int i = 0; i < 3; ++i) {
t[i].x += 5.0;
t[i].y += 5.0;
}
This is the closest I’ve gotten, but you can see it isn’t exactly what I wanted.
Is there a way of doing this in Objective-C? Is the a better way than what I do below of accomplishing something similar – maybe with a union?
typedef struct AOTriangle_ {
NSPoint a;
NSPoint b;
NSPoint c;
} AOTriangle;
AOTriangle t;
t.a = NSMakePoint(0,0);
t.b = NSMakePoint(3,0);
t.c = NSMakePoint(0,4);
NSPoint* t = (NSPoint*)▵
for(int i = 0; i < 3; ++i) {
t[i].x += 5.0;
t[i].y += 5.0;
}
Yes, you can use a “union“. I believe the declaration would go something like this:
A
unionis basically a way of saying “I can refer to the members of this struct as eithera,b, orc, or aspoints[0],points[1], orpoints[2].”And you would use it like this: