transform.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 1993, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /*
  23. * transformed coordinate system objects for X
  24. */
  25. #include <X11/Xlib.h>
  26. #include "transform.h"
  27. #include <stdlib.h>
  28. static XPoint *
  29. TranslatePoints(TPoint *points, int n_points,
  30. Transform *t, int mode)
  31. {
  32. XPoint *xpoints;
  33. int i;
  34. double xoff = 0.0, yoff = 0.0;
  35. xpoints = (XPoint *) malloc ((unsigned)n_points * sizeof (*xpoints));
  36. if (!xpoints)
  37. return NULL;
  38. for (i = 0; i < n_points; i++) {
  39. xpoints[i].x = Xx(points[i].x + xoff, points[i].y + yoff, t);
  40. xpoints[i].y = Xy(points[i].x + xoff, points[i].y + yoff, t);
  41. if (mode == CoordModePrevious) {
  42. xoff += points[i].x;
  43. yoff += points[i].y;
  44. }
  45. }
  46. return xpoints;
  47. }
  48. void
  49. TFillPolygon (register Display *dpy, Drawable d, GC gc, Transform *t,
  50. TPoint *points, int n_points, int shape, int mode)
  51. {
  52. XPoint *xpoints;
  53. xpoints = TranslatePoints (points, n_points, t, mode);
  54. if (xpoints) {
  55. XFillPolygon (dpy, d, gc, xpoints, n_points, shape,
  56. CoordModeOrigin);
  57. free (xpoints);
  58. }
  59. }
  60. void
  61. TDrawArc (register Display *dpy, Drawable d, GC gc, Transform *t,
  62. double x, double y, double width, double height,
  63. int angle1, int angle2)
  64. {
  65. int xx, xy, xw, xh;
  66. xx = Xx(x,y,t);
  67. xy = Xy(x,y,t);
  68. xw = Xwidth (width, height, t);
  69. xh = Xheight (width, height, t);
  70. if (xw < 0) {
  71. xx += xw;
  72. xw = -xw;
  73. }
  74. if (xh < 0) {
  75. xy += xh;
  76. xh = -xh;
  77. }
  78. XDrawArc (dpy, d, gc, xx, xy, xw, xh, angle1, angle2);
  79. }
  80. void
  81. TFillArc (register Display *dpy, Drawable d, GC gc, Transform *t,
  82. double x, double y, double width, double height,
  83. int angle1, int angle2)
  84. {
  85. int xx, xy, xw, xh;
  86. xx = Xx(x,y,t);
  87. xy = Xy(x,y,t);
  88. xw = Xwidth (width, height, t);
  89. xh = Xheight (width, height, t);
  90. if (xw < 0) {
  91. xx += xw;
  92. xw = -xw;
  93. }
  94. if (xh < 0) {
  95. xy += xh;
  96. xh = -xh;
  97. }
  98. XFillArc (dpy, d, gc, xx, xy, xw, xh, angle1, angle2);
  99. }
  100. void
  101. SetTransform (Transform *t, int xx1, int xx2, int xy1, int xy2,
  102. double tx1, double tx2, double ty1, double ty2)
  103. {
  104. t->mx = ((double) xx2 - xx1) / (tx2 - tx1);
  105. t->bx = ((double) xx1) - t->mx * tx1;
  106. t->my = ((double) xy2 - xy1) / (ty2 - ty1);
  107. t->by = ((double) xy1) - t->my * ty1;
  108. }