-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_plotter.cpp
More file actions
254 lines (217 loc) · 6.68 KB
/
graph_plotter.cpp
File metadata and controls
254 lines (217 loc) · 6.68 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <graphics.h>
#include <iostream>
#include <cmath>
using namespace std;
// Function to draw axes with markings (scale)
void drawAxesWithMarkings() {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Draw X and Y axes
line(0, midY, getmaxx(), midY); // X-axis
line(midX, 0, midX, getmaxy()); // Y-axis
// Markings for X-axis
for (int x = midX; x < getmaxx(); x += 50) {
line(x, midY - 5, x, midY + 5); // Positive X tick marks
if (x != midX) {
char label[10];
sprintf(label, "%d", (x - midX) / 50);
outtextxy(x - 5, midY + 10, label);
}
}
for (int x = midX; x > 0; x -= 50) {
line(x, midY - 5, x, midY + 5); // Negative X tick marks
if (x != midX) {
char label[10];
sprintf(label, "%d", -(midX - x) / 50);
outtextxy(x - 10, midY + 10, label);
}
}
// Markings for Y-axis
for (int y = midY; y < getmaxy(); y += 50) {
line(midX - 5, y, midX + 5, y); // Positive Y tick marks
if (y != midY) {
char label[10];
sprintf(label, "%d", -(y - midY) / 50);
outtextxy(midX + 10, y - 5, label);
}
}
for (int y = midY; y > 0; y -= 50) {
line(midX - 5, y, midX + 5, y); // Negative Y tick marks
if (y != midY) {
char label[10];
sprintf(label, "%d", (midY - y) / 50);
outtextxy(midX + 10, y - 5, label);
}
}
}
// Base class: Graph
class Graph {
public:
virtual void draw() = 0; // Pure virtual function for drawing the graph
};
// Derived class: Line for plotting linear functions
class Line : public Graph {
private:
float slope;
float intercept;
public:
Line(float m, float c) : slope(m), intercept(c) {}
void draw() override {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Plot y = mx + c
for (int x = -midX; x < midX; x++) {
int y = slope * x + intercept;
putpixel(midX + x, midY - y, WHITE);
}
}
};
// Derived class: Parabola
class Parabola : public Graph {
private:
float a, b, c;
public:
Parabola(float coefA, float coefB, float coefC) : a(coefA), b(coefB), c(coefC) {}
void draw() override {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Plot y = ax^2 + bx + c
for (int x = -midX; x < midX; x++) {
int y = a * x * x + b * x + c;
putpixel(midX + x, midY - y, WHITE);
}
}
};
// Derived class: Ellipse
class EllipseGraph : public Graph {
private:
int a, b;
public:
EllipseGraph(int semiMajor, int semiMinor) : a(semiMajor), b(semiMinor) {}
void draw() override {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Draw the ellipse
ellipse(midX, midY, 0, 360, a, b);
}
};
// Derived class: Hyperbola
class Hyperbola : public Graph {
private:
float a, b;
public:
Hyperbola(float semiMajor, float semiMinor) : a(semiMajor), b(semiMinor) {}
void draw() override {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Plot hyperbola y^2/b^2 - x^2/a^2 = 1
for (int x = -midX; x < midX; x++) {
if ((x * x / (a * a)) <= 1) continue;
int y1 = sqrt((x * x * b * b / (a * a)) + b * b);
int y2 = -sqrt((x * x * b * b / (a * a)) + b * b);
putpixel(midX + x, midY - y1, WHITE);
putpixel(midX + x, midY - y2, WHITE);
}
}
};
// Derived class: Trigonometric (Sine and Cosine)
class Trigonometric : public Graph {
private:
bool isSine;
public:
Trigonometric(bool sine) : isSine(sine) {}
void draw() override {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Plot sine or cosine
for (int x = -midX; x < midX; x++) {
float rad = x * 0.05; // Scale factor for plotting
int y = isSine ? sin(rad) * 100 : cos(rad) * 100;
putpixel(midX + x, midY - y, WHITE);
}
}
};
// Derived class: Logarithmic
class Logarithmic : public Graph {
public:
void draw() override {
int midX = getmaxx() / 2;
int midY = getmaxy() / 2;
// Plot y = log(x)
for (int x = 1; x < midX; x++) { // Starting from x = 1 to avoid log(0)
int y = log(x) * 50; // Scale factor for plotting
putpixel(midX + x, midY - y, WHITE);
}
}
};
// Utility class: GraphPlotter
class GraphPlotter {
public:
void plot(Graph* graph) {
drawAxesWithMarkings(); // Draw the axes with markings first
graph->draw(); // Then plot the graph on top of the axes
}
};
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int choice;
cout << "Graph Plotter\n";
cout << "1. Plot Line (y = mx + c)\n";
cout << "2. Plot Parabola (y = ax^2 + bx + c)\n";
cout << "3. Plot Ellipse\n";
cout << "4. Plot Hyperbola\n";
cout << "5. Plot Sine\n";
cout << "6. Plot Cosine\n";
cout << "7. Plot Logarithmic\n";
cout << "Enter your choice: ";
cin >> choice;
GraphPlotter plotter;
if (choice == 1) {
float m, c;
cout << "Enter slope (m): ";
cin >> m;
cout << "Enter y-intercept (c): ";
cin >> c;
Line line(m, c);
plotter.plot(&line);
} else if (choice == 2) {
float a, b, c;
cout << "Enter coefficient a: ";
cin >> a;
cout << "Enter coefficient b: ";
cin >> b;
cout << "Enter coefficient c: ";
cin >> c;
Parabola parabola(a, b, c);
plotter.plot(¶bola);
} else if (choice == 3) {
int a, b;
cout << "Enter semi-major axis (a): ";
cin >> a;
cout << "Enter semi-minor axis (b): ";
cin >> b;
EllipseGraph ellipse(a, b);
plotter.plot(&ellipse);
} else if (choice == 4) {
float a, b;
cout << "Enter semi-major axis (a): ";
cin >> a;
cout << "Enter semi-minor axis (b): ";
cin >> b;
Hyperbola hyperbola(a, b);
plotter.plot(&hyperbola);
} else if (choice == 5) {
Trigonometric sine(true);
plotter.plot(&sine);
} else if (choice == 6) {
Trigonometric cosine(false);
plotter.plot(&cosine);
} else if (choice == 7) {
Logarithmic logGraph;
plotter.plot(&logGraph);
}
getch();
closegraph();
return 0;
}