poly.c 1.6 KB

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