include cstdlib include SDL include SDL_draw include SDL_ttf include m

  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
#include <cstdlib>
#include <SDL.h>
#include <SDL_draw.h>
#include <SDL_ttf.h>
#include <math.h>
#include <iostream>
void draw_sheet(SDL_Surface *screen, int firstkord, int secondkord, TTF_Font *font,SDL_Color fc);
void draw_graph(SDL_Surface *screen, int firstkord, int secondkord);
int main(int argc, char** argv){
TTF_Font *font = NULL;
SDL_Surface *text_surface = NULL;
SDL_Color fc;
SDL_Rect dest;
int firstkord, secondkord;
freopen("CON", "w", stdout);
printf("Enter an interval:\n");
scanf("%d", &firstkord);
scanf("%d", &secondkord);
if(firstkord > secondkord){
printf("Error\n");
system("PAUSE");
return 0;
}
if((SDL_Init(SDL_INIT_VIDEO) < 0)){
printf("Unable to init SDL:%s\n", SDL_GetError());
return 1;
}
if(TTF_Init()){
printf("Unable to init TTF:%s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
if(!screen){
printf("Unable to set 640*480video:%s\n", SDL_GetError());
return 1;
}
fc.r = 250;
fc.g = 0;
fc.b = 0;
SDL_Rect dstrect;
dstrect.x = 0;
dstrect.y = 0;
bool done = false;
draw_sheet(screen, firstkord, secondkord, font, fc);
draw_graph(screen, firstkord, secondkord);
while(!done){
SDL_Event event;
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:{
if(event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
}
}
SDL_Flip(screen);
}
printf("Exit\n");
return 0;
}
int temp(int amount){
int num = 0;
amount = abs(amount);
while(amount >= 10){
amount/= 10;
num++;
printf("%d\n", num);
}
return num+3;
}
void line(char*kord1, int firstkord, int amount){
amount--;
kord1[0] = (firstkord >= 0) ? (firstkord == 0) ? ' ' : '+' : '-';
firstkord = abs(firstkord);
kord1[amount--] = '\0';
while (firstkord >= 10){
kord1[amount--] = 48+(firstkord%10);
firstkord/= 10;
}
kord1[amount] = 48+(firstkord%10);
}
void draw_sheet(SDL_Surface *screen, int firstkord, int secondkord, TTF_Font *font, SDL_Color fc){
SDL_Rect text = {0, 245, 640, 480};
SDL_Surface *stext1, *stext2;
int x, y, tmp = firstkord;
char *kord1, *kord2;
int diff = secondkord-firstkord;
int q = 576/diff;
Draw_FillRect(screen, 0, 0, 640, 480, SDL_MapRGB(screen->format, 255, 255, 255));
for(y = 20; y <= 452; y+= q)
Draw_HLine(screen, 20, y, 20+q*diff, SDL_MapRGB(screen->format, 100, 100, 100));
for(x = 20; x <= 20+q*diff; x+= q)
Draw_VLine(screen, x, 20, 452, SDL_MapRGB(screen->format, 100, 100, 100));
Draw_HLine(screen, 20, 20+(452/(2*q))*q, 20+q*diff, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20+q*diff, 20+(452/(2*q))*q, 10+q*diff, 30+(452/(2*q))*q, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20+q*diff, 20+(452/(2*q))*q, 10+q*diff, 10+(452/(2*q))*q, SDL_MapRGB(screen->format, 250, 0, 0));
if(firstkord < 0 && secondkord > 0){
Draw_VLine(screen, 20+(-firstkord)*q, 20, 452, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20+(-firstkord)*q, 20, 20+(-firstkord)*q+10, 30, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20+(-firstkord)*q, 20, 20+(-firstkord)*q-10, 30, SDL_MapRGB(screen->format, 250, 0, 0));
}
if(firstkord < 0 && secondkord < 0){
Draw_VLine(screen, 10+q*diff, 20, 452, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 10+q*diff, 20, 20+q*diff, 30, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 10+q*diff, 20, 40+q*diff, 30, SDL_MapRGB(screen->format, 250, 0, 0));
}
if(firstkord > 0 && secondkord > 0){
Draw_VLine(screen, 10, 20, 452, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 10, 20, 20, 30, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 10, 20, 0, 30, SDL_MapRGB(screen->format, 250, 0, 0));
}
if(firstkord == 0){
Draw_VLine(screen, 20, 20, 452, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20, 20, 10, 30, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20, 20, 30, 30, SDL_MapRGB(screen->format, 250, 0, 0));
}
if(secondkord == 0){
Draw_VLine(screen, 20+q*diff, 20, 425, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20+q*diff, 20, 10+q*diff, 30, SDL_MapRGB(screen->format, 250, 0, 0));
Draw_Line(screen, 20+q*diff, 20, 20+q*diff, 30, SDL_MapRGB(screen->format, 250, 0, 0));
}
}
void draw_graph(SDL_Surface *screen, int firstkord, int secondkord){
int i;
double x = firstkord, y , a, b, x1 = secondkord;
int diff = secondkord-firstkord;
int q = 576/diff;
while(x < x1){
y = log(fabs(x)/4);
a = x*q+(abs(firstkord)*q)+20+(firstkord*q+1*q)*((firstkord < 0 && secondkord < 0) ? 1 : (firstkord > 0 && secondkord > 0) ? -1 : 0);
b = -y*q+20+(452/(2*q))*q;
if(a > 20 && a < 20+q*diff && b > 20 && b < 452)
Draw_FillEllipse(screen, a, b, 1, 1, SDL_MapRGB(screen->format, 0, 0, 0));
x+= 0.0001;
}
}