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
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)
18 typedef struct Param Param;
19 typedef struct State State;
21 static void bellipse(int, State*, Param*);
22 static void erect(int, int, int, int, Param*);
23 static void eline(int, int, int, int, Param*);
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
45 vlong b2x; /* b^2 * x */
46 vlong a2y; /* a^2 * y */
48 vlong c2; /* test criteria */
49 vlong ee; /* ee = e(x+1/2,y-1/2) - (a^2+b^2)/4 */
58 newstate(State *s, int a, int b)
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));
77 * return x coord of rightmost pixel on next scan line
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) */
95 if(s->ee-s->a2y <= s->c2) { /* e(x+1/2,y-1) <= 0 */
107 memellipse(Memimage *dst, Point c, int a, int b, int t, Memimage *src, Point sp, int op)
110 int y, inb, inx, outx, u;
126 if(b<a && u>b*b || a<b && -u>a*a) {
127 /* if(b<a&&(t<<1)>b*b/a || a<b&&(t<<1)>a*a/b) # very thick */
128 bellipse(b, newstate(&in, a, b), &p);
134 newstate(&out, a, y = b);
137 newstate(&out, a+t, y = b+t);
140 newstate(&in, a-t, inb);
145 erect(-outx, y, outx, y, &p);
147 erect(-outx, -y, outx, -y, &p);
154 } else if(inx > outx)
156 erect(inx, y, outx, y, &p);
158 erect(inx, -y, outx, -y, &p);
159 erect(-outx, y, -inx, y, &p);
161 erect(-outx, -y, -inx, -y, &p);
166 static Point p00 = {0, 0};
173 bellipse(int y, State *s, Param *p)
175 int t, ox, oy, x, nx;
178 p->disc = allocmemimage(Rect(-t,-t,t+1,t+1), GREY1);
181 memfillcolor(p->disc, DTransparent);
182 memellipse(p->disc, p00, t, t, -1, memopaque, p00, p->op);
187 while(nx==x && y-->0)
190 eline(-x,-oy,-ox, -y, p);
191 eline(ox,-oy, x, -y, p);
192 eline(-x, y,-ox, oy, p);
193 eline(ox, y, x, oy, p);
202 * a rectangle with closed (not half-open) coordinates expressed
203 * relative to the center of the ellipse
207 erect(int x0, int y0, int x1, int y1, Param *p)
211 /* print("R %d,%d %d,%d\n", x0, y0, x1, y1); */
212 r = Rect(p->c.x+x0, p->c.y+y0, p->c.x+x1+1, p->c.y+y1+1);
213 memdraw(p->dst, r, p->src, addpt(p->sp, r.min), memopaque, p00, p->op);
217 * a brushed point similarly specified
221 epoint(int x, int y, Param *p)
226 /* print("P%d %d,%d\n", p->t, x, y); */
227 p0 = Pt(p->c.x+x, p->c.y+y);
228 r = Rpt(addpt(p0, p->disc->r.min), addpt(p0, p->disc->r.max));
229 memdraw(p->dst, r, p->src, addpt(p->sp, r.min), p->disc, p->disc->r.min, p->op);
233 * a brushed horizontal or vertical line similarly specified
237 eline(int x0, int y0, int x1, int y1, Param *p)
239 /* print("L%d %d,%d %d,%d\n", p->t, x0, y0, x1, y1); */
241 erect(x0+1, y0-p->t, x1-1, y1+p->t, p);
243 erect(x0-p->t, y0+1, x1+p->t, y1-1, p);