-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment.cpp
99 lines (69 loc) · 2.77 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Segment.hpp"
Segment::Segment(){}
Segment::Segment(const Point& p1, const Point& p2){
this->p1 = p1;
this->p2 = p2;
}
Point Segment::getP1() const{
return p1;
}
Point Segment::getP2() const{
return p2;
}
void Segment::swap_points(){
Point aux = p1;
p1 = p2;
p2 = aux;
}
bool Segment::operator() (const Segment& lhs, const Segment& rhs) const{
if((float)(lhs.p1.y + ((lhs.p2.y-lhs.p1.y)/(lhs.p2.x-lhs.p1.x)*(currentX-lhs.p1.x))) != (float)(rhs.p1.y + ((rhs.p2.y-rhs.p1.y)/(rhs.p2.x-rhs.p1.x)*(currentX-rhs.p1.x)))){
return (lhs.p1.y + ((lhs.p2.y-lhs.p1.y)/(lhs.p2.x-lhs.p1.x)*(currentX-lhs.p1.x))) < (rhs.p1.y + ((rhs.p2.y-rhs.p1.y)/(rhs.p2.x-rhs.p1.x)*(currentX-rhs.p1.x)));
}else{
double angCoefLhs = (lhs.p2.y-lhs.p1.y)/(lhs.p2.x-lhs.p1.x);
double angCoefRhs = (rhs.p2.y-rhs.p1.y)/(rhs.p2.x-rhs.p1.x);
if(after){
return angCoefLhs < angCoefRhs;
}else{
return angCoefLhs > angCoefRhs;
}
}
}
bool Segment::equals(const Segment& s) const{
return (s.p1.x == p1.x && s.p1.y == p1.y && s.p2.x == p2.x && s.p2.y == s.p2.y) || (s.p1.x == p2.x && s.p1.y == p2.y && s.p2.x == p1.x && s.p2.y == s.p1.y);
}
double Segment::line_side(Point p) const{
return (p.x - p1.x)*(p2.y - p1.y) - (p.y - p1.y)*(p2.x - p1.x);
}
bool Segment::intersect(Segment s) const{
if((line_side(s.getP1())*line_side(s.getP2())) < 0 && (s.line_side(p1)*s.line_side(p2)) < 0){
return true;
}
return false;
}
bool Segment::inside_segment(Point p) const{
if(!line_side(p)){
if(p.x >= p1.x && p.y >= p1.y && p.x <= p2.x && p.y <= p2.y) return true;
if(p.x >= p1.x && p.y <= p1.y && p.x <= p2.x && p.y >= p2.y) return true;
if(p.x <= p1.x && p.y <= p1.y && p.x >= p2.x && p.y >= p2.y) return true;
if(p.x <= p1.x && p.y >= p1.y && p.x >= p2.x && p.y <= p2.y) return true;
}
return false;
}
Point Segment::point_of_intersection(Segment s) const{
Point p = {0,0};
double denominator = (p1.x - p2.x)*(s.getP1().y - s.getP2().y) - (p1.y - p2.y)*(s.getP1().x - s.getP2().x);
if(!denominator){
p = p1;
return p;
}
p.x = ((p1.x*p2.y-p1.y*p2.x)*(s.getP1().x - s.getP2().x) - (p1.x - p2.x)*(s.getP1().x*s.getP2().y - s.getP1().y*s.getP2().x)) / denominator;
p.y = ((p1.x*p2.y-p1.y*p2.x)*(s.getP1().y - s.getP2().y) - (p1.y - p2.y)*(s.getP1().x*s.getP2().y - s.getP1().y*s.getP2().x)) / denominator;
return p;
}
double Segment::getValueOnX(const double x) const{
return (p1.y + ((p2.y-p1.y)/(p2.x-p1.x)*(x-p1.x)));
}
std::ostream& operator<<(std::ostream& os, const Segment& s){
os << s.getP1().x << " " << s.getP1().y << " " << s.getP2().x << " " << s.getP2().y;
return os;
}