home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #2 / amigaacscoverdisc1998-021998.iso / utilities / shareware / dev / libx11 / graphics_examples / class1.c next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  7.5 KB  |  395 lines

  1. /*
  2.  
  3.   Program One 
  4.   Bert Nelson
  5.   Copyright 1992 Bert Nelson
  6.  
  7.   Purpose:  Draw a triangle
  8.             Draw a Rectangle
  9.             Draw a Octagon
  10.         Draw a Circle
  11.             Draw a Pie Chart with a different sizes of slices 
  12.         Draw a series of arcs
  13.  
  14.  
  15.   Compile Instructions:  cc file.c -lX11 -lm -o file
  16.  
  17.   Run Instructions:  type 'file' after compiling and click a button
  18.                      on the mouse to draw (or redraw) the figures
  19.  
  20.  
  21.    Permission to use, copy, modify, and distribute this software and its
  22.    documentation for any purpose and without fee is hereby granted,
  23.    provided that the above copyright notice appear in all copies and that
  24.    both that copyright notice and this permission notice appear in
  25.    supporting documentation.  It is provided "as is" without any express or
  26.    implied warranty.
  27.  
  28.    Bert Nelson DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29.    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30.    Bert Nelson BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31.    ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32.    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33.    ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34.    SOFTWARE.
  35.  
  36.  
  37.  
  38.   */
  39.  
  40.  
  41. #include <X11/Xlib.h>
  42. #include <X11/Xutil.h>
  43. #include <math.h>
  44.  
  45. #define PI 3.141592654    /* set up PI defintion */
  46.  
  47.  
  48. /* set strings as global */
  49.  
  50. char hello[] = "Program One - by Bert Nelson";
  51. char tri_str[] = "Triangle";
  52. char rect_str[] = "Rectangle";
  53. char oct_str[] = "Octagon";
  54. char circ_str[] = "Circle";
  55. char sect_str[] = "Sectors";
  56. char arcs_str[] = "Arcs";
  57. Font font;
  58.  
  59. main(argc,argv)
  60. int argc;
  61. char **argv;
  62. {
  63.     Display *mydisplay;
  64.     Window  mywindow;
  65.     GC mygc;
  66.     XEvent myevent;
  67.     KeySym mykey;
  68.     XSizeHints myhint;
  69.  
  70.     int myscreen;
  71.     unsigned long myforeground, mybackground;
  72.     int i;
  73.     char text[10];
  74.     int done;
  75.  
  76.     /* set up display and foreground/background */
  77.  
  78.     mydisplay = XOpenDisplay("");
  79.  
  80.     myscreen = DefaultScreen (mydisplay);
  81.     mybackground = WhitePixel (mydisplay, myscreen);
  82.     myforeground = BlackPixel (mydisplay, myscreen);
  83.  
  84.     /* create a window with a width of 630 x 425 */
  85.  
  86.     myhint.x = 0;
  87.     myhint.y = 0;
  88.     myhint.width = 630;
  89.     myhint.height = 425;
  90.     myhint.flags = PPosition | PSize;
  91.     mywindow = XCreateSimpleWindow (mydisplay,
  92.         DefaultRootWindow (mydisplay),
  93.         myhint.x, myhint.y, myhint.width, myhint.height,
  94.         5, myforeground, mybackground);
  95.  
  96.     XSetStandardProperties (mydisplay, mywindow, hello, hello,
  97.         None, argv, argc, &myhint);
  98.  
  99.     mygc = XCreateGC (mydisplay, mywindow, 0, 0);
  100.  
  101.     /* Load a font called 8x16 */
  102.  
  103.     font = XLoadFont(mydisplay,"8x16");
  104.     XSetFont(mydisplay,mygc,font);
  105.  
  106.  
  107.     XSetBackground (mydisplay, mygc, mybackground);
  108.     XSetForeground (mydisplay, mygc, myforeground);
  109.  
  110.     XSelectInput (mydisplay, mywindow,
  111.         ButtonPressMask | KeyPressMask | ExposureMask);
  112.  
  113.     /* map window to the screen */
  114.  
  115.     XMapRaised (mydisplay, mywindow);
  116.  
  117.  
  118.     /* Loop through until a q is press when the cursor is in
  119.            the window, which will cause the application to quit */
  120.  
  121.     done = 0;
  122.     while (done == 0) {
  123.         XNextEvent (mydisplay, &myevent );
  124.  
  125.  
  126.         switch (myevent.type) {
  127.  
  128.         case Expose:
  129.             if (myevent.xexpose.count == 0)
  130.  
  131.                 break;
  132.  
  133.  
  134.         case MappingNotify:
  135.             XRefreshKeyboardMapping (&myevent);
  136.             break;
  137.  
  138.  
  139.     /* When there is a mouse click fire up the graphics subroutines */
  140.  
  141.         case ButtonPress:
  142.  
  143.             triangle(mydisplay,mywindow,mygc);
  144.             rect(mydisplay,mywindow,mygc);
  145.             oct(mydisplay,mywindow,mygc);
  146.             circ(mydisplay,mywindow,mygc);
  147.             sectors(mydisplay,mywindow,mygc);
  148.             arcs(mydisplay,mywindow,mygc);
  149.             break;
  150.  
  151.             /* If a 'q' was pressed while the cursor is within
  152.            the window set done to 1, which causes the loop
  153.            to be broken and the program finishes */
  154.  
  155.  
  156.         case KeyPress:
  157.             i = XLookupString (&myevent, text, 10, &mykey, 0);
  158.             if (i == 1 && text[0] == 'q')
  159.                 done = 1;
  160.             break;
  161.  
  162.  
  163.         } /* switch */
  164.  
  165.  
  166.     } /* while (done == 0) */
  167.  
  168.  
  169.     /* Free up and clean up the window created */
  170.  
  171.  
  172.     XFreeGC (mydisplay, mygc);
  173.     XDestroyWindow (mydisplay, mywindow);
  174.     XCloseDisplay (mydisplay);
  175.  
  176.     exit(0);
  177.  
  178. } /* main */
  179.  
  180.  
  181. /* Draw a Triangle */
  182.  
  183. triangle(the_display,the_window,the_gc)
  184. Display *the_display;
  185. Drawable the_window;
  186. GC the_gc;
  187.  
  188. {
  189.  
  190.     XPoint points[4] ;
  191.  
  192.  
  193.     points[0].x = 100;
  194.     points[0].y = 100;
  195.     points[1].x = 50;
  196.     points[1].y = 200;
  197.     points[2].x = 150;
  198.     points[2].y = 200;
  199.     points[3].x = 100;
  200.     points[3].y = 100;
  201.  
  202.     XDrawLines(the_display,the_window,the_gc,points,4,CoordModeOrigin);
  203.  
  204.     XDrawImageString(the_display,the_window,the_gc,75,85,
  205.         tri_str,strlen(tri_str));
  206.  
  207. }
  208.  
  209.  
  210. /* Draw a rectangle */
  211.  
  212.  
  213. rect(the_display,the_window,the_gc)
  214. Display *the_display;
  215. Drawable the_window;
  216. GC the_gc;
  217. {
  218.  
  219.     XDrawRectangle(the_display,the_window,the_gc,225,100,150,100);
  220.  
  221.     XDrawImageString(the_display,the_window,the_gc,265,85,                
  222.         rect_str,strlen(rect_str));
  223. }
  224.  
  225.  
  226.  
  227.  
  228. /* Draw an octagon */
  229.  
  230.  
  231. oct(the_display,the_window,the_gc)
  232.  
  233.  
  234. Display *the_display;
  235. Drawable the_window;
  236. GC the_gc;
  237.  
  238. {
  239.  
  240.     XPoint points[9];
  241.  
  242.     points[0].x = 516;
  243.     points[0].y = 100;
  244.     points[1].x = 550;
  245.     points[1].y = 133;
  246.     points[2].x = 550;
  247.     points[2].y = 166;
  248.     points[3].x = 516;
  249.     points[3].y = 200;
  250.     points[4].x = 483;
  251.     points[4].y = 200;
  252.     points[5].x = 450;
  253.     points[5].y = 166;
  254.     points[6].x = 450;
  255.     points[6].y = 133;
  256.     points[7].x = 483;
  257.     points[7].y = 100;
  258.     points[8].x = 516;
  259.     points[8].y = 100;
  260.  
  261.  
  262.     XDrawLines(the_display,the_window,the_gc,points,9,CoordModeOrigin);
  263.  
  264.     XDrawImageString(the_display,the_window,the_gc,470,85,
  265.         oct_str,strlen(oct_str));
  266.  
  267.  
  268. }
  269.  
  270.  
  271.  
  272. /* Draw a circle */
  273.  
  274. circ(the_display,the_window,the_gc)
  275.  
  276. Display *the_display;
  277. Drawable the_window;
  278. GC the_gc;
  279.  
  280. {
  281.  
  282.     XDrawArc(the_display,the_window,the_gc,50,300,100,100,0,360*64);
  283.  
  284.     XDrawImageString(the_display,the_window,the_gc,75,285,
  285.         circ_str,strlen(circ_str));
  286.  
  287.  
  288. }
  289.  
  290. /* Draw sectors according to the degrees wanted */
  291.  
  292. sectors(the_display,the_window,the_gc)
  293.  
  294. Display *the_display;
  295. Drawable the_window;
  296. GC the_gc;
  297.  
  298. {
  299.  
  300.     int i;
  301.     double x;
  302.     double y;
  303.     int degrees[13];
  304.     int degrees2[13];
  305.     XPoint points[2];
  306.  
  307.     degrees[0] = 0;
  308.     degrees[1] = 20;
  309.     degrees[2] = 25;
  310.     degrees[3] = 85;
  311.     degrees[4] = 90;
  312.     degrees[5] = 175;
  313.     degrees[6] = 180;
  314.     degrees[7] = 215;
  315.     degrees[8] = 220;
  316.     degrees[9] = 295;
  317.     degrees[10] = 300;
  318.     degrees[11] = 355;
  319.     degrees[12] = 0;
  320.  
  321.     points[0].x = 300;
  322.     points[0].y = 350;
  323.  
  324.  
  325.  
  326.     for( i=0; i<12; i++)
  327.  
  328.     {
  329.  
  330.     /* convert to radians and find the cos/sin and multiply by 50, which
  331.    will give us the absolute points from the origin */
  332.  
  333.  
  334.         x = cos(degrees[i] * (PI/180)) * 50;
  335.         y = sin(degrees[i] * (PI/180)) * 50;
  336.  
  337.  
  338.         /* get the sign changed */
  339.  
  340.         y = -1.0 * y;
  341.  
  342.  
  343.         points[1].x = x + 300;
  344.         points[1].y = y + 350;
  345.  
  346. /* Draw the arc when you only need to, which is every other degree[i] */
  347.  
  348.  
  349.         if (i == 0 || (i%2) == 0)
  350.         {
  351.             XDrawArc(the_display,the_window,the_gc,
  352.                 250,300,100,100,degrees[i] * 64,
  353.                 (degrees[i+1] - degrees[i])*64);
  354.         }
  355.  
  356.     /* Draw the lines from the center to the end points of the arc */
  357.  
  358.  
  359.         XDrawLines(the_display,the_window,the_gc,
  360.             points,2,CoordModeOrigin);
  361.  
  362.     } /* end for loop */
  363.  
  364.  
  365.     XDrawImageString(the_display,the_window,the_gc,275,285,sect_str,
  366.         strlen(sect_str));
  367.  
  368.  
  369. }
  370.  
  371.  
  372. /* Draw four arcs spanning 180 degrees */
  373.  
  374.  
  375. arcs(the_display,the_window,the_gc)
  376.  
  377. Display *the_display;
  378. Drawable the_window;
  379. GC the_gc;
  380.  
  381. {
  382.  
  383.     XDrawArc(the_display,the_window,the_gc,460,350,100,100,0,180*64);
  384.  
  385.     XDrawArc(the_display,the_window,the_gc,470,360,80,80,0,180*64);
  386.  
  387.     XDrawArc(the_display,the_window,the_gc,480,370,60,60,0,180*64);
  388.  
  389.     XDrawArc(the_display,the_window,the_gc,490,380,40,40,0,180*64);
  390.  
  391.     XDrawImageString(the_display,the_window,the_gc,
  392.         490,340,arcs_str,strlen(arcs_str)+1);
  393.  
  394. }
  395.