34 * Rather than line clip, we run the Bresenham loop over the full line,
35 * and clip on each pixel. This is more expensive but means that
36 * lines look the same regardless of how the windowing has tiled them.
37 * For speed, we check for clipping outside the loop and make the
38 * test easy when possible.
43 horline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
45 int x, y, dy, deltay, deltax, maxx;
46 int dd, easy, e, bpp, m, m0;
51 dd = dst->width*sizeof(ulong);
58 maxx = lmin(p1.x, clipr.max.x-1);
60 m0 = 0xFF^(0xFF>>bpp);
61 m = m0 >> (p0.x&(7/dst->depth))*bpp;
62 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
63 e = 2*deltay - deltax;
65 d = byteaddr(dst, p0);
67 deltax = deltay - 2*deltax;
68 for(x=p0.x; x<=maxx; x++){
69 if(easy || (clipr.min.x<=x && clipr.min.y<=y && y<clipr.max.y))
70 *d ^= (*d^srcval) & m;
86 verline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
88 int x, y, deltay, deltax, maxy;
89 int easy, e, bpp, m, m0, dd;
99 maxy = lmin(p1.y, clipr.max.y-1);
101 m0 = 0xFF^(0xFF>>bpp);
102 m = m0 >> (p0.x&(7/dst->depth))*bpp;
103 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
104 e = 2*deltax - deltay;
106 d = byteaddr(dst, p0);
108 deltay = deltax - 2*deltay;
109 for(y=p0.y; y<=maxy; y++){
110 if(easy || (clipr.min.y<=y && clipr.min.x<=x && x<clipr.max.x))
111 *d ^= (*d^srcval) & m;
118 d += dst->width*sizeof(ulong);
127 horliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
129 int x, y, sx, sy, deltay, deltax, minx, maxx;
133 deltax = p1.x - p0.x;
134 deltay = p1.y - p0.y;
135 sx = drawreplxy(src->r.min.x, src->r.max.x, p0.x+dsrc.x);
136 minx = lmax(p0.x, clipr.min.x);
137 maxx = lmin(p1.x, clipr.max.x-1);
139 m0 = 0xFF^(0xFF>>bpp);
140 m = m0 >> (minx&(7/dst->depth))*bpp;
141 for(x=minx; x<=maxx; x++){
142 y = p0.y + (deltay*(x-p0.x)+deltax/2)/deltax;
143 if(clipr.min.y<=y && y<clipr.max.y){
144 d = byteaddr(dst, Pt(x, y));
145 sy = drawreplxy(src->r.min.y, src->r.max.y, y+dsrc.y);
146 s = byteaddr(src, Pt(sx, sy));
149 if(++sx >= src->r.max.x)
159 verliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
161 int x, y, sx, sy, deltay, deltax, miny, maxy;
165 deltax = p1.x - p0.x;
166 deltay = p1.y - p0.y;
167 sy = drawreplxy(src->r.min.y, src->r.max.y, p0.y+dsrc.y);
168 miny = lmax(p0.y, clipr.min.y);
169 maxy = lmin(p1.y, clipr.max.y-1);
171 m0 = 0xFF^(0xFF>>bpp);
172 for(y=miny; y<=maxy; y++){
173 if(deltay == 0) /* degenerate line */
176 x = p0.x + (deltax*(y-p0.y)+deltay/2)/deltay;
177 if(clipr.min.x<=x && x<clipr.max.x){
178 m = m0 >> (x&(7/dst->depth))*bpp;
179 d = byteaddr(dst, Pt(x, y));
180 sx = drawreplxy(src->r.min.x, src->r.max.x, x+dsrc.x);
181 s = byteaddr(src, Pt(sx, sy));
184 if(++sy >= src->r.max.y)
191 horline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
193 int x, y, deltay, deltax, minx, maxx;
197 deltax = p1.x - p0.x;
198 deltay = p1.y - p0.y;
199 minx = lmax(p0.x, clipr.min.x);
200 maxx = lmin(p1.x, clipr.max.x-1);
202 m0 = 0xFF^(0xFF>>bpp);
203 m = m0 >> (minx&(7/dst->depth))*bpp;
204 for(x=minx; x<=maxx; x++){
205 y = p0.y + (deltay*(x-p0.x)+deltay/2)/deltax;
206 if(clipr.min.y<=y && y<clipr.max.y){
207 d = byteaddr(dst, Pt(x, y));
208 s = byteaddr(src, addpt(dsrc, Pt(x, y)));
219 verline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
221 int x, y, deltay, deltax, miny, maxy;
225 deltax = p1.x - p0.x;
226 deltay = p1.y - p0.y;
227 miny = lmax(p0.y, clipr.min.y);
228 maxy = lmin(p1.y, clipr.max.y-1);
230 m0 = 0xFF^(0xFF>>bpp);
231 for(y=miny; y<=maxy; y++){
232 if(deltay == 0) /* degenerate line */
235 x = p0.x + deltax*(y-p0.y)/deltay;
236 if(clipr.min.x<=x && x<clipr.max.x){
237 m = m0 >> (x&(7/dst->depth))*bpp;
238 d = byteaddr(dst, Pt(x, y));
239 s = byteaddr(src, addpt(dsrc, Pt(x, y)));
249 static Memimage *brush;
250 static int brushradius;
252 if(brush==nil || brushradius!=radius){
254 brush = allocmemimage(Rect(0, 0, 2*radius+1, 2*radius+1), memopaque->chan);
256 memfillcolor(brush, DTransparent); /* zeros */
257 memellipse(brush, Pt(radius, radius), radius, radius, -1, memopaque, Pt(radius, radius), S);
259 brushradius = radius;
266 discend(Point p, int radius, Memimage *dst, Memimage *src, Point dsrc, int op)
271 disc = membrush(radius);
273 r.min.x = p.x - radius;
274 r.min.y = p.y - radius;
275 r.max.x = p.x + radius+1;
276 r.max.y = p.y + radius+1;
277 memdraw(dst, r, src, addpt(r.min, dsrc), disc, Pt(0,0), op);
283 arrowend(Point tip, Point *pp, int end, int sin, int cos, int radius)
287 /* before rotation */
293 x1 = (end>>5) & 0x1FF; /* distance along line from end of line to tip */
294 x2 = (end>>14) & 0x1FF; /* distance along line from barb to tip */
295 x3 = (end>>23) & 0x1FF; /* distance perpendicular from edge of line to barb */
298 /* comments follow track of right-facing arrowhead */
299 pp->x = tip.x+((2*radius+1)*sin/2-x1*cos); /* upper side of shaft */
300 pp->y = tip.y-((2*radius+1)*cos/2+x1*sin);
302 pp->x = tip.x+((2*radius+2*x3+1)*sin/2-x2*cos); /* upper barb */
303 pp->y = tip.y-((2*radius+2*x3+1)*cos/2+x2*sin);
308 pp->x = tip.x+(-(2*radius+2*x3+1)*sin/2-x2*cos); /* lower barb */
309 pp->y = tip.y-(-(2*radius+2*x3+1)*cos/2+x2*sin);
311 pp->x = tip.x+(-(2*radius+1)*sin/2-x1*cos); /* lower side of shaft */
312 pp->y = tip.y+((2*radius+1)*cos/2-x1*sin);
316 _memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr, int op)
319 * BUG: We should really really pick off purely horizontal and purely
320 * vertical lines and handle them separately with calls to memimagedraw
325 int sin, cos, dx, dy, t;
327 Point q, pts[10], *pp, d;
331 if(rectclip(&clipr, dst->r) == 0)
333 if(rectclip(&clipr, dst->clipr) == 0)
336 if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0)
338 if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0)
340 /* this means that only verline() handles degenerate lines (p0==p1) */
341 hor = (abs(p1.x-p0.x) > abs(p1.y-p0.y));
343 * Clipping is a little peculiar. We can't use Sutherland-Cohen
344 * clipping because lines are wide. But this is probably just fine:
345 * we do all math with the original p0 and p1, but clip when deciding
346 * what pixels to draw. This means the layer code can call this routine,
347 * using clipr to define the region being written, and get the same set
348 * of pixels regardless of the dicing.
350 if((hor && p0.x>p1.x) || (!hor && p0.y>p1.y)){
359 if((p0.x == p1.x || p0.y == p1.y) && (end0&0x1F) == Endsquare && (end1&0x1F) == Endsquare){
371 sp = addpt(r.min, d);
373 memimagedraw(dst, r, src, sp, memopaque, sp, op);
379 /* draw thick line using polygon fill */
380 icossin2(p1.x-p0.x, p1.y-p0.y, &cos, &sin);
381 dx = (sin*(2*radius+1))/2;
382 dy = (cos*(2*radius+1))/2;
386 q.x = ICOSSCALE*p0.x+ICOSSCALE/2-cos/2;
387 q.y = ICOSSCALE*p0.y+ICOSSCALE/2-sin/2;
390 discend(p0, radius, dst, src, d, op);
402 arrowend(q, pp, end0, -sin, -cos, radius);
403 _memfillpolysc(dst, pts, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
407 q.x = ICOSSCALE*p1.x+ICOSSCALE/2+cos/2;
408 q.y = ICOSSCALE*p1.y+ICOSSCALE/2+sin/2;
411 discend(p1, radius, dst, src, d, op);
423 arrowend(q, pp, end1, sin, cos, radius);
424 _memfillpolysc(dst, pp, 5, ~0, src, addpt(pp[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
428 _memfillpolysc(dst, pts, pp-pts, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 0, 10, 1, op);
434 memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, int op)
436 _memimageline(dst, p0, p1, end0, end1, radius, src, sp, dst->clipr, op);
440 * Simple-minded conservative code to compute bounding box of line.
441 * Result is probably a little larger than it needs to be.
445 addbbox(Rectangle *r, Point p)
458 memlineendsize(int end)
462 if((end&0x3F) != Endarrow)
467 x3 = (end>>23) & 0x1FF;
472 memlinebbox(Point p0, Point p1, int end0, int end1, int radius)
481 extra = lmax(memlineendsize(end0), memlineendsize(end1));
482 r1 = insetrect(canonrect(Rpt(p0, p1)), -(radius+extra));