home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / x / x11p-13.zip / do_triangles.c < prev    next >
C/C++ Source or Header  |  1991-05-02  |  6KB  |  241 lines

  1. /*****************************************************************************
  2. Copyright 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of Digital not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21.  
  22. ******************************************************************************/
  23.  
  24.  
  25. #undef POLYTRIANGLE_HACK    /* don't use this code */
  26. #ifdef POLYTRIANGLE_HACK
  27. #include <X11/Xlibint.h>
  28. #endif
  29.  
  30. #include "x11perf.h"
  31. #include "bitmaps.h"
  32. #include <stdio.h>
  33. #include <math.h>
  34.  
  35. #define NUM_POINTS 3   /* 3 points to a triangle */
  36. static XPoint *points;
  37. static GC     pgc;
  38.  
  39. extern double sin();
  40. extern double cos();
  41. extern double sqrt();
  42. #define PI  3.14159265357989
  43.  
  44.  
  45. double Area(p1, p2, p3)
  46.     XPoint  p1, p2, p3;
  47. {
  48.     return
  49.       (p1.x*p2.y - p1.x*p3.y + p2.x*p3.y - p2.x*p1.y + p3.x*p1.y - p3.x*p2.y)/2;
  50. }
  51.  
  52. double Distance(p1, p2)
  53.     XPoint p1, p2;
  54. {
  55.     return sqrt((float) ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)));
  56. }
  57.     
  58. int InitTriangles(xp, p, reps)
  59.     XParms  xp;
  60.     Parms   p;
  61.     int     reps;
  62. {
  63.     int     i, j, numPoints;
  64.     int     rows;
  65.     int     x, y;
  66.     int     size, iradius;
  67.     double  phi, phiinc, radius, delta, phi2, area, aarea;
  68.     XPoint  *curPoint;
  69.  
  70.     pgc = xp->fggc;
  71.  
  72.     size = p->special;
  73.     phi = 0.0;
  74.     delta = 2.0 * PI / ((double) NUM_POINTS);
  75.     if (xp->version == VERSION1_2) {
  76.     radius = ((double) size) * sqrt(3.0)/2.0;
  77.     phiinc = delta/10.0;
  78.     } else {
  79.     /* Version 1.2's radius computation was completely bogus, and resulted
  80.        in triangles with sides about 50% longer than advertised.  However,
  81.        this inadvertently resulted in triangles with areas just a little bit
  82.        smaller than the triangle that covers size^2 pixels, which would
  83.        make the area directly comparable to 10x10 rectangles and 10x10
  84.        trapezoids.  So here's the new computation so -triangleN has the same
  85.        area as -rectN.
  86.      */
  87.     radius = ((double) size) * sqrt(sqrt(16.0/27.0));
  88.     phiinc = 1.75*PI / ((double) p->objects);
  89.     }
  90.     iradius = (int) (radius + 0.5);
  91.  
  92.     numPoints = (p->objects) * NUM_POINTS;  
  93.     points = (XPoint *)malloc(numPoints * sizeof(XPoint));
  94.     curPoint = points;
  95.     x = iradius;
  96.     y = iradius;
  97.     rows = 0;
  98.     aarea = 0.0;
  99.  
  100.     for (i = 0; i != p->objects; i++) {
  101.     for (j = 0; j != NUM_POINTS; j++) {
  102.         phi2 = phi + ((double) j) * delta;
  103.         curPoint->x = (int) ((double)x + (radius * cos(phi2)) + 0.5);
  104.         curPoint->y = (int) ((double)y + (radius * sin(phi2)) + 0.5);
  105.         curPoint++;
  106.     }
  107.     area = Area(curPoint[-1], curPoint[-2], curPoint[-3]);
  108.     aarea += area;
  109. /*    printf("%6.1lf %6.1lf %6.1lf   %6.1lf\n",
  110.         Distance(curPoint[-1], curPoint[-2]),
  111.         Distance(curPoint[-1], curPoint[-3]),
  112.         Distance(curPoint[-2], curPoint[-3]),
  113.         area);
  114. */    
  115.     phi += phiinc;
  116.     y += 2 * iradius;
  117.     rows++;
  118.     if (y + iradius > HEIGHT || rows == MAXROWS) {
  119.         rows = 0;
  120.         y = iradius;
  121.         x += 2 * iradius;
  122.         if (x + iradius > WIDTH) {
  123.         x = iradius;
  124.         }
  125.     }
  126.     }
  127. /*    printf("Average area = %6.2lf\n", aarea/p->objects); */
  128.  
  129.     SetFillStyle(xp, p);
  130.  
  131.     return reps;
  132. }
  133.  
  134. #ifndef POLYTRIANGLE_HACK
  135.  
  136. void DoTriangles(xp, p, reps)
  137.     XParms  xp;
  138.     Parms   p;
  139.     int     reps;
  140. {
  141.     int     i, j;
  142.     XPoint  *curPoint;
  143.  
  144.     for (i = 0; i != reps; i++) {
  145.         curPoint = points;
  146.         for (j = 0; j != p->objects; j++) {
  147.             XFillPolygon(xp->d, xp->w, pgc, curPoint, NUM_POINTS, Convex, 
  148.              CoordModeOrigin);
  149.             curPoint += NUM_POINTS;
  150.     }
  151.         if (pgc == xp->bggc)
  152.             pgc = xp->fggc;
  153.         else
  154.             pgc = xp->bggc;
  155.     }
  156. }
  157.  
  158. #else
  159. void DoTriangles(xp, p, reps)
  160.     XParms  xp;
  161.     Parms   p;
  162.     int     reps;
  163. {
  164.     int     i, j;
  165.     XPoint  *curPoint;
  166.  
  167.     for (i = 0; i != reps; i++) {
  168.         XPolyTriangle (xp->d, xp->w, pgc, points, p->objects, Convex, 
  169.              CoordModeOrigin);
  170.         if (pgc == xp->bggc)
  171.             pgc = xp->fggc;
  172.         else
  173.             pgc = xp->bggc;
  174.     }
  175. }
  176.  
  177. static xReq _dummy_request = {
  178.     0, 0, 0
  179. };
  180.  
  181. XPolyTriangle(dpy, d, gc, points, n_triangles, shape, mode)
  182. register Display *dpy;
  183. Drawable d;
  184. GC gc;
  185. XPoint *points;
  186. int n_triangles;
  187. int shape;
  188. int mode;
  189. {
  190.     register xFillPolyReq *req;
  191.     register long nbytes;
  192.     int        max_triangles;
  193.     int        n_this_time;
  194.     int        *buf, *pts;
  195.     int        gcid;
  196.     int        last;
  197.  
  198.     max_triangles = (dpy->bufmax - dpy->buffer) / 28;
  199.     LockDisplay(dpy);
  200.     FlushGC(dpy, gc);
  201.     dpy->request += n_triangles;
  202.     pts = (int *) points;
  203.     gcid = gc->gid;
  204.     last = shape | (mode << 8);
  205.     while (n_triangles)
  206.     {
  207.     if ((n_this_time = max_triangles) > n_triangles)
  208.         n_this_time = n_triangles;
  209.     n_triangles -= n_this_time;
  210.     GetReqExtra(FillPoly, 
  211.         (SIZEOF(xFillPolyReq) + 12) * n_this_time - SIZEOF(xFillPolyReq), req);
  212.     --dpy->request;
  213.  
  214.     buf = req;
  215.         while (n_this_time--)
  216.     {
  217.         buf[0] = X_FillPoly | (7 << 16);
  218.         buf[1] = d;
  219.         buf[2] = gcid;
  220.         buf[3] = last;
  221.         buf[4] = pts[0];
  222.         buf[5] = pts[1];
  223.         buf[6] = pts[2];
  224.         buf += 7;
  225.         pts += 3;
  226.     }
  227.     }
  228.     dpy->last_req = &_dummy_request;
  229.     UnlockDisplay(dpy);
  230.     SyncHandle();
  231. }
  232. #endif
  233.  
  234. void EndTriangles(xp, p)
  235.     XParms  xp;
  236.     Parms   p;
  237. {
  238.     free(points);
  239. }
  240.  
  241.