官术网_书友最值得收藏!

Creating a vector

Vectors will be implemented as structures, not classes. The vector struct will contain an anonymous union that allows the vector's components to be accessed as an array or as individual elements.

To declare the vec3 structure and the function headers, create a new file, vec3.h. Declare the new vec3 structure in this file. The vec3 struct needs three constructors—a default constructor, one that takes each component as an element, and one that takes a pointer to a float array:

#ifndef _H_VEC3_

#define _H_VEC3_

struct vec3 {

    union {

        struct  {

            float x;

            float y;

            float z;

        };

        float v[3];

    };

    inline vec3() : x(0.0f), y(0.0f), z(0.0f) { }

    inline vec3(float _x, float _y, float _z) :

        x(_x), y(_y), z(_z) { }

    inline vec3(float *fv) :

        x(fv[0]), y(fv[1]), z(fv[2]) { }

};

#endif

The anonymous union in the vec3 struct allows data to be accessed using .x, .y, and .z notation, or as a contiguous array using .v. Before moving on to implementing functions that work on the vec3 struct, you need to consider comparing floating point numbers and whether or not to use an epsilon value.

主站蜘蛛池模板: 东港市| 朔州市| 开化县| 团风县| 海盐县| 武山县| 阜阳市| 元氏县| 增城市| 盖州市| 罗山县| 花莲市| 巴彦淖尔市| 文安县| 丹巴县| 游戏| 邵武市| 沐川县| 盐池县| 襄汾县| 辽中县| 九台市| 贡山| 东丰县| 辽阳县| 邯郸市| 兴安盟| 宣威市| 嵩明县| 康马县| 大邑县| 陆丰市| 贵港市| 唐河县| 上饶市| 明水县| 共和县| 河池市| 清徐县| 百色市| 临漳县|