-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoline.cpp
85 lines (75 loc) · 2.13 KB
/
twoline.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
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
79
80
81
82
83
84
85
/*************************************************************************
> File Name: twoline.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Thu Aug 6 17:03:30 2015
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
class Point
{
public:
float x;
float y;
Point(float _x, float _y):x(_x),y(_y){}
Point operator +(const Point &that) const
{
return Point(x+that.x, y+that.y);
}
Point operator -(const Point &that) const
{
return Point(x-that.x, y-that.y);
}
Point& operator =(const Point &that)
{
if (this == &that)
return *this;
x = that.x;
y = that.y;
return *this;
}
};
float crossProduct(const Point &p1, const Point &p2)
{
return p1.x*p2.y - p2.x*p1.y;
}
float direction(const Point &pi, const Point &pj, const Point &pk)
{
return crossProduct(pk-pi, pj-pi);
}
bool onSegmant(const Point &pi, const Point &pj, const Point &pk)
{
if (min(pi.x, pj.x) <= pk.x && max(pi.x, pj.x) >= pk.x && min(pi.y, pj.y) <= pk.y && max(pi.y, pj.y) >= pk.y)
return true;
return false;
}
bool segmentsInterect(const Point &p1, const Point &p2, const Point &p3, const Point &p4)
{
float d1 = direction(p3, p4, p1);
float d2 = direction(p3, p4, p2);
float d3 = direction(p1, p2, p3);
float d4 = direction(p1, p2, p4);
if ( ((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)) )
return true;
else if (abs(d1) < 1e-8 && onSegmant(p3, p4, p1))
return true;
else if (abs(d2) < 1e-8 && onSegmant(p3, p4, p2))
return true;
else if (abs(d3) < 1e-8 && onSegmant(p1, p2, p3))
return true;
else if (abs(d4) < 1e-8 && onSegmant(p1, p2, p4))
return true;
return false;
}
int main() {
Point p1(0,0);
Point p2(5,0);
Point p3(2,2);
Point p4(2,-2);
cout << segmentsInterect(p1,p2,p3,p4) << endl;
cout << segmentsInterect(p1,p3,p2,p4) << endl;
return 0;
}