ellipse.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <memdraw.h>
  5. #include <memlayer.h>
  6. /*
  7. * ellipse(dst, c, a, b, t, src, sp)
  8. * draws an ellipse centered at c with semiaxes a,b>=0
  9. * and semithickness t>=0, or filled if t<0. point sp
  10. * in src maps to c in dst
  11. *
  12. * very thick skinny ellipses are brushed with circles (slow)
  13. * others are approximated by filling between 2 ellipses
  14. * criterion for very thick when b<a: t/b > 0.5*x/(1-x)
  15. * where x = b/a
  16. */
  17. typedef struct Param Param;
  18. typedef struct State State;
  19. static void bellipse(int, State*, Param*);
  20. static void erect(int, int, int, int, Param*);
  21. static void eline(int, int, int, int, Param*);
  22. struct Param {
  23. Memimage *dst;
  24. Memimage *src;
  25. Point c;
  26. int t;
  27. Point sp;
  28. Memimage *disc;
  29. int op;
  30. };
  31. /*
  32. * denote residual error by e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2
  33. * e(x,y) = 0 on ellipse, e(x,y) < 0 inside, e(x,y) > 0 outside
  34. */
  35. struct State {
  36. int a;
  37. int x;
  38. vlong a2; /* a^2 */
  39. vlong b2; /* b^2 */
  40. vlong b2x; /* b^2 * x */
  41. vlong a2y; /* a^2 * y */
  42. vlong c1;
  43. vlong c2; /* test criteria */
  44. vlong ee; /* ee = e(x+1/2,y-1/2) - (a^2+b^2)/4 */
  45. vlong dxe;
  46. vlong dye;
  47. vlong d2xe;
  48. vlong d2ye;
  49. };
  50. static
  51. State*
  52. newstate(State *s, int a, int b)
  53. {
  54. s->x = 0;
  55. s->a = a;
  56. s->a2 = (vlong)(a*a);
  57. s->b2 = (vlong)(b*b);
  58. s->b2x = (vlong)0;
  59. s->a2y = s->a2*(vlong)b;
  60. s->c1 = -((s->a2>>2) + (vlong)(a&1) + s->b2);
  61. s->c2 = -((s->b2>>2) + (vlong)(b&1));
  62. s->ee = -s->a2y;
  63. s->dxe = (vlong)0;
  64. s->dye = s->ee<<1;
  65. s->d2xe = s->b2<<1;
  66. s->d2ye = s->a2<<1;
  67. return s;
  68. }
  69. /*
  70. * return x coord of rightmost pixel on next scan line
  71. */
  72. static
  73. int
  74. step(State *s)
  75. {
  76. while(s->x < s->a) {
  77. if(s->ee+s->b2x <= s->c1 || /* e(x+1,y-1/2) <= 0 */
  78. s->ee+s->a2y <= s->c2) { /* e(x+1/2,y) <= 0 (rare) */
  79. s->dxe += s->d2xe;
  80. s->ee += s->dxe;
  81. s->b2x += s->b2;
  82. s->x++;
  83. continue;
  84. }
  85. s->dye += s->d2ye;
  86. s->ee += s->dye;
  87. s->a2y -= s->a2;
  88. if(s->ee-s->a2y <= s->c2) { /* e(x+1/2,y-1) <= 0 */
  89. s->dxe += s->d2xe;
  90. s->ee += s->dxe;
  91. s->b2x += s->b2;
  92. return s->x++;
  93. }
  94. break;
  95. }
  96. return s->x;
  97. }
  98. void
  99. memellipse(Memimage *dst, Point c, int a, int b, int t, Memimage *src, Point sp, int op)
  100. {
  101. State in, out;
  102. int y, inb, inx, outx, u;
  103. Param p;
  104. if(a < 0)
  105. a = -a;
  106. if(b < 0)
  107. b = -b;
  108. p.dst = dst;
  109. p.src = src;
  110. p.c = c;
  111. p.t = t;
  112. p.sp = subpt(sp, c);
  113. p.disc = nil;
  114. p.op = op;
  115. u = (t<<1)*(a-b);
  116. if(b<a && u>b*b || a<b && -u>a*a) {
  117. /* if(b<a&&(t<<1)>b*b/a || a<b&&(t<<1)>a*a/b) # very thick */
  118. bellipse(b, newstate(&in, a, b), &p);
  119. return;
  120. }
  121. if(t < 0) {
  122. inb = -1;
  123. newstate(&out, a, y = b);
  124. } else {
  125. inb = b - t;
  126. newstate(&out, a+t, y = b+t);
  127. }
  128. if(t > 0)
  129. newstate(&in, a-t, inb);
  130. inx = 0;
  131. for( ; y>=0; y--) {
  132. outx = step(&out);
  133. if(y > inb) {
  134. erect(-outx, y, outx, y, &p);
  135. if(y != 0)
  136. erect(-outx, -y, outx, -y, &p);
  137. continue;
  138. }
  139. if(t > 0) {
  140. inx = step(&in);
  141. if(y == inb)
  142. inx = 0;
  143. } else if(inx > outx)
  144. inx = outx;
  145. erect(inx, y, outx, y, &p);
  146. if(y != 0)
  147. erect(inx, -y, outx, -y, &p);
  148. erect(-outx, y, -inx, y, &p);
  149. if(y != 0)
  150. erect(-outx, -y, -inx, -y, &p);
  151. inx = outx + 1;
  152. }
  153. }
  154. static Point p00 = {0, 0};
  155. /*
  156. * a brushed ellipse
  157. */
  158. static
  159. void
  160. bellipse(int y, State *s, Param *p)
  161. {
  162. int t, ox, oy, x, nx;
  163. t = p->t;
  164. p->disc = allocmemimage(Rect(-t,-t,t+1,t+1), GREY1);
  165. if(p->disc == nil)
  166. return;
  167. memfillcolor(p->disc, DTransparent);
  168. memellipse(p->disc, p00, t, t, -1, memopaque, p00, p->op);
  169. oy = y;
  170. ox = 0;
  171. nx = x = step(s);
  172. do {
  173. while(nx==x && y-->0)
  174. nx = step(s);
  175. y++;
  176. eline(-x,-oy,-ox, -y, p);
  177. eline(ox,-oy, x, -y, p);
  178. eline(-x, y,-ox, oy, p);
  179. eline(ox, y, x, oy, p);
  180. ox = x+1;
  181. x = nx;
  182. y--;
  183. oy = y;
  184. } while(oy > 0);
  185. }
  186. /*
  187. * a rectangle with closed (not half-open) coordinates expressed
  188. * relative to the center of the ellipse
  189. */
  190. static
  191. void
  192. erect(int x0, int y0, int x1, int y1, Param *p)
  193. {
  194. Rectangle r;
  195. /* print("R %d,%d %d,%d\n", x0, y0, x1, y1); /**/
  196. r = Rect(p->c.x+x0, p->c.y+y0, p->c.x+x1+1, p->c.y+y1+1);
  197. memdraw(p->dst, r, p->src, addpt(p->sp, r.min), memopaque, p00, p->op);
  198. }
  199. /*
  200. * a brushed point similarly specified
  201. */
  202. static
  203. void
  204. epoint(int x, int y, Param *p)
  205. {
  206. Point p0;
  207. Rectangle r;
  208. /* print("P%d %d,%d\n", p->t, x, y); /**/
  209. p0 = Pt(p->c.x+x, p->c.y+y);
  210. r = Rpt(addpt(p0, p->disc->r.min), addpt(p0, p->disc->r.max));
  211. memdraw(p->dst, r, p->src, addpt(p->sp, r.min), p->disc, p->disc->r.min, p->op);
  212. }
  213. /*
  214. * a brushed horizontal or vertical line similarly specified
  215. */
  216. static
  217. void
  218. eline(int x0, int y0, int x1, int y1, Param *p)
  219. {
  220. /* print("L%d %d,%d %d,%d\n", p->t, x0, y0, x1, y1); /**/
  221. if(x1 > x0+1)
  222. erect(x0+1, y0-p->t, x1-1, y1+p->t, p);
  223. else if(y1 > y0+1)
  224. erect(x0-p->t, y0+1, x1+p->t, y1-1, p);
  225. epoint(x0, y0, p);
  226. if(x1-x0 || y1-y0)
  227. epoint(x1, y1, p);
  228. }