#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;
}
}