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