#include<iostream>
using namespace std;
class Vector3
{
public:
float x;
float y;
float z;
Vector3()
{
x = 0;
y = 0;
z = 0;
}
Vector3(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
#ifndef 연산자 오버로딩
friend Vector3 operator+(const Vector3& a, const Vector3& b) // 벡터 덧셈
{
float Vx = a.x + b.x;
float Vy = a.y + b.y;
float Vz = a.z + b.z;
return Vector3(Vx, Vy, Vz);
}
friend Vector3 operator-(const Vector3& a, const Vector3& b) // 벡터 뺼셈
{
float Vx = a.x - b.x;
float Vy = a.y - b.y;
float Vz = a.z - b.z;
return Vector3(Vx, Vy, Vz);
}
friend float operator*(const Vector3& a, const Vector3& b) //내적 구하기(스칼라 곱)
{
float Vx = a.x * b.x;
float Vy = a.y * b.y;
float Vz = a.z * b.z;
return Vx + Vy + Vz;
}
bool operator==(Vector3 a) // 두 벡터가 같을떄
{
if (x == a.x && y == a.y && z == a.z)
{
return true;
}
else
{
return false;
}
}
#endif // 연산자 오버로딩
static float InnerCos(Vector3 v1, Vector3 v2) //각도 구하기
{
float Cos = (v1 * v2) / ((v1.Magnitude()*v2.Magnitude()));
return Cos;
}
Vector3 Normalrize() // 정규화(단위벡터로 만들기)
{
Vector3 normal;
float magnitude = this->Magnitude();
normal.x = x / magnitude;
normal.y = y / magnitude;
normal.z = z / magnitude;
return normal;
}
float Magnitude() // 벡터 크기
{
auto magnitude = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
return magnitude;
}
static double Seta(float cos) //세타값 구하기 각도(디그리)
{
return acos(cos) * (180 / 3.14);
}
static Vector3 Proj(Vector3 v1, Vector3 v2) // 방향(normal) * 크기(magnitude = Proj(V1)
{
Vector3 result;
result.x = ((v1*v2) / (v2*v2)) * v2.x;
result.y = ((v1*v2) / (v2*v2)) * v2.y;
result.z = ((v1*v2) / (v2*v2)) * v2.z;
return result;
}
static Vector3 Orthogonal_Projection(Vector3 v1, Vector3 proj) // 직교화
{
Vector3 ortho;
ortho = v1 - proj;
return ortho;
}
static Vector3 CrossProduct(Vector3 v1, Vector3 v2) //외적
{
Vector3 cross;
cross.x = v1.y*v2.z - v1.z*v2.y;
cross.y = v1.z*v2.x - v1.x - v2.z;
cross.z = v1.x*v2.y - v1.y*v2.x;
return cross;
}
};
int main()
{
Vector3 u(1, 2, 3);
Vector3 v(1, 2, 1);
cout <<u*v<<" "<<u.Normalrize().x<< " "<<u.Normalrize().y<<" "<<u.Normalrize().z<< " "<<endl;
cout << " 스칼라 " << u * v;
float cos = Vector3::InnerCos(u, v);
cout << " 코사인 " << cos;
cout<< "각도"<< Vector3::Seta(cos)<<endl;
Vector3 pro = Vector3::Proj(u, v);
cout << "Proj(V): "<<pro.x << " " << pro.y << " " << pro.z<<endl;
return 0;
}
'DirectX( 다이렉트 X)' 카테고리의 다른 글
난반사광(Diffuse Reflection)과 정반사광(Specluar Reflection) (0) | 2019.06.08 |
---|---|
C++ 커스텀 행렬 (Custom Vector3 For D3D) (0) | 2019.05.02 |