Self-define float4 vector

The float4 structure encapsulates 4 float values and can be used to represent a 4-element vector or a row of a 4-column matrix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct float4
{
  float4() {};
  float4( float s) : x(s), y(s), z(s), w(s) {}
  float4( float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
  float x, y, z, w;
 
  inline float4 operator*( float s) const { return float4(x*s, y*s, z*s, w*s); }
  inline float4 operator+( const float4& a) const { return float4(x+a.x, y+a.y, z+a.z, w+a.w); }
};
 
// dot product of two float4 vectors
inline float dot( const float4& a, const float4& b) {
  return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}

Similar to the float4 datatype, we define another common type: float3. It can be used, for example, for representing 3D vectors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct float3
{
  float3() {};
  float3( float s) : x(s), y(s), z(s) {}
  float3( float x, float y, float z) : x(x), y(y), z(z) {}
  float x, y, z;
 
  inline float3 operator*( float s) const { return float3(x*s, y*s, z*s); }
  inline float3 operator+( const float3& a) const { return float3(x+a.x, y+a.y, z+a.z); }
};
 
// dot product of two float3 vectors
inline float dot( const float3& a, const float3& b) {
  return a.x * b.x + a.y * b.y + a.z * b.z;
}

The float2 datatype contains 2 float elements:

1
2
3
4
5
6
7
struct float3
{
  float2() {};
  float2( float s) : x(s), y(s) {}
  float2( float x, float y) : x(x), y(y) {}
  float x, y;
};

你可能感兴趣的:(C++/C)