-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolinear_check_crossProduct.cpp
More file actions
96 lines (90 loc) · 1.81 KB
/
colinear_check_crossProduct.cpp
File metadata and controls
96 lines (90 loc) · 1.81 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <cmath>
#include <climits>
#define eps (0.0001)
using namespace std;
float getDet(int a,int b,int c,int d);
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 getCrossProduct(Point p2);
Point operator - (Point p2);
};
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;
}
Point Point::operator - (Point p2)
{
Point tempPoint(this->x-p2.x,this->y-p2.y);
return tempPoint;
}
bool Point::isColinear(Point p2,Point p3)
{
Point p1(this->x,this->y);
Point p4=p2-p1;
float d1=p4.getCrossProduct(p3-p1);
return abs(d1)<=eps?true:false;
}
float Point::getCrossProduct(Point p2)
{
float result=getDet(this->x,p2.x,this->y,p2.y);
return result;
}
float getDet(int a,int b,int c,int d)
{
return (float)(a*d-b*c);
}
int main()
{
Point p1(2,3),p2(2,3),p3(5,4);
bool isCol=p1.isColinear(p2,p3);
cout<<"isCol?"<<isCol<<endl;
return 0;
}