Skip to content

Commit

Permalink
Create line_cross.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Jul 6, 2024
1 parent 2208598 commit 7fedbab
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ps/Geometry/CCW/line_cross.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//overflow를 대비해서 cross 부분은 양수 또는 음수에 따라 1, 0, -1로 전처리해주는게 좋습니다.
template <typename T>
bool line_cross(const pt<T>& a, const pt<T>& b, const pt<T>& c, const pt<T>& d) {
T abc = a.cross(b, c), abd = a.cross(b, d);
T cda = c.cross(d, a), cdb = c.cross(d, b);

if (abc * abd <= 0 && cda * cdb <= 0) {
if (abc * abd == 0 && cda * cdb == 0) {
if (std::max(a.x, b.x) >= std::min(c.x, d.x) && std::max(c.x, d.x) >= std::min(a.x, b.x) &&
std::max(a.y, b.y) >= std::min(c.y, d.y) && std::max(c.y, d.y) >= std::min(a.y, b.y)) return true;
return false;
}
return true;
}
return false;
}

0 comments on commit 7fedbab

Please sign in to comment.