Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
SegmentIntersection Struct Reference

Public Member Functions

bool intersect (Point first_point, Point second_point, Point third_point, Point forth_point)
 
int direction (Point first_point, Point second_point, Point third_point)
 
bool on_segment (Point first_point, Point second_point, Point third_point)
 

Detailed Description

intersect returns true if segments of two line intersects and false if they do not. It calls the subroutines direction which computes the orientation.

Member Function Documentation

◆ direction()

int SegmentIntersection::direction ( Point first_point,
Point second_point,
Point third_point )
inline

We will find direction of line here respect to @first_point. Here @second_point and @third_point is first and second points of the line respectively. we want a method to determine which way a given angle these three points turns. If returned number is negative, then the angle is counter-clockwise. That means the line is going to right to left. We will fount angle as clockwise if the method returns positive number.

64 {
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 }
int y
Point respect to x coordinate.
Definition line_segment_intersection.cpp:14

◆ intersect()

bool SegmentIntersection::intersect ( Point first_point,
Point second_point,
Point third_point,
Point forth_point )
inline
24 {
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 }
bool on_segment(Point first_point, Point second_point, Point third_point)
Definition line_segment_intersection.cpp:75
int direction(Point first_point, Point second_point, Point third_point)
Definition line_segment_intersection.cpp:63

◆ on_segment()

bool SegmentIntersection::on_segment ( Point first_point,
Point second_point,
Point third_point )
inline

This method determines whether a point known to be colinear with a segment lies on that segment.

76 {
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 }
T max(T... args)
T min(T... args)
Here is the call graph for this function:

The documentation for this struct was generated from the following file: