xref: /inferno-os/libdraw/arith.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include "lib9.h"
2 #include "draw.h"
3 
4 Point
Pt(int x,int y)5 Pt(int x, int y)
6 {
7 	Point p;
8 
9 	p.x = x;
10 	p.y = y;
11 	return p;
12 }
13 
14 Rectangle
Rect(int x,int y,int bx,int by)15 Rect(int x, int y, int bx, int by)
16 {
17 	Rectangle r;
18 
19 	r.min.x = x;
20 	r.min.y = y;
21 	r.max.x = bx;
22 	r.max.y = by;
23 	return r;
24 }
25 
26 Rectangle
Rpt(Point min,Point max)27 Rpt(Point min, Point max)
28 {
29 	Rectangle r;
30 
31 	r.min = min;
32 	r.max = max;
33 	return r;
34 }
35 
36 Point
addpt(Point a,Point b)37 addpt(Point a, Point b)
38 {
39 	a.x += b.x;
40 	a.y += b.y;
41 	return a;
42 }
43 
44 Point
subpt(Point a,Point b)45 subpt(Point a, Point b)
46 {
47 	a.x -= b.x;
48 	a.y -= b.y;
49 	return a;
50 }
51 
52 Rectangle
insetrect(Rectangle r,int n)53 insetrect(Rectangle r, int n)
54 {
55 	r.min.x += n;
56 	r.min.y += n;
57 	r.max.x -= n;
58 	r.max.y -= n;
59 	return r;
60 }
61 
62 Point
divpt(Point a,int b)63 divpt(Point a, int b)
64 {
65 	a.x /= b;
66 	a.y /= b;
67 	return a;
68 }
69 
70 Point
mulpt(Point a,int b)71 mulpt(Point a, int b)
72 {
73 	a.x *= b;
74 	a.y *= b;
75 	return a;
76 }
77 
78 Rectangle
rectsubpt(Rectangle r,Point p)79 rectsubpt(Rectangle r, Point p)
80 {
81 	r.min.x -= p.x;
82 	r.min.y -= p.y;
83 	r.max.x -= p.x;
84 	r.max.y -= p.y;
85 	return r;
86 }
87 
88 Rectangle
rectaddpt(Rectangle r,Point p)89 rectaddpt(Rectangle r, Point p)
90 {
91 	r.min.x += p.x;
92 	r.min.y += p.y;
93 	r.max.x += p.x;
94 	r.max.y += p.y;
95 	return r;
96 }
97 
98 int
eqpt(Point p,Point q)99 eqpt(Point p, Point q)
100 {
101 	return p.x==q.x && p.y==q.y;
102 }
103 
104 int
eqrect(Rectangle r,Rectangle s)105 eqrect(Rectangle r, Rectangle s)
106 {
107 	return r.min.x==s.min.x && r.max.x==s.max.x &&
108 	       r.min.y==s.min.y && r.max.y==s.max.y;
109 }
110 
111 int
rectXrect(Rectangle r,Rectangle s)112 rectXrect(Rectangle r, Rectangle s)
113 {
114 	return r.min.x<s.max.x && s.min.x<r.max.x &&
115 	       r.min.y<s.max.y && s.min.y<r.max.y;
116 }
117 
118 int
rectinrect(Rectangle r,Rectangle s)119 rectinrect(Rectangle r, Rectangle s)
120 {
121 	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;
122 }
123 
124 int
ptinrect(Point p,Rectangle r)125 ptinrect(Point p, Rectangle r)
126 {
127 	return p.x>=r.min.x && p.x<r.max.x &&
128 	       p.y>=r.min.y && p.y<r.max.y;
129 }
130 
131 Rectangle
canonrect(Rectangle r)132 canonrect(Rectangle r)
133 {
134 	int t;
135 	if (r.max.x < r.min.x) {
136 		t = r.min.x;
137 		r.min.x = r.max.x;
138 		r.max.x = t;
139 	}
140 	if (r.max.y < r.min.y) {
141 		t = r.min.y;
142 		r.min.y = r.max.y;
143 		r.max.y = t;
144 	}
145 	return r;
146 }
147 
148 void
combinerect(Rectangle * r1,Rectangle r2)149 combinerect(Rectangle *r1, Rectangle r2)
150 {
151 	if(r1->min.x > r2.min.x)
152 		r1->min.x = r2.min.x;
153 	if(r1->min.y > r2.min.y)
154 		r1->min.y = r2.min.y;
155 	if(r1->max.x < r2.max.x)
156 		r1->max.x = r2.max.x;
157 	if(r1->max.y < r2.max.y)
158 		r1->max.y = r2.max.y;
159 }
160 
161 ulong
162 drawld2chan[] = {
163 	GREY1,
164 	GREY2,
165 	GREY4,
166 	CMAP8,
167 };
168 
169 ulong
setalpha(ulong color,uchar alpha)170 setalpha(ulong color, uchar alpha)
171 {
172 	int red, green, blue;
173 
174 	red = (color >> 3*8) & 0xFF;
175 	green = (color >> 2*8) & 0xFF;
176 	blue = (color >> 1*8) & 0xFF;
177 	/* ignore incoming alpha */
178 	red = (red * alpha)/255;
179 	green = (green * alpha)/255;
180 	blue = (blue * alpha)/255;
181 	return (red<<3*8) | (green<<2*8) | (blue<<1*8) | (alpha<<0*8);
182 }
183 
184 Point	ZP;
185 Rectangle ZR;
186 int
Rfmt(Fmt * f)187 Rfmt(Fmt *f)
188 {
189 	Rectangle r;
190 
191 	r = va_arg(f->args, Rectangle);
192 	return fmtprint(f, "%P %P", r.min, r.max);
193 }
194 
195 int
Pfmt(Fmt * f)196 Pfmt(Fmt *f)
197 {
198 	Point p;
199 
200 	p = va_arg(f->args, Point);
201 	return fmtprint(f, "[%d %d]", p.x, p.y);
202 }
203 
204