include SDL SDL include stdlib include stdio include SDL_draw include

  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
#include <SDL/SDL.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL_draw.h>
#include <math.h>
#include <SDL_ttf.h>
#include <string.h>
const Sint16 width=1060, height=60 0, depth=32, ybeg = -5, yend = 5;
void setka( TTF_Font *fnt,SDL_Surface * who_draw, double xmin, double xmax, Sint16 mx, Sint16 my, Sint16 x0scr, Sint16 y0scr)
{ Sint16 xch,ych;
SDL_Color txtcolor{00, 00, 00};
SDL_Surface * text_surface;
char s[5],Pi[]="Pi", Pi2[]="Pi/2",mns[]="-";
int i;
SDL_Rect dest{10,10, 1,1};
//красим поле
Draw_FillRect(who_draw,0,0,width,height,0xFFFFFF);
//прям-ик
Draw_Rect(who_draw,30,30,width-1-60,height-1-60,0x9F9F9F);
//вертикальные линии
for(dest.y=height-25,xch=0,i=0;xch<width-10-30-x0scr;i++,xch+=floor(M_PI_2*mx))
{
Draw_VLine(who_draw,x0scr+xch,30,height-2-30,0x9F9F9F);
dest.x=x0scr+xch;
if(i==0){text_surface=TTF_RenderText_Solid(fnt,"0", txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
continue;
}
else if(i%2)
{
itoa(i,s,10);
strcat(s,Pi2);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
else {
itoa(i/2,s,10);
strcat(s,Pi);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
}
dest.x+=floor(M_PI_2*mx);
if(i%2)
{
itoa(i,s,10);
strcat(s,Pi2);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
else {
itoa(i/2,s,10);
strcat(s,Pi);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
for(i=0,xch=0;xch<x0scr-40;i--,xch+=floor(M_PI_2*mx))
{
Draw_VLine(who_draw,x0scr-xch,30,height-2-30,0x9F9F9F);
dest.x=x0scr-xch;
if(i==0){text_surface=TTF_RenderText_Solid(fnt,"0", txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
continue;
}
else if(i%2)
{
itoa(i,s,10);
strcat(s,Pi2);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
else {
itoa(i/2,s,10);
strcat(s,Pi);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
}
dest.x-=floor(M_PI_2*mx);
if(i%2)
{
itoa(i,s,10);
strcat(s,Pi2);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
else {
itoa(i/2,s,10);
strcat(s,Pi);
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
//горизонтальные линии + вертикальные цифры
for(dest.x=15,ych=0,i=5;ych<10;i--,ych+=1)
{
Draw_HLine(who_draw,30,(30+ych*my),width-2-30,0x9F9F9F);
itoa(i,s,10);
dest.y=15+ych*my;
text_surface=TTF_RenderText_Solid(fnt,s, txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
dest.y=height-45;
text_surface=TTF_RenderText_Solid(fnt,"-5", txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
//ось ОУ
Draw_VLine(who_draw,x0scr,10,height-2-30,0x000000);
Draw_Line(who_draw,x0scr,10,x0scr-7,25,0x000000);
Draw_Line(who_draw,x0scr,10,x0scr+7,25,0x000000);
dest.x=x0scr-15;
dest.y=2;
text_surface=TTF_RenderText_Solid(fnt,"Y", txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
//ось ОХ
Draw_HLine(who_draw,30,y0scr,width-2-10,0x000000);
Draw_Line(who_draw,width-1-10,y0scr,width-2-25,y0scr-7,0x000000);
Draw_Line(who_draw,width-1-10,y0scr,width-2-25,y0scr+7,0x000000);
dest.x=width-1-20;
dest.y=y0scr+10;
text_surface=TTF_RenderText_Solid(fnt,"X", txtcolor);
SDL_BlitSurface(text_surface, NULL, who_draw,&dest);
}
void draw_func(SDL_Surface * who_draw, double xmin, double xmax,double mx, double my,Sint16 x0scr, Sint16 y0scr)
{ double dx=0.0001,x,y;
Sint16 xscr,yscr;
for(x=xmin;x<=xmax;x+=dx)
{if(sin(x/3+M_PI_4))
y=(cos(x/3+M_PI_4)/sin(x/3+M_PI_4));
xscr=x0scr+floor(x*mx);
yscr=y0scr-floor(y*my);
if((yscr<30)||(height-2-30<yscr))continue;
Draw_Pixel(who_draw, xscr, yscr,0xFF0000);
}
}
int main ( int argc, char** argv )
{
TTF_Font *fnt = NULL;
SDL_Surface * screen;
SDL_Event event;
double xmin=30, xmax=50,a;
Sint16 mx, my;
Sint16 x0scr, y0scr, xscr, yscr;
freopen("CON","w",stdout);
// ввод границ
printf("xmin v Pi=");
scanf("%lf",&xmin);
xmin*=M_PI;
printf("xmax v Pi=");
scanf("%lf",&xmax);
xmax*=M_PI;
//лежит ли справа или слева от оси координат
if(((xmin>0)&&(xmax>0))||((xmin<0)&&(xmax<0)))
mx=floor((width-60)/(fabs(xmax+xmin)));
else
mx=floor((width-60)/(fabs(xmax-xmin)));
//подсчет всего
my=(height-60)/10;
x0scr=fabs(floor(-xmin*mx)+30);
y0scr=floor(height/2);
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) )
return 1;
screen=SDL_SetVideoMode(width,height,depth,SDL_ANYFORMAT);
// create a new window
if ( !screen )
{
SDL_Quit;
return 1;
}
//initialize TTF
if (TTF_Init())
{
printf( "Unable to init SDL_Font: %s\n", SDL_GetError() );
return 1;
}
//подключение шрифта
if(!(fnt = TTF_OpenFont("CharisSILR.ttf", 15))){
printf("Unable to set Font: %s\n", SDL_GetError());
TTF_Quit();
SDL_Quit();
return 3;
}
setka(fnt,screen,xmin,xmax,mx,my,x0scr,y0scr);
draw_func(screen,xmin,xmax,mx,my,x0scr,y0scr);
SDL_Flip(screen);
while (SDL_WaitEvent(&event))
{
if(event.type==SDL_QUIT||(event.type==SDL_KEYDOWN &&event.key.keysym.sym==SDLK_ESCAPE))
{
SDL_FreeSurface(screen);
TTF_CloseFont(fnt);
TTF_Quit();
SDL_Quit();
return 0;
}
}
return 2;
}