35 * Rather than line clip, we run the Bresenham loop over the full line,
36 * and clip on each pixel. This is more expensive but means that
37 * lines look the same regardless of how the windowing has tiled them.
38 * For speed, we check for clipping outside the loop and make the
39 * test easy when possible.
44 horline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
46 int x, y, dy, deltay, deltax, maxx;
47 int dd, easy, e, bpp, m, m0;
52 dd = dst->width*sizeof(u32int);
59 maxx = lmin(p1.x, clipr.max.x-1);
61 m0 = 0xFF^(0xFF>>bpp);
62 m = m0 >> (p0.x&(7/dst->depth))*bpp;
63 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
64 e = 2*deltay - deltax;
66 d = byteaddr(dst, p0);
68 deltax = deltay - 2*deltax;
69 for(x=p0.x; x<=maxx; x++){
70 if(easy || (clipr.min.x<=x && clipr.min.y<=y && y<clipr.max.y))
71 *d ^= (*d^srcval) & m;
87 verline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
89 int x, y, deltay, deltax, maxy;
90 int easy, e, bpp, m, m0, dd;
100 maxy = lmin(p1.y, clipr.max.y-1);
102 m0 = 0xFF^(0xFF>>bpp);
103 m = m0 >> (p0.x&(7/dst->depth))*bpp;
104 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
105 e = 2*deltax - deltay;
107 d = byteaddr(dst, p0);
109 deltay = deltax - 2*deltay;
110 for(y=p0.y; y<=maxy; y++){
111 if(easy || (clipr.min.y<=y && clipr.min.x<=x && x<clipr.max.x))
112 *d ^= (*d^srcval) & m;
119 d += dst->width*sizeof(u32int);
128 horliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
130 int x, y, sx, sy, deltay, deltax, minx, maxx;
134 deltax = p1.x - p0.x;
135 deltay = p1.y - p0.y;
136 sx = drawreplxy(src->r.min.x, src->r.max.x, p0.x+dsrc.x);
137 minx = lmax(p0.x, clipr.min.x);
138 maxx = lmin(p1.x, clipr.max.x-1);
140 m0 = 0xFF^(0xFF>>bpp);
141 m = m0 >> (minx&(7/dst->depth))*bpp;
142 for(x=minx; x<=maxx; x++){
143 y = p0.y + (deltay*(x-p0.x)+deltax/2)/deltax;
144 if(clipr.min.y<=y && y<clipr.max.y){
145 d = byteaddr(dst, Pt(x, y));
146 sy = drawreplxy(src->r.min.y, src->r.max.y, y+dsrc.y);
147 s = byteaddr(src, Pt(sx, sy));
150 if(++sx >= src->r.max.x)
160 verliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
162 int x, y, sx, sy, deltay, deltax, miny, maxy;
166 deltax = p1.x - p0.x;
167 deltay = p1.y - p0.y;
168 sy = drawreplxy(src->r.min.y, src->r.max.y, p0.y+dsrc.y);
169 miny = lmax(p0.y, clipr.min.y);
170 maxy = lmin(p1.y, clipr.max.y-1);
172 m0 = 0xFF^(0xFF>>bpp);
173 for(y=miny; y<=maxy; y++){
174 if(deltay == 0) /* degenerate line */
177 x = p0.x + (deltax*(y-p0.y)+deltay/2)/deltay;
178 if(clipr.min.x<=x && x<clipr.max.x){
179 m = m0 >> (x&(7/dst->depth))*bpp;
180 d = byteaddr(dst, Pt(x, y));
181 sx = drawreplxy(src->r.min.x, src->r.max.x, x+dsrc.x);
182 s = byteaddr(src, Pt(sx, sy));
185 if(++sy >= src->r.max.y)
192 horline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
194 int x, y, deltay, deltax, minx, maxx;
198 deltax = p1.x - p0.x;
199 deltay = p1.y - p0.y;
200 minx = lmax(p0.x, clipr.min.x);
201 maxx = lmin(p1.x, clipr.max.x-1);
203 m0 = 0xFF^(0xFF>>bpp);
204 m = m0 >> (minx&(7/dst->depth))*bpp;
205 for(x=minx; x<=maxx; x++){
206 y = p0.y + (deltay*(x-p0.x)+deltay/2)/deltax;
207 if(clipr.min.y<=y && y<clipr.max.y){
208 d = byteaddr(dst, Pt(x, y));
209 s = byteaddr(src, addpt(dsrc, Pt(x, y)));
220 verline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
222 int x, y, deltay, deltax, miny, maxy;
226 deltax = p1.x - p0.x;
227 deltay = p1.y - p0.y;
228 miny = lmax(p0.y, clipr.min.y);
229 maxy = lmin(p1.y, clipr.max.y-1);
231 m0 = 0xFF^(0xFF>>bpp);
232 for(y=miny; y<=maxy; y++){
233 if(deltay == 0) /* degenerate line */
236 x = p0.x + deltax*(y-p0.y)/deltay;
237 if(clipr.min.x<=x && x<clipr.max.x){
238 m = m0 >> (x&(7/dst->depth))*bpp;
239 d = byteaddr(dst, Pt(x, y));
240 s = byteaddr(src, addpt(dsrc, Pt(x, y)));
250 static Memimage *brush;
251 static int brushradius;
253 if(brush==nil || brushradius!=radius){
255 brush = allocmemimage(Rect(0, 0, 2*radius+1, 2*radius+1), memopaque->chan);
257 memfillcolor(brush, DTransparent); /* zeros */
258 memellipse(brush, Pt(radius, radius), radius, radius, -1, memopaque, Pt(radius, radius), S);
260 brushradius = radius;
267 discend(Point p, int radius, Memimage *dst, Memimage *src, Point dsrc, int op)
272 disc = membrush(radius);
274 r.min.x = p.x - radius;
275 r.min.y = p.y - radius;
276 r.max.x = p.x + radius+1;
277 r.max.y = p.y + radius+1;
278 memdraw(dst, r, src, addpt(r.min, dsrc), disc, Pt(0,0), op);
284 arrowend(Point tip, Point *pp, int end, int sin, int cos, int radius)
288 /* before rotation */
294 x1 = (end>>5) & 0x1FF; /* distance along line from end of line to tip */
295 x2 = (end>>14) & 0x1FF; /* distance along line from barb to tip */
296 x3 = (end>>23) & 0x1FF; /* distance perpendicular from edge of line to barb */
299 /* comments follow track of right-facing arrowhead */
300 pp->x = tip.x+((2*radius+1)*sin/2-x1*cos); /* upper side of shaft */
301 pp->y = tip.y-((2*radius+1)*cos/2+x1*sin);
303 pp->x = tip.x+((2*radius+2*x3+1)*sin/2-x2*cos); /* upper barb */
304 pp->y = tip.y-((2*radius+2*x3+1)*cos/2+x2*sin);
309 pp->x = tip.x+(-(2*radius+2*x3+1)*sin/2-x2*cos); /* lower barb */
310 pp->y = tip.y-(-(2*radius+2*x3+1)*cos/2+x2*sin);
312 pp->x = tip.x+(-(2*radius+1)*sin/2-x1*cos); /* lower side of shaft */
313 pp->y = tip.y+((2*radius+1)*cos/2-x1*sin);
317 _memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr, int op)
320 * BUG: We should really really pick off purely horizontal and purely
321 * vertical lines and handle them separately with calls to memimagedraw
326 int sin, cos, dx, dy, t;
328 Point q, pts[10], *pp, d;
332 if(rectclip(&clipr, dst->r) == 0)
334 if(rectclip(&clipr, dst->clipr) == 0)
337 if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0)
339 if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0)
341 /* this means that only verline() handles degenerate lines (p0==p1) */
342 hor = (abs(p1.x-p0.x) > abs(p1.y-p0.y));
344 * Clipping is a little peculiar. We can't use Sutherland-Cohen
345 * clipping because lines are wide. But this is probably just fine:
346 * we do all math with the original p0 and p1, but clip when deciding
347 * what pixels to draw. This means the layer code can call this routine,
348 * using clipr to define the region being written, and get the same set
349 * of pixels regardless of the dicing.
351 if((hor && p0.x>p1.x) || (!hor && p0.y>p1.y)){
360 if((p0.x == p1.x || p0.y == p1.y) && (end0&0x1F) == Endsquare && (end1&0x1F) == Endsquare){
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(pts[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));