
C_Kurs_Lekt / C_III_семестр / 17-3_примеры_графики
.pdf
#include <conio.h> #include <graphics.h> #include <process.h> #include <stdio.h>
int Initialize(int *GraphDriver,int *GraphMode , double *AspectRatio);
main () |
|
|
|
|
|
{ |
|
|
|
|
|
int |
res; |
|
|
|
|
char |
buffer[240]; |
|
|
|
|
int |
GraphDriver; |
/* The Graphics device driver |
*/ |
||
int GraphMode; |
|
/* The Graphics mode value |
*/ |
||
int |
ErrorCode; |
/* Reports any graphics errors |
*/ |
||
double |
AspectRatio; |
/* Aspect ratio of a pixel on the screen*/ |
|||
// |
GraphDriver = DETECT; |
/* Request auto-detection */ |
|||
GraphDriver = 9; |
|
/* Request EGAVGA.BGI */ |
|
||
GraphMode=2; /* |
VGA 640x480 16 colors */ |
|
|||
ErrorCode=Initialize( &GraphDriver, &GraphMode, &AspectRatio); |
|||||
line |
( |
0, 0, 0, getmaxy () ); |
|
||
line |
( |
0, getmaxy (), getmaxx (), getmaxy () ); |
|
||
line |
( |
getmaxx (), getmaxy (), getmaxx (), 0 ); |
|
||
line |
( |
getmaxx (), 0, 0, 0 ); |
|
||
line |
( |
0, 0, getmaxx (), getmaxy ()); |
|
||
line |
( |
0, getmaxy (), getmaxx (), 0 ); |
|
//mode = getgraphmode();
sprintf(buffer,"MAXmode = %d, |
DriverName - %s, ModeName - %s", |
|
||
getmaxmode(), getdrivername(),getmodename(GraphMode)); |
|
|
||
outtextxy(30, 10, buffer); |
|
|
|
|
sprintf(buffer, "AspectRatio = %lf kvadrat 50x50", AspectRatio); |
||||
outtextxy( 100, 35, buffer); |
|
|
|
|
rectangle(200, 50, 250, 100); |
|
|
|
|
getch (); |
|
|
|
|
closegraph (); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
int Initialize(int *GraphDriver,int *GraphMode, double *AspectRatio) |
|
|||
{ |
|
|
|
|
int xasp, yasp; |
/* Used to read the aspect ratio*/ |
|
||
int ErrorCode; |
/* Reports any graphics errors |
*/ |
|
|
initgraph( GraphDriver, GraphMode, "" ); |
|
|
||
ErrorCode = graphresult(); |
/* Read result of initialization*/ |
|||
if ( ErrorCode != grOk ){ |
/* Error occured during init |
*/ |
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode )
);
exit( 1 );
}
getaspectratio( &xasp, &yasp ); /* read the hardware aspect */
* AspectRatio = (double)xasp / (double)yasp; /* Get correction factor */ return ErrorCode;
}
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
//void _Cdecl EGAVGA_driver(void);
/* the names of the text styles supported */
char *fname[] = { "DEFAULT font проба русских фонтов", "TRIPLEX font проба русских фонтов", "SMALL font проба русских фонтов", "SANS SERIF font проба русских фонтов", "GOTHIC font проба русских фонтов"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode; int style, midx, midy;
int size = 1;
/* register a driver that was added into graphics.lib */ /* For information on adding the driver, see the
/* BGIOBJ section of UTIL.DOC */
//errorcode = registerbgidriver(EGAVGA_driver);
/* report any registration errors */ /*if (errorcode < 0)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1);} /* terminate with an error code */
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2; midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* loop through the available text styles */
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
{
cleardevice();
if (style == TRIPLEX_FONT) size = 4;
/* select the text style */ settextstyle(style, HORIZ_DIR, size);
/* output a message */
//outtextxy(midx, midy, fname[style]);
outtextxy(midx, midy, "Наша строка"); getch();
}
/* clean up */ closegraph(); return 0;
}
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
/* the names of the text styles supported */ char *fname[] = { "DEFAULT font РУССКИЕ",
"TRIPLEX font РУССКИЕ", "SMALL font РУССКИЕ", "SANS SERIF font РУССКИЕ", "GOTHIC font РУССКИЕ"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode; int style, midx, midy;
int size = 1;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2; midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* loop through the available text styles */
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
{
cleardevice();
if (style == TRIPLEX_FONT) size = 4;
/* select the text style */ settextstyle(style, HORIZ_DIR, 1);
/* output a message */ outtextxy(midx, midy, fname[style]); getch();
}
/* clean up */ closegraph(); return 0;
}

// Точки случайно закрашивают прямоугольник
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
int main(void) {
/* request auto detection */
int gdriver = DETECT, gmode, errorcode; int style, midx, midy;
int l, t, r, b, i, x, y, c=0;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
l = getmaxx()/2-300; t = getmaxy()/2-100; r = getmaxx()/2+300; b = getmaxy()/2+100; rectangle(l,t,r,b);
for( i=0 ; i< 5000; i++) { x = random(r-l);
y = random(b-t); putpixel(x+l, y+t, c);
if ( c < 15 ) c++; else c=0; delay(2);
}
getch();
closegraph(); return 0;
}

