xref: /plan9/sys/src/cmd/unix/drawterm/libmemdraw/ellipse.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
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 			erect(-outx, -y, outx, -y, &p);
146 			continue;
147 		}
148 		if(t > 0) {
149 			inx = step(&in);
150 			if(y == inb)
151 				inx = 0;
152 		} else if(inx > outx)
153 			inx = outx;
154 		erect(inx, y, outx, y, &p);
155 		erect(inx, -y, outx, -y, &p);
156 		erect(-outx, y, -inx, y, &p);
157 		erect(-outx, -y, -inx, -y, &p);
158 		inx = outx + 1;
159 	}
160 }
161 
162 static Point p00 = {0, 0};
163 
164 /*
165  * a brushed ellipse
166  */
167 static
168 void
169 bellipse(int y, State *s, Param *p)
170 {
171 	int t, ox, oy, x, nx;
172 
173 	t = p->t;
174 	p->disc = allocmemimage(Rect(-t,-t,t+1,t+1), GREY1);
175 	if(p->disc == nil)
176 		return;
177 	memfillcolor(p->disc, DTransparent);
178 	memellipse(p->disc, p00, t, t, -1, memopaque, p00);
179 	oy = y;
180 	ox = 0;
181 	nx = x = step(s);
182 	do {
183 		while(nx==x && y-->0)
184 			nx = step(s);
185 		y++;
186 		eline(-x,-oy,-ox, -y, p);
187 		eline(ox,-oy,  x, -y, p);
188 		eline(-x,  y,-ox, oy, p);
189 		eline(ox,  y,  x, oy, p);
190 		ox = x+1;
191 		x = nx;
192 		y--;
193 		oy = y;
194 	} while(oy > 0);
195 }
196 
197 /*
198  * a rectangle with closed (not half-open) coordinates expressed
199  * relative to the center of the ellipse
200  */
201 static
202 void
203 erect(int x0, int y0, int x1, int y1, Param *p)
204 {
205 	Rectangle r;
206 
207 /*	print("R %d,%d %d,%d\n", x0, y0, x1, y1); */
208 	r = Rect(p->c.x+x0, p->c.y+y0, p->c.x+x1+1, p->c.y+y1+1);
209 	memdraw(p->dst, r, p->src, addpt(p->sp, r.min), memopaque, p00);
210 }
211 
212 /*
213  * a brushed point similarly specified
214  */
215 static
216 void
217 epoint(int x, int y, Param *p)
218 {
219 	Point p0;
220 	Rectangle r;
221 
222 /*	print("P%d %d,%d\n", p->t, x, y);	*/
223 	p0 = Pt(p->c.x+x, p->c.y+y);
224 	r = Rpt(addpt(p0, p->disc->r.min), addpt(p0, p->disc->r.max));
225 	memdraw(p->dst, r, p->src, addpt(p->sp, r.min), p->disc, p->disc->r.min);
226 }
227 
228 /*
229  * a brushed horizontal or vertical line similarly specified
230  */
231 static
232 void
233 eline(int x0, int y0, int x1, int y1, Param *p)
234 {
235 /*	print("L%d %d,%d %d,%d\n", p->t, x0, y0, x1, y1); */
236 	if(x1 > x0+1)
237 		erect(x0+1, y0-p->t, x1-1, y1+p->t, p);
238 	else if(y1 > y0+1)
239 		erect(x0-p->t, y0+1, x1+p->t, y1-1, p);
240 	epoint(x0, y0, p);
241 	if(x1-x0 || y1-y0)
242 		epoint(x1, y1, p);
243 }
244