vector2 클래스

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;

const double PI = 2.0 * acos(0.0);

struct vector2 {
    double x, y;
    vector2(double x_ = 0, double y_ = 0) : x(x_), y(y_) {}

    // 두 벡터가 같은 경우
    bool operator == (const vector2& rhs) const { return x == rhs.x && y == rhs.y; }

    // 벡터의 덧셈과 뺄셈, 단항 연산자의 구현
    vector2 operator + (const vector2& rhs) const { return vector2(x + rhs.x, y + rhs.y); }
    vector2 operator - (const vector2& rhs) const { return vector2(x - rhs.x, y - rhs.y); }
    vector2 operator - () const { return vector2(-x, -y); }
    bool operator < (const vector2& rhs) const
    {
        return x != rhs.x? x < rhs.x: y < rhs.y;
    }
    // 스칼라로 곱셈
    vector2 operator * (double rhs) const { return vector2(x * rhs, y * rhs); }

    // 벡터의 길이를 반환한다
    double norm() const { return hypot(x, y); }

    // 방향이 같은 단위 벡터 (unit vector) 를 반환한다
    vector2 normalize() const { double n = norm(); return vector2(x / n, y / n); }

    // 내적/외적의 구현
    double dot(const vector2& rhs) const { return x * rhs.x + y * rhs.y; }
    double cross(const vector2& rhs) const { return x * rhs.y - rhs.x * y; }

    // 이 벡터를 rhs 에 사영한 결과
    vector2 project(const vector2& rhs) const {
        vector2 r = rhs.normalize();
        return r * r.dot(*this);
    }
};

double ccw(vector2 a, vector2 b)
{
    return a.cross(b);
}
double ccw(vector2 p, vector2 a, vector2 b)
{
    return ccw(a-p, b-p);
}
  • 외적(cross)
  • norm

results matching ""

    No results matching ""