/* Compile with: gcc xmirror.c -o a -L../lib -L/usr/X11R6/lib -lnr -lm -lX11 */ #include #include #include #include #include #include #include #include /* Every Xlib program must include this */ #include #include "../util/nr.h" #include "../util/nrutil.h" #include "../util/matutl6.c" #define DISP_W 600 #define DISP_H 600 #define BEAM_W 0.004 #define EPS 1.0e-10 #define NIL 0 typedef struct vec { double x, y; } vec; typedef struct mirror { vec a,b,normal; } mirror; Display *dpy; Window w; GC gc; int blackColor,whiteColor,redColor,glassColor; void Xinit_display(void) { /* Open the display */ dpy = XOpenDisplay(NIL); assert(dpy); /* Get some colors */ blackColor = BlackPixel(dpy, DefaultScreen(dpy)); whiteColor = WhitePixel(dpy, DefaultScreen(dpy)); glassColor = 0xfff; redColor = 0xff000; /* Create the window */ w= XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, DISP_W, DISP_H, 0, blackColor,blackColor); /* We want to get MapNotify events */ XSelectInput(dpy, w, StructureNotifyMask); /* "Map" the window (that is, make it appear on the screen) */ XMapWindow(dpy, w); /* Create a "Graphics Context" */ gc = XCreateGC(dpy, w, 0, NIL); /* Tell the GC we draw using the white color */ XSetForeground(dpy, gc, whiteColor); /* Wait for the MapNotify event */ for(;;) { XEvent e; XNextEvent(dpy, &e); if (e.type == MapNotify) break; } } void Xdraw_mirrors(mirror *m, int mirrors) { int i,j,corr; vec p; float t,mirror_w=BEAM_W*2.0; corr=(int)((mirror_w*DISP_W)/2.0); XSetForeground(dpy, gc, glassColor); for(j=0;j=0.0-EPS) && (temp.x<=1.0+EPS) && (temp.y>=0.0-EPS) && (temp.y<=1.0+EPS)) { i_min=-1; t_min=sqrt(2.0); temp=u; for (i=0;iEPS)) { t_min=t; i_min=i; } } if (i_min >=0) { u.x += t_min*v.x; u.y += t_min*v.y; d = v.x*m[i_min].normal.x + v.y*m[i_min].normal.y; v.x -= 2*d*m[i_min].normal.x; v.y -= 2*d*m[i_min].normal.y; } else { u.x += t_min*v.x; u.y += t_min*v.y; Xlaser_beam(temp,u); break; } Xlaser_beam(temp,u); } getchar(); free(m); return 0; }