【问题标题】:Extracting X and Y from a GLKVector3 ? iOS从 GLKVector3 中提取 X 和 Y ? iOS
【发布时间】:2013-10-20 18:10:38
【问题描述】:

假设我有一个 GLKVector3 并且只想读取 x 和 y 值作为 CGPoints - 我该怎么做?

【问题讨论】:

    标签: ios objective-c opengl-es glkit


    【解决方案1】:

    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个选项:

    GLKVector3v 属性是{x,y,z}float[3] 数组

    即:

    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,bs,t,p 用于向量类型的不同用途:

    CGPoint p = CGPointMake(vector.x,vector.y);
    

    【讨论】:

      【解决方案2】:

      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);
      

      但请注意,CGPointCGFloats 组成,在 64 位环境中可能是双精度数。

      【讨论】:

      • 关于 64 位的好点子 - 如果您需要转换两种方式和/或写出可能在不同设备架构上读取的数据,您需要小心。
      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      相关资源
      最近更新 更多