// Цветные прямоугольники
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
int main(void) {
/* request auto detection */
int gdriver = DETECT, gmode, errorcode; int style, midx, midy;
int l, t, r, b, c=0;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
l = 0; t = 0;
r = getmaxx(); b = getmaxy();
for( ; r-l > 20 && b-t > 20; l+=10, r-=10, t+=10, b-=10) { setcolor(c);
setfillstyle(SOLID_FILL, c ); rectangle(l,t,r,b); floodfill( l+1, t+1, c);
// вместо rectangle |
и floodfill |
- bar(l,t,r,b); |
if ( c < 15 ) c++; |
else c=0; |
}
getch();
closegraph(); return 0;
}

// Цветные окружности с заливкой
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
int main(void) { |
|
|
|
/* request auto detection */ |
|
|
int gdriver = DETECT, |
gmode, errorcode; |
|
int x, y, i, xradius, |
c=1; |
// |
int xasp, yasp; |
// для CIRCLE |
//getaspectratio(&xasp, &yasp);
//setaspectratio(xasp/2, yasp);
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx()/2; y = getmaxy()/2;
i = EMPTY_FILL; // 0
for (xradius=y; xradius > 20; xradius-=20) { setcolor(c);
setfillstyle(i, c);
//pieslice(x, y, 10, 360, xradius); // - секторами circle(x, y, xradius);
floodfill( x, y, c); |
|
||
if ( c < |
15 ) c++; |
else c=1; |
|
if ( i < |
USER_FILL |
) i++; |
else i=EMPTY_FILL; // < 12 |
getch();
}
closegraph(); return 0;
}

// Эллипсы разного цвета и эксцентриситета
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
int main(void) {
/* request auto detection */
int gdriver = DETECT, gmode, errorcode; int x, y, i, rx, ry, c=1, xasp, yasp;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
getaspectratio(&xasp, &yasp);
printf("aspectratio x %d y %d\n", xasp, yasp); getch();
cleardevice();
x = getmaxx()/2; y = getmaxy()/2; ry=y;
for (rx=y; rx > 20; rx-=20)
for (ry=y; ry > 20; ry-=20){ setcolor(c);
ellipse(x, y, 0, 360, rx, ry);
if ( c < 15 ) c++; |
else c=1; |
} |
|
getch(); |
|
closegraph(); |
|
return 0; |
|
}

// Цветная полоса
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>
int main(void) {
/* request auto detection */
int gdriver = DETECT, gmode, errorcode; int style, midx, midy;
int left, top, right, bottom, x, colr=0;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
left = getmaxx() / 2 - 200; top = getmaxy() / 2 - 100; right = getmaxx() / 2 + 200; bottom = getmaxy() / 2 + 100;
rectangle(left,top,right,bottom);
for( x = left+1; x < right; x++) { setcolor(colr);
line(x, top, x, bottom); if ( colr < 15 ) colr++;
else colr=0;
}
getch();
closegraph(); return 0;
}

#include <stdio.h> #include <graphics.h> #include <string.h> main()
{
int w1,w2;
int i,j,s=0,a[6],m; double d;
char c[5],c1[5]={" \0"}; for(i=0;i<6;i++)
{
printf("\na[%d]=",i);
scanf("%4d",&a[i]);
s+=a[i];
}
detectgraph(&w1,&w2);
initgraph(&w1,&w2,"c:\\tc\\bgi");
for(i=0;i<6;i++)
{
setcolor(i+9); bar3d(i*20+100,200-a[i],i*20+120,200,4,1); d=(double)a[i]/(double)s; circle(i*80+50,280,30); sector(i*80+50,280,0,360*d,30,30); m=a[i];
j=3;
strcpy(c,c1);
while(m!=0)
{
c[j--]=m%10+48; m/=10;
}
outtextxy(i*80+40,315,c);
}
while(!kbhit());bioskey(0);
closegraph();
}

// Выводим надпись в центре экрана и обводим рамкою с отступом 20 #include <graphics.h>
#include <stdlib.h> #include <stdio.h> #include <conio.h>
/* the names of the text styles supported */ int main(void)
{
/* request auto detection */ int gdriver = DETECT;
int gmode, errorcode; int style, midx, midy ;
int l, r, t, b, width, height; int size = 1;
char *str="Наша строка";
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("display - %d x- %d y - %d", gdriver, getmaxx(),getmaxy()); printf("This mode supports colors 0..%d", getmaxcolor());
midx = getmaxx() / 2; midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT); size = 4;
settextstyle(TRIPLEX_FONT, HORIZ_DIR, size); outtextxy(midx, midy, str);
getch();
l=midx - textwidth(str)/2 - 20; r=midx + textwidth(str)/2 + 20; t=midy - textheight(str)/2 - 20; b=midy + textheight(str)/2 + 20; rectangle(l,t,r,b);
getch();
/* clean up */ closegraph(); return 0;
}