Upload
tulgaa-gankhuyag
View
108
Download
2
Embed Size (px)
Citation preview
Лекц – 10 давтан
Объект хандлагат
програмчлал
C++-ийн үндсэн график сан(Basic Graphics in C++)
Си хэлэнд дүрс зурахад зориулсан график сан байдаг. Энэ санг
ажиллуулахад хэрэгтэй толгой файл:
#include <graphic.h>
Дүрсийг зурахын тулд тусдаа дүрс зурах цонхтой ажилдаг. Үндсэн
програмын код нь Console-ийн цонх дээр ажиллана.
Dev-C++ дээр график санг ажиллуулах
Dev-C++ 4.9.9.2 хувилбар дээр график санг ажиллуулахын
тулд дараах тохиргоог хийнэ.
Сайт : http://www.cs.colorado.edu/~main/bgi/dev-c++/
1. graphics.h санг Dev-C++-ийн include/ хавтсанд хуулна.
2. libbgi.a файлыг lib/ хавтсанд хуулна.
3. Dev-C++ дээр New Project үүсгэж Project Options-ийн
Parameters-ийн Linker талбар дараах холбоосийг нэмнэ.a. -lbgi
b. -lgdi32
c. -lcomdlg32
d. -luuid
e. -loleaut32
f. -lole32
Шинэ прожект үүсгэх
Project Options-ийн Parameters-ийн Linker талбар дараах
холбоосийг нэмэх
Жишээ
#include <graphics.h>
int main( )
{
initwindow(400, 300, "First Sample");
circle(100, 50, 40);
system("pause");
return 0;
}
Дэлгэцийн координат
(0,0) (400,0)
(0,300) (400,300)
(200,150)
Графикийн функцууд
http://www.softwareandfinance.com/Turbo_C/Graphics/
Хэрэгцээт функцууд
• cleardevice()
• getmaxx()
• getmaxy()
• delay(millsecond) // = Sleep(delay(millsecond)
• setcolor(RED)
• setfillstyle(SOLID_FILL, BLUE);
• circle(x, y, R);
• fillellipse(x, y, R1, R2);
• rectangle(x,y,width,height);
• bar(x1,1,x2,y2);
• drawpoly(maxpoints/2, poly);
• outtextxy(x,y,"text"); // only char array
• closegraph()
Өнгө Тоон утга
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
Битүү дүрсний өнгөний будалт Setfillstyle
1. SOLID_FILL
2. LINE_FILL
3. LTSLASH_FILL
4. SLASH_FILL
5. BKSLASH_FILL
6. LTBKSLASH_FILL
7. HATCH_FILL
8. XHATCH_FILL
9. INTERLEAVE_FILL
10.WIDE_DOT_FILL
11.CLOSE_DOT_FILL
setfillstyle(XHATCH_FILL, YELLOW);
#include <graphics.h>
#include <math.h>
int main()
{
initwindow(600, 600, "SetFillStyle");
while(!kbhit())
{
setcolor(WHITE);
setfillstyle(rand() % 11, rand() % 15);
bar(100, 100, 500, 500);
delay(500);
}
closegraph();
return 0;
}
Олон өнцөгтийн жишээ:
#include <graphics.h>
#include <iostream>
#include <time.h>
using namespace std;
int main( )
{
srand(time(NULL));
initwindow(400, 300, “Polygon example");
int maxpoints = 24;
int poly[maxpoints];
int maxx = getmaxx() - 50;
int maxy = getmaxy() - 50;
for(int i = 0; i < maxpoints - 2; i += 2)
{
poly[i] = (50 + rand()) % maxx;
poly[i+1] = (50 + rand()) % maxy;
}
poly[maxpoints - 2] = poly[0];
poly[maxpoints - 1] = poly[1];
drawpoly(maxpoints/2, poly);
system("pause");
return 0;
}
Холбоотой шулуунууд зурах
#include <graphics.h>
#include <iostream>
#include <time.h>
using namespace std;
int main( )
{
srand(time(NULL));
initwindow(600, 400, "Star example");
moveto(300, 50);
lineto(400, 290);
lineto(140, 120);
lineto(460, 120);
lineto(200, 290);
lineto(300, 50);
system("pause");
return 0;
}
Дүрсийг хөдөлгөөнт байдлаар харагдуулахын тулд бага хугацааны
зогсолттойгоор дүрсийг давтан зурна. Үүнд:delay(millsecond);
clearviewport(); графикын функцыг ашиглана.
#include <graphics.h>
using namespace std;
class Circle{
public:
int x, y, dx, dy, R;
Circle(){
R=20+rand()%20;
x=10+R+rand()%getmaxx()/2;
y=10+R+rand()%getmaxy()/2;
dx=2+rand()%4;
if(rand()%2) dx=-dx;
dy=2+rand()%4;
if(rand()%2) dy=-dy;
}
void drawCircle(){
circle(x, y, R);
x+=dx;
y+=dy;
if(x<0+R || getmaxx()-R<x)
dx=-dx;
if(y<0+R || getmaxy()-R<y)
dy=-dy;
}
};
int main( )
{
srand(time(NULL));
initwindow(800, 600, "Circle class");
int size=20;
Circle cr[size];
setfillstyle(SOLID_FILL, BLACK);
while(true){
// clearviewport();
bar(0,0,getmaxx(), getmaxy());
for(int i=0; i<size; i++)
cr[i].drawCircle();
delay(10);
}
return 0;
}
Тоглоомын жишээ#include <process.h>void animateCircle(){
while(true){
cleardevice();
setfillstyle(CLOSE_DOT_FILL , YELLOW);
fillellipse(x, y, R, R);
x+=dx;
y+=dy;
setfillstyle(SOLID_FILL , CYAN);
bar(blockx, getmaxy() - 40, blockx + 100, getmaxy() - 20);
if(x<0+R || getmaxx()-R<x)
dx=-dx;
if(y<0+R || getmaxy()-R<y)
dy=-dy;
if(y + R == getmaxy() - 40){
if(blockx - 20 < x && x <= blockx + 120)
dy=-dy;
}
delay(20);
}
}
Circle *cr;
void StartBounce(void* p){
cr=new Circle();
while(true)
cr->animateCircle();
}
int main( )
{
srand(time(NULL));
char kv;int grd, grm;
initwindow(600, 400, "Bounce");
_beginthread(StartBounce, 0, 0);
while(true){
kv=getche();
if(kv==75){
cr->blockx-=5;
if(cr->blockx < 0) cr->blockx = 0;
}
if(kv==77){
cr->blockx+=5;
if(cr->blockx + 100 > getmaxx()) cr->blockx =
getmaxx() - 100;
}
}
closegraph();
return 0;
}
Төгсөв