-
Notifications
You must be signed in to change notification settings - Fork 2
/
segment.cpp
executable file
·35 lines (33 loc) · 1.33 KB
/
segment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "segment.h"
std::pair<bool, Point> SegmentIntersection(const Segment &lhs, const Segment &rhs)
// возвращает статус и точку истинного пересечения отрезков
// (то есть если отрезки неколлинеарны и пересекаются в точке)
{
auto lhs_direction = lhs.GetEnd(1) - lhs.GetEnd(0);
auto rhs_direction = rhs.GetEnd(1) - rhs.GetEnd(0);
if (Equal(SkewProduct(lhs_direction, rhs_direction), 0))
{
return {false, {}};
}
auto slope = SkewProduct(rhs.GetEnd(0) - lhs.GetEnd(0), rhs_direction) /
SkewProduct(lhs_direction, rhs_direction);
auto intersection = lhs.GetEnd(0) + lhs_direction * slope;
if (lhs.IsPointOnSegment(intersection) && rhs.IsPointOnSegment(intersection))
{
return {true, intersection};
}
return {false, {}};
}
bool DoSegmentsHaveCommonPoint(const Segment &lhs, const Segment &rhs)
// возвращает true, если отрезки имеют общую точку, и false иначе
{
if (rhs.IsPointOnSegment(lhs.GetEnd(0)) || rhs.IsPointOnSegment(lhs.GetEnd(1)))
{
return true;
}
if (lhs.IsPointOnSegment(rhs.GetEnd(0)) || lhs.IsPointOnSegment(rhs.GetEnd(1)))
{
return true;
}
return SegmentIntersection(lhs, rhs).first;
}