假设我有一个GLKVector3,并希望只读取x和y值作为CGPoints - 我该怎么做?
答案 0 :(得分:3)
在GLKVector3
doc中,有类型定义:
union _GLKVector3
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float s, t, p; };
float v[3];
};
typedef union _GLKVector3 GLKVector3;
有3种选择:
GLKVector3
的{{1}}属性v
数组float[3]
即:
{x,y,z}
对于矢量类型的不同用途,还有浮点属性GLKVector3 vector;
...
float x = vector.v[0];
float y = vector.v[1];
float z = vector.v[2];
CGPoint p = CGPointMake(x,y);
或不太相关的x,y,z
或r,g,b
:
s,t,p
答案 1 :(得分:1)
GLKVector3
被声明为
union _GLKVector3
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float s, t, p; };
float v[3];
};
typedef union _GLKVector3 GLKVector3;
因此,最简单,最易读的转换方式是:
GLKVector3 someVector;
…
CGPoint somePoint = CGPointMake(someVector.x,someVector.y);
但请注意,CGPoint
由CGFloat
组成,在64位环境中可能是双倍。