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); }
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; }
vector2 project(const vector2& rhs) const {
vector2 r = rhs.normalize();
return r * r.dot(*this);
}
};
정리
- 외적(cross)
- 벡터 AB : (x2-x1, y2-y1), 벡터 AC : (x3-x1, y3-y1)
- = (x2-x1)(y3-y1) - (y2-y1)(x3-x1) = A->C 로 이동, 반시계방향는 양수
- 내적(dot)