-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.cpp
52 lines (41 loc) · 921 Bytes
/
Line.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
template <class T>
class Line
{
public:
Point<T> u, v;
Line(void)
{
}
Line(const Point<T> & u, const Point<T> & v) : u(u), v(v)
{
}
Line(const T & px, const T & py, const T & qx, const T & qy) : u(px, py), v(qx, qy)
{
}
T operator * (const Line & rhs) const
{
return (v - u) * (rhs.v - rhs.u);
}
T operator & (const Line & rhs) const
{
return (v - u) & (rhs.v - rhs.u);
}
bool operator < (const Line & rhs) const
{
int vs = sgn((v - u) * (rhs.v - rhs.u)), ss = sgn((v - u) & (rhs.v - rhs.u));
if (vs == 0 && ss > 0)
return on_left(rhs.u, false);
else
return v - u < rhs.v - rhs.u;
}
bool on_left(const Point<T> & p, const bool & inclusive = true) const
{
int s = sgn(p.cross(u, v));
return s == 0 ? inclusive : s > 0;
}
bool on_right(const Point<T> & p, const bool & inclusive = true) const
{
int s = sgn(p.cross(u, v));
return s == 0 ? inclusive : s < 0;
}
};