-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirection_of_point_from_line_segment.cpp
More file actions
63 lines (50 loc) · 1.11 KB
/
direction_of_point_from_line_segment.cpp
File metadata and controls
63 lines (50 loc) · 1.11 KB
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
#include<iostream>
#include<vector>
using namespace std;
struct point{
double x,y;
point(){};
point(double x, double y): x(x), y(y){};
};
int LEFT=-1, RIGHT=1, ON_THE_LINE=0;
double get_cross_product(point p1, point p2){
//cout<<p1.x<<" "<<p1.y<<endl;
//cout<<p2.x<<" "<<p2.y<<endl;
return p1.x*p2.y - p1.y*p2.x;
}
point subtract(point p1, point p2){
point res;
res.x=p1.x - p2.x;
res.y=p1.y - p2.y;
return res;
}
int get_direction(point a, point b, point p){
//cout<<a.x<<" "<<a.y<<endl;
//cout<<b.x<<" "<<b.y<<endl;
//cout<<p.x<<" "<<p.y<<endl;
b=subtract(b,a);//cout<<b.x<<" "<<b.y<<endl;
p=subtract(p,a);//cout<<p.x<<" "<<p.y<<endl;
double cross_product=get_cross_product(b,p);
if(cross_product > 0){
return LEFT;
}
else if(cross_product < 0){
return RIGHT;
}
else{
return ON_THE_LINE;
}
}
int main(){
int res=get_direction(point(-30,10), point(29,-15), point(15,28));
if(res==-1){
cout<<"point is on the left of the line segment"<<endl;
}
else if(res==1){
cout<<"point is on the right of the line segment"<<endl;
}
else{
cout<<"point is on the line segment"<<endl;
}
return 0;
}