// (c) Dinko Korunic, 36355514, kreator@fly.srk.fer.hr // Zadatak broj 8 iz predmeta Racunalna grafika #include #include #include #include #include #define MAXX 640 // max x #define MAXY 480 // max y #define MYSTEP 0.001 // stepping class MyDisplay { // klasa MyDisplay u kojoj se nalaze inicijalizacijske xlib // rutine, kao i postavljanje background i foreground boje, itd. XGCValues gcv; int winx, winy; public: Display *disp; Window win; GC gc; XEvent ev; void MyWait(void); int winsx, winsy, screen; unsigned long wp, bp; MyDisplay(int _winx, int _winy, int _winsx, int _winsy); ~MyDisplay(); }; MyDisplay::MyDisplay(int _winx=0, int _winy=0, int _winsx=MAXX, int _winsy=MAXY) : winx(_winx), winy(_winy), winsx(_winsx), winsy(_winsy) { // konstruktor koji sluzi za misc. inicijalizaciju cout << "Initialising graphic subsystem.." << endl; // otvaranje displaya if ((disp=XOpenDisplay(NULL))==NULL) { cerr << "I can't open display." << endl; exit(EXIT_FAILURE); } screen=DefaultScreen(disp); // stvaranje prototipnog prozora win=XCreateSimpleWindow(disp, DefaultRootWindow(disp), winx, winy, winsx, winsy, 1, wp=WhitePixel(disp, screen), bp=BlackPixel(disp, screen)); XSelectInput(disp, win, StructureNotifyMask | KeyPressMask | ExposureMask); // mapiranje prozora XMapWindow(disp, win); while(1) { // cekaj dok nije gotovo XNextEvent(disp, &ev); if (ev.type==MapNotify) break; } gcv.foreground=wp; gcv.background=bp; // stvori gc gc=XCreateGC(disp, win, GCForeground | GCBackground, &gcv); cout << "done." << endl; } MyDisplay::~MyDisplay() { // destruktor za graficki engine cout << "Shutting graphic subsystem.." << endl; XFreeGC(disp, gc); XDestroyWindow(disp, win); XCloseDisplay(disp); cout << "done." << endl; } void MyDisplay::MyWait(void) { // postavi wait na keypress XSelectInput(disp, win, KeyPressMask | ExposureMask); while(1) { XNextEvent(disp, &ev); if (ev.type==KeyPress) break; } } class MiscDraw : public MyDisplay { Colormap cmap; XColor xcolor, colorcell; int darkcolor, lightcolor, black, white; public: XImage *image; long translate[4]; /* for converting colors */ MiscDraw(); }; MiscDraw::MiscDraw() { cmap=DefaultColormap(disp, screen); if (XAllocNamedColor(disp, cmap, "red", &colorcell, &xcolor)) translate[0]=colorcell.pixel; if (XAllocNamedColor(disp, cmap, "green", &colorcell, &xcolor)) translate[1]=colorcell.pixel; if (XAllocNamedColor(disp, cmap, "blue", &colorcell, &xcolor)) translate[2]=colorcell.pixel; if (XAllocNamedColor(disp, cmap, "grey", &colorcell, &xcolor)) translate[3]=colorcell.pixel; image=XGetImage(disp, win, 0, 0, MAXX, MAXY, AllPlanes, ZPixmap); } class MyDraw8 : public MiscDraw { double B(int, double); int Bezier(int *, double); public: MyDraw8(); }; double MyDraw8::B(int i, double t) { switch(i) { case 0: return (1.-t)*(1.-t)*(1.-t); case 1: return 3.*t*(1.-t)*(1.-t); case 2: return 3.*t*t*(1.-t); case 3: return t*t*t; default: return 0.; } } int MyDraw8::Bezier(int *r, double t) { int i; double p=0.; for (i=0; i<4; ++i) p+=r[i]*B(i,t); return p; } MyDraw8::MyDraw8() { int i, rx[4], ry[4]; cout << "Unesi rx i ry" << endl; for (i=0; i<4; ++i) { cout << "Unos #" << i << " podatka" << endl; cin >> rx[i]; cin >> ry[i]; } for (double t=0; t<=1; t+=MYSTEP) XPutPixel(image, Bezier(rx, t), Bezier(ry, t), translate[0]); XPutImage(disp, win, gc, image, 0, 0, 0, 0, MAXX, MAXY); for (i=0; i<3; ++i) XDrawLine(disp, win, gc, rx[i], ry[i], rx[i+1], ry[i+1]); XDrawString(disp, win, gc, 10, 10, "RGF labos br. 8-1", 17); XFlush(disp); MyWait(); } int main(void) { MyDraw8 MyOb; return EXIT_SUCCESS; }