(알고리즘) 기하(선분과 선분의 교차)

struct vector2{
public:
double x, y;

public:
explicit vector2(double _x, double _y) :x(_x), y(_y){}

bool operator<(const vector2& rhs)const{
return x != rhs.x ? x < rhs.x : y < rhs.y;
}
vector2 operator-(const vector2& rhs){
return vector2(x - rhs.x, y - rhs.y);
}
vector2 operator*(const vector2& rhs){
return vector2(x*rhs.x, y*rhs.y);
}
double cross(const vector2& rhs)const{
return x*rhs.y - y*rhs.x;
}
//벡터(x,y)의 기이
double norm()
{return hypot(x,y);}
//방향이 같은 단위벡터를 반환한다
//0일경우 반환하지 않는다.
vector2 normalize()const
{
 return vector2(x/norm(),y/norm());
}
//이 벡터를 rhs정사영항 결과
vecotor2 project(const vector2& rhs)
{
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);
}
vector<vector2> cor;
bool segmentIntersection(vector2 a, vector2 b, vector2 c, vector2 d)
{
double ab = ccw(a, b, c)*ccw(a, b, d);
double cd = ccw(c, d, a)*ccw(c, d, b);
if (ab == 0 && cd == 0)
{
if (b < a)swap(a, b);
if (d < c)swap(c, d);
return!(b < c || d < a);
}
return ab <= 0 && cd <= 0;
}

댓글

이 블로그의 인기 게시물

(네트워크)폴링방식 vs 롱 폴링방식

(ElasticSearch) 결과에서 순서 정렬

(18장) WebSocekt과 STOMP를 사용하여 메시징하기