xref: /plan9/sys/src/libdraw/poly.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 
5 static
6 uchar*
7 addcoord(uchar *p, int oldx, int newx)
8 {
9 	int dx;
10 
11 	dx = newx-oldx;
12 	/* does dx fit in 7 signed bits? */
13 	if((unsigned)(dx - -0x40) <= 0x7F)
14 		*p++ = dx&0x7F;
15 	else{
16 		*p++ = 0x80 | (newx&0x7F);
17 		*p++ = newx>>7;
18 		*p++ = newx>>15;
19 	}
20 	return p;
21 }
22 
23 static
24 void
25 dopoly(int cmd, Image *dst, Point *pp, int np, int end0, int end1, int radius, Image *src, Point *sp)
26 {
27 	uchar *a, *t, *u;
28 	int i, ox, oy;
29 
30 	if(np == 0)
31 		return;
32 	t = malloc(np*2*3);
33 	if(t == nil)
34 		return;
35 	u = t;
36 	ox = oy = 0;
37 	for(i=0; i<np; i++){
38 		u = addcoord(u, ox, pp[i].x);
39 		ox = pp[i].x;
40 		u = addcoord(u, oy, pp[i].y);
41 		oy = pp[i].y;
42 	}
43 	a = bufimage(dst->display, 1+4+2+4+4+4+4+2*4+(u-t));
44 	if(a == 0){
45 		free(t);
46 		fprint(2, "image poly: %r\n");
47 		return;
48 	}
49 	a[0] = cmd;
50 	BPLONG(a+1, dst->id);
51 	BPSHORT(a+5, np-1);
52 	BPLONG(a+7, end0);
53 	BPLONG(a+11, end1);
54 	BPLONG(a+15, radius);
55 	BPLONG(a+19, src->id);
56 	BPLONG(a+23, sp->x);
57 	BPLONG(a+27, sp->y);
58 	memmove(a+31, t, u-t);
59 	free(t);
60 }
61 
62 void
63 poly(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp)
64 {
65 	dopoly('p', dst, p, np, end0, end1, radius, src, &sp);
66 }
67 
68 void
69 fillpoly(Image *dst, Point *p, int np, int wind, Image *src, Point sp)
70 {
71 	dopoly('P', dst, p, np, wind, 0, 0, src, &sp);
72 }
73