Welcome to mirror list, hosted at ThFree Co, Russian Federation.

line2d.cpp « geometry - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e11ce3bf042e7ce81a9359e61e0bdfe77452112b (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "geometry/line2d.hpp"

#include <cmath>
#include <sstream>

using namespace std;

namespace m2
{
namespace
{
bool Collinear(PointD const & a, PointD const & b, double eps)
{
  return fabs(CrossProduct(a, b)) < eps;
}
}  // namespace

string DebugPrint(Line2D const & line)
{
  ostringstream os;
  os << "Line2D [ ";
  os << "point: " << DebugPrint(line.m_point) << ", ";
  os << "direction: " << DebugPrint(line.m_direction);
  os << " ]";
  return os.str();
}

// static
LineIntersector::Result LineIntersector::Intersect(Line2D const & lhs, Line2D const & rhs,
                                                   double eps)
{
  auto const & a = lhs.m_point;
  auto const & ab = lhs.m_direction;

  auto const & c = rhs.m_point;
  auto const & cd = rhs.m_direction;

  if (Collinear(ab, cd, eps))
  {
    if (Collinear(c - a, cd, eps))
      return Result(Result::Type::Infinity);
    return Result(Result::Type::Zero);
  }

  auto const ac = c - a;

  auto const n = CrossProduct(ac, cd);
  auto const d = CrossProduct(ab, cd);
  auto const scale = n / d;

  return Result(a + ab * scale);
}

string DebugPrint(LineIntersector::Result::Type type)
{
  using Type = LineIntersector::Result::Type;

  switch (type)
  {
  case Type::Zero: return "Zero";
  case Type::One: return "One";
  case Type::Infinity: return "Infinity";
  }
  UNREACHABLE();
}

string DebugPrint(LineIntersector::Result const & result)
{
  ostringstream os;
  os << "Result [";
  if (result.m_type == LineIntersector::Result::Type::One)
    os << DebugPrint(result.m_point);
  else
    os << DebugPrint(result.m_type);
  os << "]";
  return os.str();
}
}  // namespace m2