blob: 1b19fc1c42a98bf81368b1a519134304a3175651 [file] [log] [blame]
rsc76193d72003-09-30 17:47:42 +00001#include <u.h>
2#include <libc.h>
3#include <draw.h>
4
5Point
6Pt(int x, int y)
7{
8 Point p;
9
10 p.x = x;
11 p.y = y;
12 return p;
13}
14
15Rectangle
16Rect(int x, int y, int bx, int by)
17{
18 Rectangle r;
19
20 r.min.x = x;
21 r.min.y = y;
22 r.max.x = bx;
23 r.max.y = by;
24 return r;
25}
26
27Rectangle
28Rpt(Point min, Point max)
29{
30 Rectangle r;
31
32 r.min = min;
33 r.max = max;
34 return r;
35}
36
37Point
38addpt(Point a, Point b)
39{
40 a.x += b.x;
41 a.y += b.y;
42 return a;
43}
44
45Point
46subpt(Point a, Point b)
47{
48 a.x -= b.x;
49 a.y -= b.y;
50 return a;
51}
52
53Rectangle
54insetrect(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}
62
63Point
64divpt(Point a, int b)
65{
66 a.x /= b;
67 a.y /= b;
68 return a;
69}
70
71Point
72mulpt(Point a, int b)
73{
74 a.x *= b;
75 a.y *= b;
76 return a;
77}
78
79Rectangle
80rectsubpt(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}
88
89Rectangle
90rectaddpt(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}
98
99int
100eqpt(Point p, Point q)
101{
102 return p.x==q.x && p.y==q.y;
103}
104
105int
106eqrect(Rectangle r, Rectangle s)
107{
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;
110}
111
112int
113rectXrect(Rectangle r, Rectangle s)
114{
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;
117}
118
119int
120rectinrect(Rectangle r, Rectangle s)
121{
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;
123}
124
125int
126ptinrect(Point p, Rectangle r)
127{
128 return p.x>=r.min.x && p.x<r.max.x &&
129 p.y>=r.min.y && p.y<r.max.y;
130}
131
132Rectangle
133canonrect(Rectangle r)
134{
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;
140 }
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;
145 }
146 return r;
147}
148
149void
150combinerect(Rectangle *r1, Rectangle r2)
151{
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;
160}
161
162u32int
163drawld2chan[] = {
164 GREY1,
165 GREY2,
166 GREY4,
167 CMAP8,
168};
169
rsc76193d72003-09-30 17:47:42 +0000170u32int
171setalpha(u32int color, uchar alpha)
172{
173 int red, green, blue;
174
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);
183}
184
185Point ZP;
186Rectangle ZR;
187int
188Rfmt(Fmt *f)
189{
190 Rectangle r;
191
192 r = va_arg(f->args, Rectangle);
193 return fmtprint(f, "%P %P", r.min, r.max);
194}
195
196int
197Pfmt(Fmt *f)
198{
199 Point p;
200
201 p = va_arg(f->args, Point);
202 return fmtprint(f, "[%d %d]", p.x, p.y);
203}
204