-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
158 lines (131 loc) · 4.13 KB
/
graph.c
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
//
// Created by marosm on 25. 2. 2022.
//
#include "graph.h"
void draw_graph()
{
static unsigned long frame = 0;
static unsigned frames=0;
static double fps=0;
static unsigned long long lastDraw_us=0, now_us=0;
static char borderColor = 'c';
static char green = 'g';
static char red = 'r';
static char blue = 'b';
static char text = 't';
static char underline = 'u';
static char invertBG = 'i';
static char invertFG = 'f';
static char reset = 'n';
static const int WindowSizeX = 80;
static const int WindowSizeY = 18;
int X = ((getConsoleWidth() - WindowSizeX) / 2);
int Y = ((getConsoleHeight() - WindowSizeY) / 2) + 1;
const int xOffset = ((getConsoleWidth() - WindowSizeX) / 2) + 2;
const int yOffset = ((getConsoleHeight() - WindowSizeY) / 2) + 1;
///FPS measureC
long long old_now = now_us;
usElapsed(&now_us);
if (now_us > lastDraw_us+10)
{
fps = (double)frames*1000000 / (double)(now_us-lastDraw_us);
frames = 0;
lastDraw_us = now_us;
}
frames++;
clearScreen();
gotoXY(0, 0);
printf("%3.1f FPS %5.2fms ", fps, (now_us-old_now)/1000.0);
//firstline
gotoXY(X, Y - 2);
pprint("#", &borderColor);
for (short i=0; i < WindowSizeX + 2; i++)
pprint("#", &borderColor);
pprint("#", &borderColor);
//doubled firstline
gotoXY(X, Y - 1);
pprint("##", &borderColor);
for (short i=0; i < WindowSizeX; i++)
pprint("#", &borderColor);
pprint("##", &borderColor);
///BOARD
for (short i=0; i < WindowSizeY; i++)
{
gotoXY(X, Y++);
pprint("##", &borderColor);
for (short j=0; j < WindowSizeX; j++)
{
//pprint(board[i/g->scaling][j/(g->scaling*2)]==' ' ? " " : "█" , &board[i/g->scaling][j/(g->scaling*2)]);
//pprint("█", &green);
}
gotoXY(getConsoleWidth() - ((getConsoleWidth() - WindowSizeX) / 2), i + 1 + ((getConsoleHeight() - WindowSizeY)/2));
pprint("##", &borderColor);//end of row
}
pprint("", &reset);
float min = 1e10;
float max = 0;
unsigned long dataLen = stonksState->instruments[cursor.y]->len;
uint32_t windowLen = WindowSizeX;
if(windowLen > dataLen) windowLen = dataLen;
if(dataLen < 3) return;
for(int i = 0; i < windowLen; i++)
{
if(stonksState->instruments[cursor.y]->data[dataLen-i] > max)
{
max = stonksState->instruments[cursor.y]->data[dataLen-i];
}
if(stonksState->instruments[cursor.y]->data[dataLen-i] < min)
{
min = stonksState->instruments[cursor.y]->data[dataLen-i];
}
}
/// Scale
gotoXY(xOffset - 10, yOffset);
printf("%4.1f CZK", max);
gotoXY(xOffset - 10, yOffset + WindowSizeY / 2);
printf("%4.1f CZK", (max+min)/2);
gotoXY(xOffset - 10, yOffset + WindowSizeY);
printf("%4.1f CZK", min);
//OldRange = (OldMax - OldMin)
int oldRange = max - min;
//NewRange = (NewMax - NewMin)
int newRange = WindowSizeY;
//NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
for(int i = 0; i < windowLen; i++)
{
static int lastY = 0;
int oldY = stonksState->instruments[cursor.y]->data[dataLen - (windowLen - i)];
int newY = (((oldY - min) * newRange) / oldRange) + 0;
newY = WindowSizeY - newY; // invert, cuz Y=0 is up
gotoXY(xOffset + i, newY + yOffset);
if(lastY > newY)
{
pprint("", &green);
printf("/");
}
else if(lastY == newY)
{
pprint("", &reset);
printf("-");
}
else
{
pprint("", &red);
printf("\\");
}
lastY = newY;
}
//endl
gotoXY(X, Y);
pprint("##", &borderColor);
for (short i=0; i < WindowSizeX; i++)
pprint("#", &borderColor);
pprint("##\n", &borderColor);
//doubled end
gotoXY(X, Y + 1);
pprint("#", &borderColor);
for (short i=0; i < (WindowSizeX + 2); i++)
pprint("#", &borderColor);
pprint("#\n", &borderColor);
printf("%i", editMode);
}