-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
172 lines (147 loc) · 3.77 KB
/
main.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Author: Saiyam Jain
// License: MIT LICENSE
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
int x_coord, y_coord;
Point(int x = 0, int y = 0)
{
x_coord = x;
y_coord = y;
}
Point(Point& p)
{
x_coord = p.x_coord;
y_coord = p.y_coord;
}
double operator |(Point const& obj) // Distance formula
{
double distance;
distance = sqrt(pow(obj.x_coord - x_coord, 2) + pow(obj.y_coord - y_coord, 2));
return distance;
}
Point operator ^(Point const& obj) // Midpoint formula
{
Point res;
res.x_coord = (x_coord + obj.x_coord) / 2;
res.y_coord = (y_coord + obj.y_coord) / 2;
return res;
}
};
class Line
{
public:
Point point1, point2;
Line(Point a, Point b)
{
point1 = a;
point2 = b;
}
Line(Line& l)
{
point1 = l.point1;
point2 = l.point2;
}
// Check if a given point lies on the line
bool check_for_point(Point p)
{
if(p.y_coord - point1.y_coord == ((point2.y_coord - point1.y_coord)/(point2.x_coord - point1.x_coord)*(p.x_coord - point1.x_coord)))
{
return true;
}
return false;
}
bool check_for_point(int x, int y)
{
Point p(x, y);
return check_for_point(p);
}
};
class CartasianPlane
{
private:
int x_axis, y_axis;
public:
CartasianPlane(int x_len = 0, int y_len = 0)
{
x_axis = x_len;
y_axis = y_len;
}
// Plotting functions for points and lines
void plot(int x = 0, int y = 0)
{
for(int i = y_axis; i >= -y_axis; i--)
{
for(int j = -x_axis; j <= x_axis; j++)
{
if(i == y && j == x)
{
cout << " @ ";
}
else if(i == 0 && j == 0)
{
cout << " + ";
}
else if(j == 0)
{
cout << " | ";
}
else if(i == 0)
{
cout << " - ";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
void plot(Point p)
{
plot(p.x_coord, p.y_coord);
}
void plot(Line l)
{
for(int i = y_axis; i >= -y_axis; i--)
{
for(int j = -x_axis; j <= x_axis; j++)
{
if(l.check_for_point(i, j))
{
cout << " @ ";
}
else if(i == 0 && j == 0)
{
cout << " + ";
}
else if(j == 0)
{
cout << " | ";
}
else if(i == 0)
{
cout << " - ";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
void plot(Point a, Point b) //Plot a line from two given points
{
Line l(a, b);
plot(l);
}
};
int main()
{
return 0;
}