home *** CD-ROM | disk | FTP | other *** search
-
-
- /*
- Program Four
- Bert Nelson
- Copyright 1992 Bert Nelson
-
- Compile Instructions: cc file.c -lX11 -lm -o file
-
- Run Instructions: type 'file' after compiling and click an item
- from the menu bar to do an option
-
- Program Purpose: Display how multiple graphical transformations
- are done. The program will ask the user to
- draw the figure and then ask for transformations
- will display the original figure in red while
- the transformed figure is in blue.
-
-
-
- Permission to use, copy, modify, and distribute this software and its
- documentation for any purpose and without fee is hereby granted,
- provided that the above copyright notice appear in all copies and that
- both that copyright notice and this permission notice appear in
- supporting documentation. It is provided "as is" without any express or
- implied warranty.
-
- Bert Nelson DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
- ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
- Bert Nelson BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
- ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
- WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
- ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- SOFTWARE.
-
-
- */
-
- #define LEFT 1
- #define MIDDLE 2
- #define RIGHT 3
- #define XOFF 500
- #define YOFF 350
- #define TRUE 1
- #define FALSE 0
-
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/cursorfont.h>
- #include <string.h>
- #include <math.h>
- #include <X11/keysym.h>
- #define PI 3.141592654 /* set up PI defintion */
-
- int rotate_angle; /* of rotation */
- int rotate_x;
- int rotate_y;
-
-
- float scalef_x;
- float scalef_y;
- int scale_x;
- int scale_y;
-
-
- int x_off = 500;
- int y_off = 350;
-
-
- struct
- trans
- {
- int x;
- int dummy;
- int y;
- };
-
-
- /* set strings as global */
- int sides;
-
- char hello[] = "Transformations II - by Bert Nelson";
- char enter_str[] = "Enter";
- char trans_str[] = "Translate";
- char scale_str[] = "Scale";
- char rotate_str[] = "Rotate";
- char view_str[] = "View";
- char reset_str[] = "Reset";
- char quit_str[] = "Quit";
- Font font;
- int number;
- struct trans translate;
-
- XPoint points[9];
- XPoint newpoints[9];
-
- unsigned long black,white;
- float matrix[3][3];
- float tmatrix[3][3];
-
-
-
- static char *ncolors[9] = {
- " ", "blue","red","green",
- "magenta1","brown","chocolate",
- "gold1","DarkOrchid4"};
- Colormap color_map;
- XColor colors[9];
- XColor exact_colors[9];
-
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- Display *mydisplay;
- Window mywindow,child1,child2,child3,child4,child5,child6,child7;
- GC enter_gc,mygc;
- XEvent myevent;
- KeySym mykey;
- XSizeHints myhint;
- XGCValues gvalues;
- XComposeStatus cs;
- char string[20];
- char newstring[20];
- int nchar;
- int count;
-
- int myscreen;
- unsigned long myforeground, mybackground;
- int i;
- char text[10];
- int done;
-
-
- /* set up display and foreground/background */
-
- mydisplay = XOpenDisplay("");
- make_identity2(mydisplay);
-
-
-
-
- myscreen = DefaultScreen (mydisplay);
- white = mybackground = WhitePixel (mydisplay, myscreen);
- black = myforeground = BlackPixel (mydisplay, myscreen);
-
- /* create a window with a size of 1000 x 700 */
-
- myhint.x = 0;
- myhint.y = 0;
- myhint.width = 1000;
- myhint.height = 700;
- myhint.flags = PPosition | PSize;
- mywindow = XCreateSimpleWindow (mydisplay,
- DefaultRootWindow (mydisplay),
- myhint.x, myhint.y, myhint.width, myhint.height,
- 5, myforeground, mybackground);
-
-
- /* create children windows that will act as menu buttons */
-
- child1 = XCreateSimpleWindow (mydisplay,mywindow,
- 0,0,100,25,2,myforeground,mybackground);
-
-
- child2 = XCreateSimpleWindow (mydisplay,mywindow,
- 100,0,100,25,2,myforeground,mybackground);
-
-
- child3 = XCreateSimpleWindow (mydisplay,mywindow,
- 200,0,100,25,2,myforeground,mybackground);
-
-
- child4 = XCreateSimpleWindow (mydisplay,mywindow,
- 300,0,100,25,2,myforeground,mybackground);
-
-
- child5 = XCreateSimpleWindow (mydisplay,mywindow,
- 400,0,100,25,2,myforeground,mybackground);
-
-
- child6 = XCreateSimpleWindow (mydisplay,mywindow,
- 500,0,100,25,2,myforeground,mybackground);
-
-
- child7 = XCreateSimpleWindow (mydisplay,mywindow,
- 600,0,200,25,2,myforeground,mybackground);
-
- /* print program name at the top */
-
- XSetStandardProperties (mydisplay, mywindow, hello, hello,
- None, argv, argc, &myhint);
-
- /* set up foreground/backgrounds and set up colors */
-
-
- mygc = XCreateGC (mydisplay, mywindow, 0, 0);
-
- gvalues.foreground = myforeground;
- gvalues.background = mybackground;
-
- enter_gc = XCreateGC(mydisplay,child1,0,0);
-
- color_set_up(mydisplay);
-
- XSetBackground (mydisplay, mygc, mybackground);
- XSetForeground (mydisplay, mygc, myforeground);
- XSetBackground (mydisplay,enter_gc,myforeground);
- XSetForeground (mydisplay,enter_gc,mybackground);
-
-
- /* Load up a font called 8x16 */
-
- font = XLoadFont(mydisplay,"8x16");
- XSetFont(mydisplay,mygc,font);
- XSetFont(mydisplay,enter_gc,font);
-
- /* Ask for input from the mouse and keyboard */
-
- XSelectInput (mydisplay, mywindow,
- ButtonPressMask | KeyPressMask | ExposureMask);
-
- /* map window to the screen */
- XMapRaised (mydisplay, mywindow);
-
- /* Map children windows to the screen */
-
- XMapRaised (mydisplay, child1);
- XMapRaised (mydisplay, child2);
- XMapRaised (mydisplay, child3);
- XMapRaised (mydisplay, child4);
- XMapRaised (mydisplay, child5);
- XMapRaised (mydisplay, child6);
- XMapRaised (mydisplay, child7);
-
- /* Display menu strings */
-
- XNextEvent(mydisplay,&myevent);
-
- XDrawImageString(mydisplay,child1,mygc,
- 25,20,enter_str,strlen(enter_str));
-
-
- XDrawImageString(mydisplay,child2,mygc,
- 5,20,trans_str,strlen(trans_str));
-
-
- XDrawImageString(mydisplay,child3,mygc,
- 25,20,scale_str,strlen(scale_str));
-
-
- XDrawImageString(mydisplay,child4,mygc,
- 25,20,rotate_str,strlen(rotate_str));
-
-
- XDrawImageString(mydisplay,child5,mygc,
- 25,20,view_str,strlen(view_str));
-
- XDrawImageString(mydisplay,child6,mygc,
- 25,20,reset_str,strlen(reset_str));
-
- XDrawImageString(mydisplay,child7,mygc,
- 85,20,quit_str,strlen(quit_str));
-
-
- /* Loop through until a q is press when the cursor is in
- the window, which will cause the application to quit */
-
- done = 0;
- while (done == 0) {
- XNextEvent (mydisplay, &myevent );
-
- if (myevent.type == ButtonPress)
- {
-
- if (myevent.xbutton.subwindow == child1)
- {
- make_identity2(mydisplay);
- process_enter(mydisplay,mywindow,
- child1,mygc,enter_gc);
-
- }
-
- if (myevent.xbutton.subwindow == child2)
-
- process_translate(mydisplay,mywindow,child2,
- mygc,enter_gc);
-
- if (myevent.xbutton.subwindow == child3)
- process_scale(mydisplay,mywindow,child3,
- mygc,enter_gc);
-
- if (myevent.xbutton.subwindow == child4)
- process_rotate(mydisplay,mywindow,child4,
- mygc,enter_gc);
-
- if (myevent.xbutton.subwindow == child5)
-
- process_view(mydisplay,mywindow,child5,
- mygc,enter_gc);
-
-
- if (myevent.xbutton.subwindow == child6)
-
- process_reset(mydisplay,mywindow,child6,
- mygc,enter_gc);
-
-
- if (myevent.xbutton.subwindow == child7)
-
-
- {
- done = 1;
- break;
- }
-
- }
-
-
- if (myevent.xany.window == mywindow)
- {
- switch (myevent.type) {
-
- case KeyPress:
-
- {
- i = XLookupString (&myevent,
- text, 10, &mykey, 0);
- if (i == 1 && text[0] == 'q')
- done = 1;
- break;
-
- } /* case */
- } /* switch */
-
- } /* end if */
- } /* while */
-
- /* Free up and clean up the windows created */
-
- XFreeGC (mydisplay, mygc);
- XDestroyWindow (mydisplay, mywindow);
- XCloseDisplay (mydisplay);
-
- exit(0);
-
-
- }
-
- /* Ask for the user to enter the number of sides and the points
- for each side. By definition there are n+1 sides in reality because
- you need to draw back to where you started from, so the last point
- is assigned to the first point, which makes entering the points
- in order important */
-
-
- process_enter(display,parent,child,parent_gc,child_gc)
-
- Display *display;
- Window parent,child;
- GC parent_gc,child_gc;
- {
-
- int i,j,offy,offx;
- Window enter_window;
- XEvent enter_event;
- Font enter_font;
- GC enter_gc;
- int count;
- char text[10];
- int string[10];
- KeySym key;
- int old_x;
- int old_y;
- Cursor curs;
-
- /* Flash the Enter button on and off */
-
- XClearWindow(display,child);
- XFillRectangle(display,child,parent_gc,0,0,100,25);
- XDrawImageString(display,child,child_gc,
- 25,20,enter_str,strlen(enter_str));
- XFlush(display);
- for (i=0; i<1000; i++)
- XNoOp(display);
- XClearWindow(display,child);
- XDrawImageString(display,child,parent_gc,
- 25,20,enter_str,strlen(enter_str));
-
-
- /* change cursor to a cross hair */
-
- curs = XCreateFontCursor(display,XC_crosshair);
- XDefineCursor(display,parent,curs);
-
-
- XSelectInput(display,parent,ButtonPressMask|KeyPressMask);
-
- /* Draw x and y axis */
-
- XDrawLine(display,parent,parent_gc,XOFF,0,XOFF,YOFF*2);
- XDrawLine(display,parent,parent_gc,0,YOFF,XOFF*2,YOFF);
-
- /* Draw notches in x and y axis */
-
- for (i=25; i<YOFF*2; i=i+25)
- XDrawLine(display,parent,parent_gc,
- XOFF-5,i,XOFF+5,i);
-
- for (i=25; i<XOFF*2; i= i+25)
- XDrawLine(display,parent,parent_gc,
- i,YOFF-5,i,YOFF+5);
- do
- {
- XNextEvent(display,&enter_event);
-
- if (enter_event.type == ButtonPress &&
- enter_event.xbutton.button == LEFT)
-
- {
- old_x = enter_event.xbutton.x;
- old_y = enter_event.xbutton.y;
-
- }
-
-
- } while (enter_event.type != ButtonPress);
-
-
- i = 0;
-
-
-
- do
- {
- XNextEvent(display,&enter_event);
-
- if (enter_event.type = ButtonPress &&
- enter_event.xbutton.button == LEFT)
-
- {
-
- points[i].x = enter_event.xbutton.x ;
- points[i].y = enter_event.xbutton.y;
- XFlush(display);
-
-
- XDrawLine(display,parent,parent_gc,old_x,old_y,
- points[i].x,points[i].y);
-
- old_x = points[i].x;
- old_y = points[i].y;
-
-
- i = i + 1;
-
- }
- } while (enter_event.xbutton.button != RIGHT);
-
-
- sides = 0;
-
- sides = i;
-
-
- for (i=0; i<sides; i++)
- {
-
-
- if ( points[i].x > XOFF)
- points[i].x = points[i].x - XOFF;
- else
- points[i].x = -1 * abs (points[i].x - XOFF);
-
- /*
- points[i].x = -1 * abs(XOFF - points[i].x);
- */
- if ( points[i].y > YOFF)
- points[i].y = YOFF - points[i].y;
- else
- points[i].y = abs (YOFF - points[i].y);
-
-
-
-
- }
-
- /* clear screen and exit if less than 3 sides or greater than 8 */
-
- if (sides <3 || sides > 8)
- {
- XUndefineCursor(display,parent);
-
- sides = 0;
- XClearWindow(display,parent);
- return(0);
- }
-
- /* change the mouse pointer back to normal */
-
- XUndefineCursor(display,parent);
-
- }
-
- /* Ask user to enter in the x and y translation factors for the figure */
-
-
- process_translate(display,parent,child,parent_gc,child_gc)
-
- Display *display;
- Window parent,child;
- GC parent_gc,child_gc;
- {
-
- int i,off;
- Window trans_window;
- XEvent trans_event;
- Font trans_font;
- GC trans_gc;
- int count;
- char text[10];
- int string[10];
- KeySym key=0;
- Cursor curs;
-
- /* Flash the Translate button on and off */
-
- XClearWindow(display,child);
- XFillRectangle(display,child,parent_gc,0,0,100,25);
- XDrawImageString(display,child,child_gc,
- 5,20,trans_str,strlen(trans_str));
- XFlush(display);
- for (i=0; i<1000; i++)
- XNoOp(display);
- XClearWindow(display,child);
- XDrawImageString(display,child,parent_gc,
- 5,20,trans_str,strlen(trans_str));
-
-
- XClearWindow(display,parent);
-
-
- /* Draw x and y axis */
-
- XDrawLine(display,parent,parent_gc,XOFF,0,XOFF,YOFF*2);
- XDrawLine(display,parent,parent_gc,0,YOFF,XOFF*2,YOFF);
-
- /* Draw notches in x and y axis */
-
- for (i=25; i<YOFF*2; i=i+25)
- XDrawLine(display,parent,parent_gc,
- XOFF-5,i,XOFF+5,i);
-
- for (i=25; i<XOFF*2; i= i+25)
- XDrawLine(display,parent,parent_gc,
- i,YOFF-5,i,YOFF+5);
-
-
-
-
- /* change the cursor to a crosss */
-
- curs = XCreateFontCursor(display,XC_cross);
- XDefineCursor(display,parent,curs);
-
-
-
-
-
- do
- {
- XNextEvent(display,&trans_event);
-
- if (trans_event.type == ButtonPress &&
- trans_event.xbutton.button == LEFT)
-
- {
- translate.x = trans_event.xbutton.x;
- translate.y = trans_event.xbutton.y;
-
- }
-
-
- } while (trans_event.type != ButtonPress);
-
-
- if ( translate.x > XOFF)
- translate.x = translate.x - XOFF;
- else
- translate.x = -1 * abs (translate.x - XOFF);
-
- if ( translate.y > YOFF)
- translate.y = YOFF - translate.y;
- else
- translate.y = abs (YOFF - translate.y);
-
-
-
-
- /* do the translation */
-
- make_identity(display);
-
- matrix[2][0] = translate.x;
- matrix[2][1] = translate.y;
-
- combine_transformations(display);
-
-
- /* return the cursor to the default one */
-
- XUndefineCursor(display,parent);
-
-
- }
-
-
-
- /* Prompt user to input scale factors and where to scale about.
- Note: A scale factor of 1 = current size, but a scale factor
- of zero will cause nothing to be displayed */
-
- process_scale(display,parent,child,parent_gc,child_gc)
-
-
- Display *display;
- Window parent,child;
- GC parent_gc,child_gc;
- {
-
- int i;
-
- Window scale_window;
- XEvent scale_event;
- Font scale_font;
- GC scale_gc;
- int count;
- char text[10];
- int string[10];
- KeySym key=0;
- Cursor curs;
-
-
- /* turn on and off the scale button */
-
- XClearWindow(display,child);
- XFillRectangle(display,child,parent_gc,0,0,100,25);
- XDrawImageString(display,child,child_gc,
- 25,20,scale_str,strlen(scale_str));
- XFlush(display);
- for (i=0; i<10; i++)
- XNoOp(display);
- XClearWindow(display,child);
- XDrawImageString(display,child,parent_gc,
- 25,20,scale_str,strlen(scale_str));
-
-
- /* Create pop up window for the user */
-
- scale_window = XCreateSimpleWindow(display,parent,340,200,
- 300,200,3,black,white);
-
- scale_gc = parent_gc;
-
-
- XMapRaised(display,scale_window);
- XClearWindow(display,scale_window);
-
- XDrawString(display,scale_window,parent_gc,
- 95,25,"Scale Figure",12);
-
-
- XSelectInput(display,scale_window,ButtonPressMask|KeyPressMask);
-
-
-
- /* Load up a smaller font */
-
- font = XLoadFont(display,"7x13");
- XSetFont(display,scale_gc,font);
-
- /* prompt user for input */
-
-
- XDrawString(display,scale_window,scale_gc,
- 15,75,"Enter X Factor of Scaling: ",
- 27);
-
-
- /* set up values */
-
-
- XFlush(display);
- key = 0;
- text[0] = '\0';
- string[0] = '\0';
- i = 195;
-
-
- /* accept a series of characters until the RETURN key is pressed */
-
- do
- {
- i = i + 8;
-
- XNextEvent(display,&scale_event);
-
- count = XLookupString(&scale_event,text,10,&key,0);
-
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,scale_window,scale_gc,
- i,75,text,1);
-
- strcat(string,text);
- } while (key != XK_Return && scale_event.type == KeyPress);
-
- /* convert a string to a floating point number so fractional
- values can be used e.g. .4, .25, 1.5 etc. */
-
- scalef_x = atof(string);
-
-
- /* Clear the buffers and ask for a Y scale factor */
-
- XFlush(display);
-
- XDrawString(display,scale_window,scale_gc,
- 15,105,"Enter Y Factor of Scaling: ",
- 27);
- while (scale_event.type != KeyPress)
- XNextEvent(display,&scale_event);
- key = 0;
- text[0] = '\0';
- string[0] = '\0';
-
-
- /* ask for input until a RETURN key is pressed */
-
- i = 195;
-
- do
- {
- i = i + 8;
- XNextEvent(display,&scale_event);
- count = XLookupString(&scale_event,text,10,&key,0);
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,scale_window,scale_gc,
- i,105,text,1);
- strcat(string,text);
- } while (key != XK_Return && scale_event.type == KeyPress);
-
- /* convert input to a floating point number */
-
- scalef_y = atof(string);
-
- XFlush(display);
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,scale_gc,font);
-
-
-
- /* trash the window */
-
- XDestroyWindow(display,scale_window);
-
- XClearWindow(display,parent);
-
-
- /* draw x and y axis */
-
- XDrawLine(display,parent,parent_gc,XOFF,0,XOFF,YOFF*2);
- XDrawLine(display,parent,parent_gc,0,YOFF,XOFF*2,YOFF);
-
- /* Draw notches in x and y axis */
-
- for (i=25; i<YOFF*2; i=i+25)
- XDrawLine(display,parent,parent_gc,
- XOFF-5,i,XOFF+5,i);
-
- for (i=25; i<XOFF*2; i= i+25)
- XDrawLine(display,parent,parent_gc,
- i,YOFF-5,i,YOFF+5);
-
-
- XSelectInput(display,parent,ButtonPressMask|KeyPressMask);
-
-
- /* change the cursor to a crosss */
-
- curs = XCreateFontCursor(display,XC_cross_reverse);
- XDefineCursor(display,parent,curs);
-
- /* ask user to enter a point to rotate about */
-
- do
- {
- XNextEvent(display,&scale_event);
-
- if (scale_event.type == ButtonPress &&
- scale_event.xbutton.button == LEFT)
- {
- scale_x = scale_event.xbutton.x;
- scale_y = scale_event.xbutton.y;
- }
- } while (scale_event.type != ButtonPress);
-
-
- /* revert back to default cursor */
-
- XUndefineCursor(display,parent);
-
-
- /* change the values back to something the program can use */
-
-
- if ( scale_x > XOFF)
- scale_x = scale_x - XOFF;
- else
- scale_x = -1 * abs (scale_x - XOFF);
-
-
- if ( scale_y > YOFF)
- scale_y = YOFF - scale_y;
- else
- scale_y = abs (YOFF - scale_y);
-
- /* set up the identity matrix */
-
- make_identity(display);
-
-
- /* Assign matrix values */
-
- matrix[0][0] = scalef_x;
- matrix[1][1] = scalef_y;
- matrix[2][0] = (1-scalef_x) * scale_x;
- matrix[2][1] = (1-scalef_y) * scale_y;
-
- /* Multiply matrices */
-
- combine_transformations(display);
-
- }
-
- /* Promp the user to enter an angle in DEGREES! and a point to
- rotate the figure about */
-
-
- process_rotate(display,parent,child,parent_gc,child_gc)
-
-
- Display *display;
- Window parent,child;
- GC parent_gc,child_gc;
- {
-
-
- int i,off;
- Window rotate_window;
- XEvent rotate_event;
- Font rotate_font;
- GC rotate_gc;
- int count;
- char text[10];
- int string[10];
- KeySym key=0;
- float radians;
- double tempcos;
- double tempsin;
- Cursor curs;
-
- /* Flash rotate button on and off */
-
- XClearWindow(display,child);
- XFillRectangle(display,child,parent_gc,0,0,100,25);
- XDrawImageString(display,child,child_gc,
- 25,20,rotate_str,strlen(rotate_str));
- XFlush(display);
- for (i=0; i<1000; i++)
- XNoOp(display);
- XClearWindow(display,child);
- XDrawImageString(display,child,parent_gc,
- 25,20,rotate_str,strlen(rotate_str));
-
-
- XClearWindow(display,parent);
-
-
- /* create a pop up window */
-
- rotate_window = XCreateSimpleWindow(display,parent,340,200,
- 300,100,3,black,white);
-
- rotate_gc = parent_gc;
-
-
- XMapRaised(display,rotate_window);
- XClearWindow(display,rotate_window);
-
- XDrawString(display,rotate_window,parent_gc,
- 35,25,"Rotate Figure (in Degrees)",27);
-
- XSelectInput(display,rotate_window,ButtonPressMask
- |KeyPressMask);
-
- /* load up a small font */
-
- font = XLoadFont(display,"7x13");
- XSetFont(display,rotate_gc,font);
-
- XDrawString(display,rotate_window,rotate_gc,
- 35,75,"Enter Angle of Rotation: ",
- 23);
-
- key = 0;
- text[0] ='\0';
-
- /* input an angle, which is later converted to radians */
-
- i = 200;
-
- do
- {
- i = i + 8;
- XNextEvent(display,&rotate_event);
- count = XLookupString(&rotate_event,text,10,&key,0);
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,rotate_window,rotate_gc,
- i,75,text,1);
- strcat(string,text);
- } while (key != XK_Return && rotate_event.type == KeyPress);
-
- rotate_angle = strtol(string,NULL,10);
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,rotate_gc,font);
-
- XDestroyWindow(display,rotate_window);
-
-
- /* draw x and y axis */
-
- XDrawLine(display,parent,parent_gc,XOFF,0,XOFF,YOFF*2);
- XDrawLine(display,parent,parent_gc,0,YOFF,XOFF*2,YOFF);
-
- /* Draw notches in x and y axis */
-
- for (i=25; i<YOFF*2; i=i+25)
- XDrawLine(display,parent,parent_gc,
- XOFF-5,i,XOFF+5,i);
-
- for (i=25; i<XOFF*2; i= i+25)
- XDrawLine(display,parent,parent_gc,
- i,YOFF-5,i,YOFF+5);
-
- /* change the cursor to a crosss */
-
- curs = XCreateFontCursor(display,XC_diamond_cross);
- XDefineCursor(display,parent,curs);
-
- /* ask user to enter a point to rotate about */
-
- do
- {
- XNextEvent(display,&rotate_event);
-
- if (rotate_event.type == ButtonPress &&
- rotate_event.xbutton.button == LEFT)
-
- {
- rotate_x = rotate_event.xbutton.x;
- rotate_y = rotate_event.xbutton.y;
-
- }
-
-
- } while (rotate_event.type != ButtonPress);
-
- if ( rotate_x > XOFF)
- rotate_x = rotate_x - XOFF;
- else
- rotate_x = -1 * abs (rotate_x - XOFF);
-
-
- if ( rotate_y > YOFF)
- rotate_y = YOFF - rotate_y;
- else
- rotate_y = abs (YOFF - rotate_y);
-
-
-
-
- XUndefineCursor(display,parent);
- /* set up the identity */
-
- make_identity(display);
-
- /* assign values */
-
- radians = rotate_angle * (PI/180);
- tempcos = cos(radians);
- tempsin = sin(radians);
- matrix[0][0] = tempcos;
- matrix[0][1] = tempsin;
-
- radians = -1.0 * radians;
-
- matrix[1][0] = sin(radians);
- matrix[1][1] = tempcos;
-
- matrix[2][0] = rotate_x;
- matrix[2][1] = rotate_y;
-
- /* multiply matrices */
-
- combine_transformations(display);
-
-
- }
-
-
- /* Draw X and Y axis with notches every 25 pixels and display the
- original figure in red and the transformed figure in blue */
-
-
-
- process_view(display,parent,child,parent_gc,child_gc)
-
-
- Display *display;
- Window parent,child;
- GC parent_gc,child_gc;
- {
-
- int i;
- int xoff;
- int yoff;
- XPoint temp_points[9];
-
- XClearWindow(display,child);
- XFillRectangle(display,child,parent_gc,0,0,100,25);
- XDrawImageString(display,child,child_gc,
- 25,20,view_str,strlen(view_str));
- XFlush(display);
- for (i=0; i<1000; i++)
- XNoOp(display);
- XClearWindow(display,child);
- XDrawImageString(display,child,parent_gc,
- 25,20,view_str,strlen(view_str));
-
- /* Draw x and y axis */
-
- XDrawLine(display,parent,parent_gc,XOFF,0,XOFF,YOFF*2);
- XDrawLine(display,parent,parent_gc,0,YOFF,XOFF*2,YOFF);
-
- /* Draw notches in x and y axis */
-
- for (i=25; i<YOFF*2; i=i+25)
- XDrawLine(display,parent,parent_gc,
- XOFF-5,i,XOFF+5,i);
-
- for (i=25; i<XOFF*2; i= i+25)
- XDrawLine(display,parent,parent_gc,
- i,YOFF-5,i,YOFF+5);
-
-
-
-
- for (i=0; i<sides+1; i++)
- {
- temp_points[i].x = points[i].x + XOFF;
- temp_points[i].y = YOFF - points[i].y;
- }
- /* set foreground to blue */
-
- XSetForeground(display,parent_gc,colors[2].pixel);
-
- /* Draw the original figure */
-
- XFillPolygon(display,parent,parent_gc,temp_points,sides,Complex,
- CoordModeOrigin);
-
- /* Modify the original figure to a transformed one */
-
- transform_points(display);
-
- /* change foreground color to blue */
-
- XSetForeground(display,parent_gc,colors[1].pixel);
-
- XFillPolygon(display,parent,parent_gc,newpoints,sides,Complex,
- CoordModeOrigin);
-
- /* reset the color back to black */
-
- XSetForeground(display,parent_gc,black);
-
-
- for (xoff=0; xoff<sides+1; xoff++);
- printf("(x,y) = %d,%d\n",
- newpoints[xoff].x,newpoints[xoff].y);
-
-
-
-
-
-
- }
-
-
-
-
- /* clear the screen of the old figures and reset everything */
-
-
- process_reset(display,parent,child,parent_gc,child_gc)
-
-
- Display *display;
- Window parent,child;
- GC parent_gc,child_gc;
- {
-
- int i;
-
- /* flash the reset button */
-
- XClearWindow(display,child);
- XFillRectangle(display,child,parent_gc,0,0,100,25);
- XDrawImageString(display,child,child_gc,
- 25,20,reset_str,strlen(reset_str));
- XFlush(display);
- for (i=0; i<1000; i++)
- XNoOp(display);
- XClearWindow(display,child);
- XDrawImageString(display,child,parent_gc,
- 25,20,reset_str,strlen(reset_str));
-
- XFlush(display);
-
- /* reset everything */
-
- for (i=0; i<sides+1; i++)
- {
- points[i].x = 0;
- points[i].y = 0;
- newpoints[i].x = 0;
- newpoints[i].y = 0;
-
- }
- sides = 0;
-
- translate.x = 0;
- translate.y = 0;
-
- rotate_angle = 0;
- rotate_x = 0;
- rotate_y = 0;
-
- scalef_x = 0;
- scalef_y = 0;
-
- scale_x = 0;
- scale_y = 0;
-
-
- XClearWindow(display,parent);
-
-
-
- }
-
-
-
- combine_transformations(display)
- Display *display;
-
- {
-
-
- int row,col;
- float temp[3][3];
-
-
- XFlush(display);
- for (row = 0; row<3; row++)
- {
- for (col=0; col<3; col++)
- {
-
-
-
- temp[row][col] = tmatrix[row][0]*matrix[0][col] +
- tmatrix[row][1] * matrix[1][col]
- + tmatrix[row][2]* matrix[2][col];
- }
-
- }
-
- for (row = 0; row<3; row++)
- {
- for (col = 0; col<3; col++)
- {
-
-
- tmatrix[row][col] = temp[row][col];
-
- }
- }
- }
- make_identity(display)
- Display *display;
-
- {
-
-
- int row,col;
-
- XFlush(display);
-
-
- for (row=0; row<3; row++)
- for (col=0; col<3; col++)
- if (row == col)
- matrix[row][col] = 1;
- else
- matrix[row][col] = 0;
-
-
-
- }
-
-
-
-
-
- make_identity2(display)
- Display *display;
-
- {
-
-
- int row,col;
-
- XFlush(display);
-
- for (row=0; row<3; row++)
- for (col=0; col<3; col++)
- if (row == col)
- tmatrix[row][col] = 1;
- else
- tmatrix[row][col] = 0;
-
-
-
- }
-
-
- transform_points( display )
- Display *display;
- {
-
- int i;
- XFlush(display);
- for (i=0; i<sides+1; i++)
- {
-
-
- newpoints[i].x = points[i].x * tmatrix[0][0] +
- points[i].y * tmatrix[1][0] + tmatrix[2][0] + XOFF;
-
- newpoints[i].y = YOFF - ( points[i].x * tmatrix[0][1] +
- points[i].y * tmatrix[1][1] + tmatrix[2][1]);
-
-
-
-
- }
-
- }
-
-
-
- /* Create a set of colors from named colors */
-
- color_set_up(the_display)
-
- Display *the_display;
-
- {
-
- int i;
-
- color_map = DefaultColormap(the_display,0);
-
- /* get the colors from the color map for the colors named */
-
- for (i=1; i<5; i++)
- {
-
- XAllocNamedColor(the_display,color_map,ncolors[i],
- &exact_colors[i],&colors[i]);
-
- }
-
-
- }
-