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

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.

主站蜘蛛池模板: 虹口区| 莱州市| 卢氏县| 永吉县| 呼伦贝尔市| 阜宁县| 西乌珠穆沁旗| 锡林浩特市| 四平市| 无极县| 隆尧县| 钟祥市| 林芝县| 平泉县| 上虞市| 通海县| 黄浦区| 色达县| 龙里县| 县级市| 深泽县| 磐安县| 许昌县| 孝义市| 临桂县| 深泽县| 南丰县| 交城县| 南郑县| 东阳市| 桃江县| 紫阳县| 右玉县| 昌宁县| 多伦县| 蒙山县| 水城县| 洛南县| 和平区| 潞西市| 昌江|