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