do_tris.c 5.8 KB

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