1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 Point
Pt(int x,int y)6 Pt(int x, int y)
7 {
8 Point p;
9
10 p.x = x;
11 p.y = y;
12 return p;
13 }
14
15 Rectangle
Rect(int x,int y,int bx,int by)16 Rect(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
27 Rectangle
Rpt(Point min,Point max)28 Rpt(Point min, Point max)
29 {
30 Rectangle r;
31
32 r.min = min;
33 r.max = max;
34 return r;
35 }
36
37 Point
addpt(Point a,Point b)38 addpt(Point a, Point b)
39 {
40 a.x += b.x;
41 a.y += b.y;
42 return a;
43 }
44
45 Point
subpt(Point a,Point b)46 subpt(Point a, Point b)
47 {
48 a.x -= b.x;
49 a.y -= b.y;
50 return a;
51 }
52
53 Rectangle
insetrect(Rectangle r,int n)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 }
62
63 Point
divpt(Point a,int b)64 divpt(Point a, int b)
65 {
66 a.x /= b;
67 a.y /= b;
68 return a;
69 }
70
71 Point
mulpt(Point a,int b)72 mulpt(Point a, int b)
73 {
74 a.x *= b;
75 a.y *= b;
76 return a;
77 }
78
79 Rectangle
rectsubpt(Rectangle r,Point p)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 }
88
89 Rectangle
rectaddpt(Rectangle r,Point p)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 }
98
99 int
eqpt(Point p,Point q)100 eqpt(Point p, Point q)
101 {
102 return p.x==q.x && p.y==q.y;
103 }
104
105 int
eqrect(Rectangle r,Rectangle s)106 eqrect(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
112 int
rectXrect(Rectangle r,Rectangle s)113 rectXrect(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
119 int
rectinrect(Rectangle r,Rectangle s)120 rectinrect(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
125 int
ptinrect(Point p,Rectangle r)126 ptinrect(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
132 Rectangle
canonrect(Rectangle r)133 canonrect(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
149 void
combinerect(Rectangle * r1,Rectangle r2)150 combinerect(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
162 u32int
163 drawld2chan[] = {
164 GREY1,
165 GREY2,
166 GREY4,
167 CMAP8,
168 };
169
170 u32int
setalpha(u32int color,uchar alpha)171 setalpha(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
185 Point ZP;
186 Rectangle ZR;
187 int
Rfmt(Fmt * f)188 Rfmt(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
196 int
Pfmt(Fmt * f)197 Pfmt(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