API가 수레 대신에 수레 구조를 허용하는 큰 이유 중 하나는 가독성입니다. 3D 그래픽을 사용하는 모든 응용 프로그램에서 확실히 많은 벡터로 작업해야합니다. 이 방법이 더 읽기 쉽고 유지 보수입니다 : 이것보다
D3DXVECTOR Scale(D3DXVECTOR a, double scale)
{
return D3DXVECTOR(a.x * scale, a.y * scale, a.z * scale);
}
class MyClass
{
public:
void DoSomething()
{
a = Scale(b, 2.0);
::D3DXVec3Cross(&out, &a, &b);
}
private:
D3DXVECTOR3 out;
D3DXVECTOR3 a;
D3DXVECTOR3 b;
};
: 즉
class MyClass
{
public:
void DoSomething()
{
float scale = 2.0;
a1 = Scale(b1, scale);
a2 = Scale(b2, scale);
a3 = Scale(b3, scale);
// Hypothetical D3DX function accepting floats directly
::D3DXVec3Cross(&out1, &out2, &out3, a1, a2, a3, b1, b2, b3);
}
private:
float out1;
float out2;
float out3;
float a1;
float a2;
float a3;
float b1;
float b2;
float b3;
};
, 그것은 자기 문서화 코드 수 있습니다. 벡터를 선언 할 필요가있을 때 입력 할 시간을 줄이는 것은 말할 것도없이 (D3DXVECTOR
하나는 세 개의 별도 문자 float
과 반대 임).
일반적으로 3D 그래픽 라이브러리/프레임 워크는 다양한 플랫폼과 구성에서 이식을 위해 자체 벡터 유형과 부동 소수점 형식을 정의합니다. 이러한 유형을 다음과 같이 간단히 정의 할 수 있습니다.
namespace My3DLib
{
typedef MyFloat float;
struct MyVector
{
MyFloat x;
MyFloat y;
MyFloat z;
};
MyVector Scale(MyVector a, MyFloat b);
MyFloat DotProduct(MyVector a, MyVector b);
MyVector CrossProduct(MyVector a, MyVector b);
// ...
}