aboutsummaryrefslogtreecommitdiff
path: root/src/libdraw/arith.c
blob: 1b19fc1c42a98bf81368b1a519134304a3175651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <u.h>
#include <libc.h>
#include <draw.h>

Point
Pt(int x, int y)
{
	Point p;

	p.x = x;
	p.y = y;
	return p;
}

Rectangle
Rect(int x, int y, int bx, int by)
{
	Rectangle r;

	r.min.x = x;
	r.min.y = y;
	r.max.x = bx;
	r.max.y = by;
	return r;
}

Rectangle
Rpt(Point min, Point max)
{
	Rectangle r;

	r.min = min;
	r.max = max;
	return r;
}

Point
addpt(Point a, Point b)
{
	a.x += b.x;
	a.y += b.y;
	return a;
}

Point
subpt(Point a, Point b)
{
	a.x -= b.x;
	a.y -= b.y;
	return a;
}

Rectangle
insetrect(Rectangle r, int n)
{
	r.min.x += n;
	r.min.y += n;
	r.max.x -= n;
	r.max.y -= n;
	return r;
}

Point
divpt(Point a, int b)
{
	a.x /= b;
	a.y /= b;
	return a;
}

Point
mulpt(Point a, int b)
{
	a.x *= b;
	a.y *= b;
	return a;
}

Rectangle
rectsubpt(Rectangle r, Point p)
{
	r.min.x -= p.x;
	r.min.y -= p.y;
	r.max.x -= p.x;
	r.max.y -= p.y;
	return r;
}

Rectangle
rectaddpt(Rectangle r, Point p)
{
	r.min.x += p.x;
	r.min.y += p.y;
	r.max.x += p.x;
	r.max.y += p.y;
	return r;
}

int
eqpt(Point p, Point q)
{
	return p.x==q.x && p.y==q.y;
}

int
eqrect(Rectangle r, Rectangle s)
{
	return r.min.x==s.min.x && r.max.x==s.max.x &&
	       r.min.y==s.min.y && r.max.y==s.max.y;
}

int
rectXrect(Rectangle r, Rectangle s)
{
	return r.min.x<s.max.x && s.min.x<r.max.x &&
	       r.min.y<s.max.y && s.min.y<r.max.y;
}

int
rectinrect(Rectangle r, Rectangle s)
{
	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;
}

int
ptinrect(Point p, Rectangle r)
{
	return p.x>=r.min.x && p.x<r.max.x &&
	       p.y>=r.min.y && p.y<r.max.y;
}

Rectangle
canonrect(Rectangle r)
{
	int t;
	if (r.max.x < r.min.x) {
		t = r.min.x;
		r.min.x = r.max.x;
		r.max.x = t;
	}
	if (r.max.y < r.min.y) {
		t = r.min.y;
		r.min.y = r.max.y;
		r.max.y = t;
	}
	return r;
}

void
combinerect(Rectangle *r1, Rectangle r2)
{
	if(r1->min.x > r2.min.x)
		r1->min.x = r2.min.x;
	if(r1->min.y > r2.min.y)
		r1->min.y = r2.min.y;
	if(r1->max.x < r2.max.x)
		r1->max.x = r2.max.x;
	if(r1->max.y < r2.max.y)
		r1->max.y = r2.max.y;
}

u32int
drawld2chan[] = {
	GREY1,
	GREY2,
	GREY4,
	CMAP8,
};

u32int
setalpha(u32int color, uchar alpha)
{
	int red, green, blue;

	red = (color >> 3*8) & 0xFF;
	green = (color >> 2*8) & 0xFF;
	blue = (color >> 1*8) & 0xFF;
	/* ignore incoming alpha */
	red = (red * alpha)/255;
	green = (green * alpha)/255;
	blue = (blue * alpha)/255;
	return (red<<3*8) | (green<<2*8) | (blue<<1*8) | (alpha<<0*8);
}

Point	ZP;
Rectangle ZR;
int
Rfmt(Fmt *f)
{
	Rectangle r;

	r = va_arg(f->args, Rectangle);
	return fmtprint(f, "%P %P", r.min, r.max);
}

int
Pfmt(Fmt *f)
{
	Point p;

	p = va_arg(f->args, Point);
	return fmtprint(f, "[%d %d]", p.x, p.y);
}