Self-define float4 vector

本文介绍了浮点数向量结构float2、float3及float4的定义与使用方法,包括初始化、运算符重载及点乘运算实现,并探讨了这些结构在3D图形和通用计算中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
structfloat4
{
 float4() {};
 float4(floats) : x(s), y(s), z(s), w(s) {}
 float4(floatx,floaty,floatz,floatw) : x(x), y(y), z(z), w(w) {}
 floatx, y, z, w;
 
 inlinefloat4 operator*(floats)const{returnfloat4(x*s, y*s, z*s, w*s); }
 inlinefloat4 operator+(constfloat4& a) const{returnfloat4(x+a.x, y+a.y, z+a.z, w+a.w); }
};
 
// dot product of two float4 vectors
inlinefloatdot(constfloat4& a, constfloat4& b) {
 returna.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
structfloat3
{
 float3() {};
 float3(floats) : x(s), y(s), z(s) {}
 float3(floatx,floaty,floatz) : x(x), y(y), z(z) {}
 floatx, y, z;
 
 inlinefloat3 operator*(floats)const{returnfloat3(x*s, y*s, z*s); }
 inlinefloat3 operator+(constfloat3& a) const{returnfloat3(x+a.x, y+a.y, z+a.z); }
};
 
// dot product of two float3 vectors
inlinefloatdot(constfloat3& a, constfloat3& b) {
 returna.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
structfloat3
{
 float2() {};
 float2(floats) : x(s), y(s) {}
 float2(floatx,floaty) : x(x), y(y) {}
 floatx, y;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值