]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/arith.c
vt: implement /dev/cons and /dev/consctl as a fileserver, winch, incremental redraw
[plan9front.git] / sys / src / libdraw / arith.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 Point
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
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
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
38 addpt(Point a, Point b)
39 {
40         a.x += b.x;
41         a.y += b.y;
42         return a;
43 }
44
45 Point
46 subpt(Point a, Point b)
47 {
48         a.x -= b.x;
49         a.y -= b.y;
50         return a;
51 }
52
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 }
62
63 Point
64 divpt(Point a, int b)
65 {
66         a.x /= b;
67         a.y /= b;
68         return a;
69 }
70
71 Point
72 mulpt(Point a, int b)
73 {
74         a.x *= b;
75         a.y *= b;
76         return a;
77 }
78
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 }
88
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 }
98
99 int
100 eqpt(Point p, Point q)
101 {
102         return p.x==q.x && p.y==q.y;
103 }
104
105 int
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
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
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
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
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
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 ulong drawld2chan[] = {
163         GREY1,
164         GREY2,
165         GREY4,
166         CMAP8,
167 };
168
169 ulong
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;