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 a button
- on the mouse to draw (or redraw) the figures
-
- Program Purpose: Display how multiple graphical transformations
- are done. The program will ask the user to
- enter in (x,y) coordinates of the figure and
- 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 XOFF 500
- #define YOFF 350
- #define TRUE 1
- #define FALSE 0
-
- #include <X11/Xlib.h>
- #include <X11/Xutil.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 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;
-
- /* 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));
-
-
- /* set up input window */
-
-
- enter_window = XCreateSimpleWindow(display,parent,340,200,
- 300,320,3,black,white);
-
- enter_gc = parent_gc;
-
-
- XMapRaised(display,enter_window);
- XClearWindow(display,enter_window);
-
-
- XDrawString(display,enter_window,parent_gc,
- 70,25,"Enter Sides/Points",18);
-
-
- XSelectInput(display,enter_window,ButtonPressMask|KeyPressMask);
-
- /* set up a smaller font */
-
-
- font = XLoadFont(display,"7x13");
- XSetFont(display,enter_gc,font);
-
- /* reset everything */
-
- XFlush(display);
- string[0] = '\0';
- key = 0;
-
- /* prompt the user to input the number of sides */
-
-
- XDrawString(display,enter_window,enter_gc,
- 15,45,"Enter Number of Sides: ",
- 23);
-
- /* accept input from the user */
-
- do
-
- {
-
- XNextEvent(display,&enter_event);
- count = XLookupString(&enter_event,text,10,&key,0);
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,enter_window,enter_gc,
- 180,45,text,1);
- strcat(string,text);
-
- }
- while (key != XK_Return &&
- enter_event.type == KeyPress );
-
-
- sides = strtol(string,NULL,10);
-
- /* abort module if there are less than three or greater
- than eight sides to the figure */
-
- if (sides <3 || sides > 8)
- {
-
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,enter_gc,font);
- sides = 0;
- XClearWindow(display,enter_window);
- XDestroyWindow(display,enter_window);
- return(0);
- }
-
- /* Clear everything out of the buffers and reset */
-
- XFlush(display);
- string[0] = '\0';
- key = 0;
- text[0] = '\0';
-
-
- /* Ask user to input a x value for n number of sides */
-
- /* Place the input into a string and convert it to
- a integer */
-
- for (i=0; i<sides; i++)
- {
-
- j = 67;
- XDrawString(display,enter_window,enter_gc,
- 15,65+i*30,"Enter x: ",9);
-
- do
- {
- j = j + 8;
- offx = 65+i*30;
- XNextEvent(display,&enter_event);
- count = XLookupString(&enter_event,text,10,&key,0);
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,enter_window,enter_gc,
- j,offx,text,1);
- strcat(string,text);
- } while (key != XK_Return && enter_event.type == KeyPress);
-
- /* convert the input string to an integer */
-
-
- points[i].x = strtol(string,NULL,10);
-
-
- /* Flush out the buffers and reset everything */
-
-
- XFlush(display);
-
- key = 0;
- string[0] = '\0';
- j = 67;
-
- XDrawString(display,enter_window,enter_gc,
- 15,65+i*30+15,"Enter y: ",9);
-
- /* Ask user for a y value for each side of the figure */
- /* Accept a value only after a RETURN key has been pressed */
-
- do
- {
- j = j + 8; /* move over 8 pixels */
- offy = 80+i*30; /* move down some pixels */
-
- XNextEvent(display,&enter_event); /* ask for input */
-
- count = XLookupString(&enter_event,text,10,&key,0);
- text[count] = '\0';
-
- /* if the return key is NOT pressed display the key
- that was just pressed */
-
- if (key != XK_Return)
- XDrawString(display,enter_window,enter_gc,
- j,offy,text,1);
-
- strcat(string,text);
- } while (key != XK_Return && enter_event.type == KeyPress);
-
-
- /* flush out buffers and reset */
-
-
- points[i].y = strtol(string,NULL,10);
- XFlush(display);
- key = 0;
- string[0] = '\0';
-
-
- }
-
-
-
- /* assign the last points to the first points, so
- the figure will be correctly drawn */
-
-
- points[sides].x = points[0].x;
- points[sides].y = points[0].y;
-
-
- /* Tell the user he must press a key (or a mouse button)
- to exit. Note: for this to work the cursor must
- be inside the pop up window */
-
- XDrawString(display,enter_window,enter_gc,
- 55,65+i*30+10,"Press any key to Exit",22);
-
- /* Ask for input until a key is pressed */
-
- do
- XNextEvent(display,&enter_event);
- while( enter_event.xbutton.window != enter_window);
-
- /* set the font back to what it was before */
- /* don't ask me why, but it works this way */
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,enter_gc,font);
-
- XDestroyWindow(display,enter_window);
- }
-
- /* 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;
-
- XClearWindow(display,parent);
-
- /* 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));
-
- /* Create a pop up window and map it */
-
- trans_window = XCreateSimpleWindow(display,parent,340,200,
- 300,320,3,black,white);
-
- trans_gc = parent_gc;
-
- XMapRaised(display,trans_window);
- XClearWindow(display,trans_window);
-
- XDrawString(display,trans_window,parent_gc,
- 80,25,"Translate Figure",17);
-
- /* Set up mask for input for key presses and mouse clicks */
-
- XSelectInput(display,trans_window,ButtonPressMask|KeyPressMask);
-
-
- /* Load up a smaller font */
-
- font = XLoadFont(display,"7x13");
- XSetFont(display,trans_gc,font);
-
- /* Ask user to enter in X translation */
-
- XDrawString(display,trans_window,trans_gc,
- 15,75,"Enter Translation in X: ",
- 24);
-
-
- /* set up initial values */
-
- key = 0;
- text[0] ='\0';
- i = 175;
-
- /* Display each key as pressed and concatenate it to
- a string until the RETURN key is pressed and
- then extract an integer value from the string */
-
- do
- {
- i = i + 8;
- XNextEvent(display,&trans_event);
- count = XLookupString(&trans_event,text,10,&key,0);
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,trans_window,trans_gc,
- i,75,text,1);
- strcat(string,text);
- } while (key != XK_Return && trans_event.type == KeyPress);
-
- translate.x = strtol(string,NULL,10);
-
- /* Prompt user for y translation value */
-
- XDrawString(display,trans_window,trans_gc,
- 15,105,"Enter Translation in Y: ",
- 24);
-
- /* Reset values */
-
- key = 0;
- text[0] = '\0';
- string[0] = '\0';
- XFlush(display);
- i = 175;
-
- /* Get input from the user and concatenate it into a string */
-
- do
- {
- i = i + 8;
- XNextEvent(display,&trans_event);
- count = XLookupString(&trans_event,text,10,&key,0);
- text[count] = '\0';
- if (key != XK_Return)
- XDrawString(display,trans_window,trans_gc,
- i,105,text,1);
- strcat(string,text);
- }
- while (key != XK_Return && trans_event.type == KeyPress);
-
- /* Multiply by negative 1 to get the value correct */
-
-
- /*
- translate.y = -1.0 * strtol(string,NULL,10);
- */
- translate.y = strtol(string,NULL,10);
-
- /* Tell user to press a key to exit the window */
-
- XDrawString(display,trans_window,trans_gc,
- 55,150,"Press any key to Exit",22);
- XFlush(display);
-
- /* Wait until a key is pressed */
-
- do
- XNextEvent(display,&trans_event);
- while( trans_event.xbutton.window != trans_window);
-
- /* change back to the old font */
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,trans_gc,font);
-
- /* destroy the window */
-
- XDestroyWindow(display,trans_window);
-
- /* do the translation */
-
- make_identity(display);
-
- matrix[2][0] = translate.x;
- matrix[2][1] = translate.y;
-
- combine_transformations(display);
-
-
- }
-
-
-
- /* 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;
-
- XClearWindow(display,parent);
-
- /* 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<1000; 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,320,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);
-
- /* Prompt user to input the x of a (x,y) set
- which is where the program will scale about */
-
- XDrawString(display,scale_window,scale_gc,
- 15,135,"Enter X to Scale About :",
- 24);
- key = 0;
- text[0] ='\0';
- string[0] = '\0';
-
-
- /* Get a x value */
-
- i = 185;
-
- 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,135,text,1);
- strcat(string,text);
- } while (key != XK_Return && scale_event.type == KeyPress);
-
-
- /* convert string to an integer */
-
- scale_x = strtol(string,NULL,10);
-
-
- /* Prompt user to enter Y value to scale about */
-
- XDrawString(display,scale_window,scale_gc,
- 15,165,"Enter Y to Scale About : ",
- 24);
-
- key = 0;
- text[0] ='\0';
- string[0] = '\0';
-
- /* Get a y value */
-
- i = 185;
-
- 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,165,text,1);
- strcat(string,text);
- } while (key != XK_Return && scale_event.type == KeyPress);
-
- /* convert the string inputted to an integer */
-
- scale_y = strtol(string,NULL,10);
-
-
- /* Prompt and wait for user to press a key */
-
- XDrawString(display,scale_window,scale_gc,
- 55,210,"Press any key to Exit",22);
-
- XFlush(display);
-
- do
- XNextEvent(display,&scale_event);
- while( scale_event.xbutton.window != scale_window);
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,scale_gc,font);
-
-
-
- /* trash the window */
-
- XDestroyWindow(display,scale_window);
-
- /* 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;
-
-
- /* 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,320,3,black,white);
-
- rotate_gc = parent_gc;
-
-
- XMapRaised(display,rotate_window);
- XClearWindow(display,rotate_window);
-
- XDrawString(display,rotate_window,parent_gc,
- 30,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,
- 15,75,"Enter Angle of Rotation: ",
- 23);
-
- key = 0;
- text[0] ='\0';
-
- /* input an angle, which is later converted to radians */
-
- i = 180;
-
- 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);
-
-
- /* Prompt and get input from the user to the X value to
- rotate about */
-
- XDrawString(display,rotate_window,rotate_gc,
- 15,105,"Enter X to Rotate About :",
- 24);
-
-
- /* reset values */
-
- key = 0;
- text[0] ='\0';
- string[0] = '\0';
- i = 180;
-
- 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,105,text,1);
- strcat(string,text);
- } while (key != XK_Return && rotate_event.type == KeyPress);
-
- /* convert the string to an integer */
-
- rotate_x = strtol(string,NULL,10);
-
- /* Ask for the user to enter a y value */
-
- XDrawString(display,rotate_window,rotate_gc,
- 15,135,"Enter Y to Rotate About : ",
- 24);
-
- key = 0;
- text[0] ='\0';
- string[0] = '\0';
- i = 180;
-
- 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,135,text,1);
- strcat(string,text);
- } while (key != XK_Return && rotate_event.type == KeyPress);
-
- /* conver the string to an integer */
-
- rotate_y = strtol(string,NULL,10);
-
- XDrawString(display,rotate_window,rotate_gc,
- 55,180,"Press any key to Exit",22);
-
- XFlush(display);
-
- do
- XNextEvent(display,&rotate_event);
- while( rotate_event.xbutton.window != rotate_window);
-
- font = XLoadFont(display,"8x16");
- XSetFont(display,rotate_gc,font);
-
- XDestroyWindow(display,rotate_window);
-
- /* 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+1,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+1,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]);
-
- }
-
-
- }
-