-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolinear_check_slope.cpp
More file actions
77 lines (74 loc) · 1.54 KB
/
colinear_check_slope.cpp
File metadata and controls
77 lines (74 loc) · 1.54 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <cmath>
#include <climits>
#define eps (0.0001)
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(){this->x=0,this->y=0;}
Point(int x,int y){this->x=x,this->y=y;}
Point(const Point &p1){this->x=p1.x,this->y=p1.y;}
bool isSamePoint(Point p2);
float getSlope(Point p2);
void displayPoint();
void displaySlope(Point p2,string msg1,string msg2);
bool isColinear(Point p2,Point p3);
};
float Point::getSlope(Point p2)
{
if(p2.y-this->y==0)
{
return 0.0f;
}
if(p2.x-this->x==0)
{
return INT_MIN;
}
float m=(p2.y-this->y)/(float)(p2.x-this->x);
}
void Point::displayPoint()
{
cout<<"("<<this->x<<","<<this->y<<")";
}
bool Point::isSamePoint(Point p2)
{
return (this->x==p2.x && this->y==p2.y);
}
void Point::displaySlope(Point p2,string msg1,string msg2)
{
cout<<"slope of "<<msg1<<" ";
this->displayPoint();
cout<<" and "<<msg2<<" ";
p2.displayPoint();
cout<<" is ";
float m=this->getSlope(p2);
if(m==INT_MIN)
{
cout<<"infinity";
}else
{
cout<<m;
}
cout<<endl;
return;
}
bool Point::isColinear(Point p2,Point p3)
{
float m1_2=this->getSlope(p2);
float m1_3=this->getSlope(p3);
float isSame=this->isSamePoint(p2)||this->isSamePoint(p3)||p2.isSamePoint(p3);
return (abs(m1_2-m1_3)<eps || isSame)?true:false;
}
int main()
{
Point p1(2,3),p2(2,3),p3(5,4);
p1.displaySlope(p2,"p1","p2");
bool isCol=p1.isColinear(p2,p3);
cout<<"isCol?"<<(isCol!=0?"true":"false")<<endl;
return 0;
}