/
Graph.cpp
142 lines (121 loc) · 3.55 KB
/
Graph.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
#include "Graph.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <string>
// Constructor
Graph::Graph() {
counter = 0;
FILE *file = fopen("data/points.txt", "r");
buff = (char*) malloc(255);
// status reads the number of strings converted by fscanf
// flip keeps track of 0, 1 position in the array
char status;
bool flip = 0;
do {
status = fscanf(file, "%s", buff);
points[counter][flip] = atoi(buff);
if(flip == 1){
++counter;
}
flip = !flip;
}while(status == 1);
init(); // Draws the axis.
}
//Testing new constructor.
Graph::Graph(char* data_file, char* x_label, char* y_label){
this->data_file = data_file;
this->x_label = x_label;
this->y_label = y_label;
counter = 0; // No of points to be plotted, set in constructor.
FILE *file = fopen(data_file , "r");
buff = (char*) malloc(255);
// status reads the number of strings converted by fscanf
// flip keeps track of 0, 1 position in the array
char status;
bool flip = 0;
do {
status = fscanf(file, "%s", buff);
points[counter][flip] = atoi(buff);
if(flip == 1){
++counter;
}
flip = !flip;
}while(status == 1);
initwindow(MAX_X, MAX_Y, "Asif & Sameem's plots."); // Looks better with my name first ;)
init();
}
void Graph::init() {
cleardevice();
setcolor(CYAN);
setlinestyle(0, 0, 2);
drawline(0, 0, 500, 0);
drawline(0, 0, 0, 400);
setcolor(WHITE);
setLabels(x_label, y_label);
}
void Graph::drawline(int x1, int y1, int x2, int y2) {
x1 += X_OFFSET;
y1 = Y_OFFSET - y1;
x2 += X_OFFSET;
y2 = Y_OFFSET - y2;
line(x1, y1, x2, y2);
}
void Graph::xyPlot() {
init(); // Draws the axis.
setcolor(WHITE);
outtextxy(450, 40, "GRAPH PLOT");
setcolor(YELLOW);
for (int i = 0; i < counter - 1; ++i) {
drawline(points[i][0], points[i][1], points[i+1][0], points[i+1][1]);
}
}
void Graph::drawcircle(int x, int y, int radius) {
x += X_OFFSET;
y = Y_OFFSET - y;
circle(x, y, radius);
}
void Graph::scatterPlotCircle() {
init(); // Draws the axis.
setcolor(WHITE);
outtextxy(425, 40, "SCATTER PLOT CIRCLE");
setcolor(YELLOW);
for (int i = 0; i < counter; ++i) {
drawcircle(points[i][0], points[i][1], 3);
}
}
void Graph::scatterPlotCross(){
init(); //Draw axis and set labels
setcolor(COLOR(255,255,255));
outtextxy(425, 40, "SCATTER PLOT CROSSES");
setlinestyle(SOLID_LINE, 1, 2 );
setcolor(COLOR(174,137,118));
for (int i = 0; i < counter ; ++i) {
drawCross(points[i][0], points[i][1]);
}
setlinestyle(SOLID_LINE, 1, 1);
}
/*Draw a cross hair at point x1, y1 */
void Graph::drawCross(int x1, int y1){
/*Need 4 points to draw a cross hair which are calculated from x1, y1*/
int cross_width = 3;
drawline(x1 + cross_width, y1 + cross_width, x1 - cross_width , y1 - cross_width);
drawline(x1 - cross_width, y1 + cross_width, x1 + cross_width, y1 - cross_width);
}
void Graph::setLabels(char* x_label, char* y_label){
/*Used for setting graph labels
TODO use variables for off set and padding
*/
// X axis label
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(0, HORIZ_DIR, 0);
outtextxy(X_OFFSET + 200, Y_OFFSET + 25, x_label);
//Y axis label
settextstyle(0, VERT_DIR, 0);
outtextxy(X_OFFSET - 25, Y_OFFSET - 150, y_label);
settextstyle(0, HORIZ_DIR, 0);
}
void Graph:: clear(){
cleardevice();
}