#pragma once

#include <math.h>

struct VEC2

{

public:

VEC2() : x(0.0f), y(0.0f) {}

VEC2(float _x, float _y) : x(_x), y(_y) {}

VEC2(const VEC2& other) : x(other.x), y(other.y) {}


public:

float x;

float y;


public:

void operator+=(const VEC2& other)

{

x += other.x;

y += other.y;

}

void operator-=(const VEC2& other)

{

x -= other.x;

y -= other.y;

}

void operator*=(const float& value)

{

x *= value;

y *= value;

}

void operator/=(const float& value)

{

x /= value;

y /= value;

}


public:

VEC2 operator+(const VEC2& other)

{

VEC2 Return;

Return.x = x + other.x;

Return.y = y + other.y;

return Return;

}

VEC2 operator-(const VEC2& other)

{

VEC2 Return;

Return.x = x - other.x;

Return.y = y - other.y;

return Return;

}

VEC2 operator*(const float& value)

{

VEC2 Return;

Return.x = x * value;

Return.y = y * value;

return Return;

}

VEC2 operator/(const float& value)

{

VEC2 Return;

Return.x = x / value;

Return.y = y / value;

return Return;

}


public:

VEC2& rotate(const float& value)

{

float presentRad = theta();

float len = scalar();

float targetRad = presentRad + value;


x = cosf(targetRad) * len;

y = sinf(targetRad) * len;


return *this;

}


public:

float theta()

{

return atan2f(y, x);

}


float scalar()

{

return sqrtf(x * x + y * y);

}


VEC2 dir()

{

float theta = this->theta();

return { cosf(theta), sinf(theta) };

}

};



유용한 함수가 몇 개 있습니다.


theta 함수: 각 반환 (원의 사분면 기준)

scalar 함수: 벡터의 길이 반환

dir 함수: 방향 벡터로 반환


rotate 함수: 현재의 벡터를 인자값 만큼 돌림

여기선 내부의 값을 바꾸는 함수이기 때문에 주의해야 함


필요하면 더 추가할 수도 있겠지만

아직까지 쓰면서 불편함은 없었습니다



사용 예시) 어떤 벡터 A가 있는데 직각이고 길이가 30인 벡터 B를 만들어야 함

VEC2 B = A.dir().rotate(PI / 2) * 30.0f;

// rotate는 내부 값을 바꾸지만 여기선 dir 반환값을 바꾼 거라 상관 없음


이런 식으로 자주 썼습니다

클래스 있는 언어면 조금만 바꿔도 쓸 수 있을 듯요