Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
5 Point
6 Pt(int x, int y)
7 {
8 Point p;
10 p.x = x;
11 p.y = y;
12 return p;
13 }
15 Rectangle
16 Rect(int x, int y, int bx, int by)
17 {
18 Rectangle r;
20 r.min.x = x;
21 r.min.y = y;
22 r.max.x = bx;
23 r.max.y = by;
24 return r;
25 }
27 Rectangle
28 Rpt(Point min, Point max)
29 {
30 Rectangle r;
32 r.min = min;
33 r.max = max;
34 return r;
35 }
37 Point
38 addpt(Point a, Point b)
39 {
40 a.x += b.x;
41 a.y += b.y;
42 return a;
43 }
45 Point
46 subpt(Point a, Point b)
47 {
48 a.x -= b.x;
49 a.y -= b.y;
50 return a;
51 }
53 Rectangle
54 insetrect(Rectangle r, int n)
55 {
56 r.min.x += n;
57 r.min.y += n;
58 r.max.x -= n;
59 r.max.y -= n;
60 return r;
61 }
63 Point
64 divpt(Point a, int b)
65 {
66 a.x /= b;
67 a.y /= b;
68 return a;
69 }
71 Point
72 mulpt(Point a, int b)
73 {
74 a.x *= b;
75 a.y *= b;
76 return a;
77 }
79 Rectangle
80 rectsubpt(Rectangle r, Point p)
81 {
82 r.min.x -= p.x;
83 r.min.y -= p.y;
84 r.max.x -= p.x;
85 r.max.y -= p.y;
86 return r;
87 }
89 Rectangle
90 rectaddpt(Rectangle r, Point p)
91 {
92 r.min.x += p.x;
93 r.min.y += p.y;
94 r.max.x += p.x;
95 r.max.y += p.y;
96 return r;
97 }
99 int
100 eqpt(Point p, Point q)
102 return p.x==q.x && p.y==q.y;
105 int
106 eqrect(Rectangle r, Rectangle s)
108 return r.min.x==s.min.x && r.max.x==s.max.x &&
109 r.min.y==s.min.y && r.max.y==s.max.y;
112 int
113 rectXrect(Rectangle r, Rectangle s)
115 return r.min.x<s.max.x && s.min.x<r.max.x &&
116 r.min.y<s.max.y && s.min.y<r.max.y;
119 int
120 rectinrect(Rectangle r, Rectangle s)
122 return s.min.x<=r.min.x && r.max.x<=s.max.x && s.min.y<=r.min.y && r.max.y<=s.max.y;
125 int
126 ptinrect(Point p, Rectangle r)
128 return p.x>=r.min.x && p.x<r.max.x &&
129 p.y>=r.min.y && p.y<r.max.y;
132 Rectangle
133 canonrect(Rectangle r)
135 int t;
136 if (r.max.x < r.min.x) {
137 t = r.min.x;
138 r.min.x = r.max.x;
139 r.max.x = t;
141 if (r.max.y < r.min.y) {
142 t = r.min.y;
143 r.min.y = r.max.y;
144 r.max.y = t;
146 return r;
149 void
150 combinerect(Rectangle *r1, Rectangle r2)
152 if(r1->min.x > r2.min.x)
153 r1->min.x = r2.min.x;
154 if(r1->min.y > r2.min.y)
155 r1->min.y = r2.min.y;
156 if(r1->max.x < r2.max.x)
157 r1->max.x = r2.max.x;
158 if(r1->max.y < r2.max.y)
159 r1->max.y = r2.max.y;
162 u32int
163 drawld2chan[] = {
164 GREY1,
165 GREY2,
166 GREY4,
167 CMAP8,
168 };
170 u32int
171 setalpha(u32int color, uchar alpha)
173 int red, green, blue;
175 red = (color >> 3*8) & 0xFF;
176 green = (color >> 2*8) & 0xFF;
177 blue = (color >> 1*8) & 0xFF;
178 /* ignore incoming alpha */
179 red = (red * alpha)/255;
180 green = (green * alpha)/255;
181 blue = (blue * alpha)/255;
182 return (red<<3*8) | (green<<2*8) | (blue<<1*8) | (alpha<<0*8);
185 Point ZP;
186 Rectangle ZR;
187 int
188 Rfmt(Fmt *f)
190 Rectangle r;
192 r = va_arg(f->args, Rectangle);
193 return fmtprint(f, "%P %P", r.min, r.max);
196 int
197 Pfmt(Fmt *f)
199 Point p;
201 p = va_arg(f->args, Point);
202 return fmtprint(f, "[%d %d]", p.x, p.y);