TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
line_segment_intersection.cpp
Go to the documentation of this file.
1
6
#include <algorithm>
7
#include <iostream>
8
12
struct
Point
{
13
int
x;
14
int
y
;
15
};
16
22
struct
SegmentIntersection
{
23
inline
bool
intersect(
Point
first_point,
Point
second_point,
24
Point
third_point,
Point
forth_point) {
25
int
direction1 =
direction
(third_point, forth_point, first_point);
26
int
direction2 =
direction
(third_point, forth_point, second_point);
27
int
direction3 =
direction
(first_point, second_point, third_point);
28
int
direction4 =
direction
(first_point, second_point, forth_point);
29
30
if
((direction1 < 0 || direction2 > 0) &&
31
(direction3 < 0 || direction4 > 0))
32
return
true
;
33
34
else
if
(direction1 == 0 &&
35
on_segment
(third_point, forth_point, first_point))
36
return
true
;
37
38
else
if
(direction2 == 0 &&
39
on_segment
(third_point, forth_point, second_point))
40
return
true
;
41
42
else
if
(direction3 == 0 &&
43
on_segment
(first_point, second_point, third_point))
44
return
true
;
45
46
else
if
(direction3 == 0 &&
47
on_segment
(first_point, second_point, forth_point))
48
return
true
;
49
50
else
51
return
false
;
52
}
53
63
inline
int
direction
(
Point
first_point,
Point
second_point,
64
Point
third_point) {
65
return
((third_point.x - first_point.x) *
66
(second_point.
y
- first_point.
y
)) -
67
((second_point.x - first_point.x) *
68
(third_point.
y
- first_point.
y
));
69
}
70
75
inline
bool
on_segment
(
Point
first_point,
Point
second_point,
76
Point
third_point) {
77
if
(std::min(first_point.x, second_point.x) <= third_point.x &&
78
third_point.x <= std::max(first_point.x, second_point.x) &&
79
std::min(first_point.
y
, second_point.
y
) <= third_point.
y
&&
80
third_point.
y
<= std::max(first_point.
y
, second_point.
y
))
81
return
true
;
82
83
else
84
return
false
;
85
}
86
};
87
92
int
main
() {
93
SegmentIntersection
segment;
94
Point
first_point, second_point, third_point, forth_point;
95
96
std::cin >> first_point.x >> first_point.
y
;
97
std::cin >> second_point.x >> second_point.
y
;
98
std::cin >> third_point.x >> third_point.
y
;
99
std::cin >> forth_point.x >> forth_point.
y
;
100
101
printf(
"%d"
, segment.intersect(first_point, second_point, third_point,
102
forth_point));
103
std::cout << std::endl;
104
}
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
Point
Definition
line_segment_intersection.cpp:12
Point::y
int y
Point respect to x coordinate.
Definition
line_segment_intersection.cpp:14
Point::Point
Point(double a=0.f, double b=0.f)
Definition
smallest_circle.cpp:23
SegmentIntersection
Definition
line_segment_intersection.cpp:22
SegmentIntersection::on_segment
bool on_segment(Point first_point, Point second_point, Point third_point)
Definition
line_segment_intersection.cpp:75
SegmentIntersection::direction
int direction(Point first_point, Point second_point, Point third_point)
Definition
line_segment_intersection.cpp:63
geometry
line_segment_intersection.cpp
Generated by
1.18.0