1
2 package org.xwt;
3
4
22 public abstract class PixelBuffer {
23
24
25 protected abstract void drawPicture(Picture source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2);
26
27
28 public abstract void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
29
30
35 public abstract void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb);
36
37
38
39 public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
40
41 if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
42
43 if (x1 == x2) {
44 fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
45 x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
46 return;
47 }
48
49
50 if (w == 1) {
51 float slope = (float)(y2 - y1) / (float)(x2 - x1);
52 int last_x = x1;
53 for(int y=y1; y<=y2; y++) {
54 int new_x = (int)((float)(y - y1) / slope) + x1;
55 if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
56 last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
57 else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
58 y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
59 last_x = new_x;
60 }
61 return;
62 }
63
64
65 float width = (float)w / 2;
66 float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
67 if (phi < 0.0) phi += (float)Math.PI * 2;
68 float theta = (float)Math.PI / 2 - phi;
69
70
71 int dx = (int)(width * Math.cos(theta));
72 int dy = (int)(width * Math.sin(theta));
73
74
75 int slice = (int)(2 * width / Math.cos(theta));
76
77 if (capped) {
78 x1 -= width * Math.cos(phi);
79 x2 += width * Math.cos(phi);
80 y1 -= width * Math.sin(phi);
81 y2 += width * Math.sin(phi);
82 }
83
84 fillTrapezoid(x1 + dx, x1 + dx, y1 - dy, x1 - dx, x1 - dx + slice, y1 + dy, color);
85 fillTrapezoid(x2 + dx - slice, x2 + dx, y2 - dy, x2 - dx, x2 - dx, y2 + dy, color);
86 fillTrapezoid(x1 - dx, x1 - dx + slice, y1 + dy, x2 + dx - slice, x2 + dx, y2 - dy, color);
87 }
88
89 }
90