33 * Rather than line clip, we run the Bresenham loop over the full line,
34 * and clip on each pixel. This is more expensive but means that
35 * lines look the same regardless of how the windowing has tiled them.
36 * For speed, we check for clipping outside the loop and make the
37 * test easy when possible.
42 horline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
44 int x, y, dy, deltay, deltax, maxx;
45 int dd, easy, e, bpp, m, m0;
50 dd = dst->width*sizeof(u32int);
57 maxx = lmin(p1.x, clipr.max.x-1);
59 m0 = 0xFF^(0xFF>>bpp);
60 m = m0 >> (p0.x&(7/dst->depth))*bpp;
61 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
62 e = 2*deltay - deltax;
64 d = byteaddr(dst, p0);
66 deltax = deltay - 2*deltax;
67 for(x=p0.x; x<=maxx; x++){
68 if(easy || (clipr.min.x<=x && clipr.min.y<=y && y<clipr.max.y))
69 *d ^= (*d^srcval) & m;
85 verline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
87 int x, y, deltay, deltax, maxy;
88 int easy, e, bpp, m, m0, dd;
98 maxy = lmin(p1.y, clipr.max.y-1);
100 m0 = 0xFF^(0xFF>>bpp);
101 m = m0 >> (p0.x&(7/dst->depth))*bpp;
102 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
103 e = 2*deltax - deltay;
105 d = byteaddr(dst, p0);
107 deltay = deltax - 2*deltay;
108 for(y=p0.y; y<=maxy; y++){
109 if(easy || (clipr.min.y<=y && clipr.min.x<=x && x<clipr.max.x))
110 *d ^= (*d^srcval) & m;
117 d += dst->width*sizeof(u32int);
126 horliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
128 int x, y, sx, sy, deltay, deltax, minx, maxx;
132 deltax = p1.x - p0.x;
133 deltay = p1.y - p0.y;
134 sx = drawreplxy(src->r.min.x, src->r.max.x, p0.x+dsrc.x);
135 minx = lmax(p0.x, clipr.min.x);
136 maxx = lmin(p1.x, clipr.max.x-1);
138 m0 = 0xFF^(0xFF>>bpp);
139 m = m0 >> (minx&(7/dst->depth))*bpp;
140 for(x=minx; x<=maxx; x++){
141 y = p0.y + (deltay*(x-p0.x)+deltax/2)/deltax;
142 if(clipr.min.y<=y && y<clipr.max.y){
143 d = byteaddr(dst, Pt(x, y));
144 sy = drawreplxy(src->r.min.y, src->r.max.y, y+dsrc.y);
145 s = byteaddr(src, Pt(sx, sy));
148 if(++sx >= src->r.max.x)
158 verliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
160 int x, y, sx, sy, deltay, deltax, miny, maxy;
164 deltax = p1.x - p0.x;
165 deltay = p1.y - p0.y;
166 sy = drawreplxy(src->r.min.y, src->r.max.y, p0.y+dsrc.y);
167 miny = lmax(p0.y, clipr.min.y);
168 maxy = lmin(p1.y, clipr.max.y-1);
170 m0 = 0xFF^(0xFF>>bpp);
171 for(y=miny; y<=maxy; y++){
172 if(deltay == 0) /* degenerate line */
175 x = p0.x + (deltax*(y-p0.y)+deltay/2)/deltay;
176 if(clipr.min.x<=x && x<clipr.max.x){
177 m = m0 >> (x&(7/dst->depth))*bpp;
178 d = byteaddr(dst, Pt(x, y));
179 sx = drawreplxy(src->r.min.x, src->r.max.x, x+dsrc.x);
180 s = byteaddr(src, Pt(sx, sy));
183 if(++sy >= src->r.max.y)
190 horline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
192 int x, y, deltay, deltax, minx, maxx;
196 deltax = p1.x - p0.x;
197 deltay = p1.y - p0.y;
198 minx = lmax(p0.x, clipr.min.x);
199 maxx = lmin(p1.x, clipr.max.x-1);
201 m0 = 0xFF^(0xFF>>bpp);
202 m = m0 >> (minx&(7/dst->depth))*bpp;
203 for(x=minx; x<=maxx; x++){
204 y = p0.y + (deltay*(x-p0.x)+deltay/2)/deltax;
205 if(clipr.min.y<=y && y<clipr.max.y){
206 d = byteaddr(dst, Pt(x, y));
207 s = byteaddr(src, addpt(dsrc, Pt(x, y)));
218 verline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
220 int x, y, deltay, deltax, miny, maxy;
224 deltax = p1.x - p0.x;
225 deltay = p1.y - p0.y;
226 miny = lmax(p0.y, clipr.min.y);
227 maxy = lmin(p1.y, clipr.max.y-1);
229 m0 = 0xFF^(0xFF>>bpp);
230 for(y=miny; y<=maxy; y++){
231 if(deltay == 0) /* degenerate line */
234 x = p0.x + deltax*(y-p0.y)/deltay;
235 if(clipr.min.x<=x && x<clipr.max.x){
236 m = m0 >> (x&(7/dst->depth))*bpp;
237 d = byteaddr(dst, Pt(x, y));
238 s = byteaddr(src, addpt(dsrc, Pt(x, y)));
248 static Memimage *brush;
249 static int brushradius;
251 if(brush==nil || brushradius!=radius){
253 brush = allocmemimage(Rect(0, 0, 2*radius+1, 2*radius+1), memopaque->chan);
255 memfillcolor(brush, DTransparent); /* zeros */
256 memellipse(brush, Pt(radius, radius), radius, radius, -1, memopaque, Pt(radius, radius), S);
258 brushradius = radius;
265 discend(Point p, int radius, Memimage *dst, Memimage *src, Point dsrc, int op)
270 disc = membrush(radius);
272 r.min.x = p.x - radius;
273 r.min.y = p.y - radius;
274 r.max.x = p.x + radius+1;
275 r.max.y = p.y + radius+1;
276 memdraw(dst, r, src, addpt(r.min, dsrc), disc, Pt(0,0), op);
282 arrowend(Point tip, Point *pp, int end, int sin, int cos, int radius)
286 /* before rotation */
292 x1 = (end>>5) & 0x1FF; /* distance along line from end of line to tip */
293 x2 = (end>>14) & 0x1FF; /* distance along line from barb to tip */
294 x3 = (end>>23) & 0x1FF; /* distance perpendicular from edge of line to barb */
297 /* comments follow track of right-facing arrowhead */
298 pp->x = tip.x+((2*radius+1)*sin/2-x1*cos); /* upper side of shaft */
299 pp->y = tip.y-((2*radius+1)*cos/2+x1*sin);
301 pp->x = tip.x+((2*radius+2*x3+1)*sin/2-x2*cos); /* upper barb */
302 pp->y = tip.y-((2*radius+2*x3+1)*cos/2+x2*sin);
307 pp->x = tip.x+(-(2*radius+2*x3+1)*sin/2-x2*cos); /* lower barb */
308 pp->y = tip.y-(-(2*radius+2*x3+1)*cos/2+x2*sin);
310 pp->x = tip.x+(-(2*radius+1)*sin/2-x1*cos); /* lower side of shaft */
311 pp->y = tip.y+((2*radius+1)*cos/2-x1*sin);
315 _memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr, int op)
318 * BUG: We should really really pick off purely horizontal and purely
319 * vertical lines and handle them separately with calls to memimagedraw
324 int sin, cos, dx, dy, t;
326 Point q, pts[10], *pp, d;
330 if(rectclip(&clipr, dst->r) == 0)
332 if(rectclip(&clipr, dst->clipr) == 0)
335 if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0)
337 if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0)
339 /* this means that only verline() handles degenerate lines (p0==p1) */
340 hor = (abs(p1.x-p0.x) > abs(p1.y-p0.y));
342 * Clipping is a little peculiar. We can't use Sutherland-Cohen
343 * clipping because lines are wide. But this is probably just fine:
344 * we do all math with the original p0 and p1, but clip when deciding
345 * what pixels to draw. This means the layer code can call this routine,
346 * using clipr to define the region being written, and get the same set
347 * of pixels regardless of the dicing.
349 if((hor && p0.x>p1.x) || (!hor && p0.y>p1.y)){
358 if((p0.x == p1.x || p0.y == p1.y) && (end0&0x1F) == Endsquare && (end1&0x1F) == Endsquare){
371 memimagedraw(dst, r, src, sp, memopaque, sp, op);
377 /* draw thick line using polygon fill */
378 icossin2(p1.x-p0.x, p1.y-p0.y, &cos, &sin);
379 dx = (sin*(2*radius+1))/2;
380 dy = (cos*(2*radius+1))/2;
384 q.x = ICOSSCALE*p0.x+ICOSSCALE/2-cos/2;
385 q.y = ICOSSCALE*p0.y+ICOSSCALE/2-sin/2;
388 discend(p0, radius, dst, src, d, op);
400 arrowend(q, pp, end0, -sin, -cos, radius);
401 _memfillpolysc(dst, pts, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
405 q.x = ICOSSCALE*p1.x+ICOSSCALE/2+cos/2;
406 q.y = ICOSSCALE*p1.y+ICOSSCALE/2+sin/2;
409 discend(p1, radius, dst, src, d, op);
421 arrowend(q, pp, end1, sin, cos, radius);
422 _memfillpolysc(dst, pp, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
426 _memfillpolysc(dst, pts, pp-pts, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 0, 10, 1, op);
432 memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, int op)
434 _memimageline(dst, p0, p1, end0, end1, radius, src, sp, dst->clipr, op);
438 * Simple-minded conservative code to compute bounding box of line.
439 * Result is probably a little larger than it needs to be.
443 addbbox(Rectangle *r, Point p)
456 memlineendsize(int end)
460 if((end&0x3F) != Endarrow)
465 x3 = (end>>23) & 0x1FF;
470 memlinebbox(Point p0, Point p1, int end0, int end1, int radius)
479 extra = lmax(memlineendsize(end0), memlineendsize(end1));
480 r1 = insetrect(canonrect(Rpt(p0, p1)), -(radius+extra));