arith.c - sam - An updated version of the sam text editor.
(HTM) git clone git://vernunftzentrum.de/sam.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
arith.c (2785B)
---
1 /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
2 #include <u.h>
3 #include <libg.h>
4
5 Point
6 add(Point a, Point b)
7 {
8 a.x += b.x;
9 a.y += b.y;
10 return a;
11 }
12
13 Point
14 sub(Point a, Point b)
15 {
16 a.x -= b.x;
17 a.y -= b.y;
18 return a;
19 }
20
21 Rectangle
22 inset(Rectangle r, int n)
23 {
24 r.min.x += n;
25 r.min.y += n;
26 r.max.x -= n;
27 r.max.y -= n;
28 return r;
29 }
30
31 Point
32 divpt(Point a, int b)
33 {
34 a.x /= b;
35 a.y /= b;
36 return a;
37 }
38
39 Point
40 mul(Point a, int b)
41 {
42 a.x *= b;
43 a.y *= b;
44 return a;
45 }
46
47 Rectangle
48 rsubp(Rectangle r, Point p)
49 {
50 r.min.x -= p.x;
51 r.min.y -= p.y;
52 r.max.x -= p.x;
53 r.max.y -= p.y;
54 return r;
55 }
56
57 Rectangle
58 raddp(Rectangle r, Point p)
59 {
60 r.min.x += p.x;
61 r.min.y += p.y;
62 r.max.x += p.x;
63 r.max.y += p.y;
64 return r;
65 }
66
67 Rectangle
68 rmul(Rectangle r, int a)
69 {
70 if (a != 1) {
71 r.min.x *= a;
72 r.min.y *= a;
73 r.max.x *= a;
74 r.max.y *= a;
75 }
76 return r;
77 }
78
79 Rectangle
80 rdiv(Rectangle r, int a)
81 {
82 if (a != 1) {
83 r.min.x /= a;
84 r.min.y /= a;
85 r.max.x /= a;
86 r.max.y /= a;
87 }
88 return r;
89 }
90
91 Rectangle
92 rshift(Rectangle r, int a)
93 {
94 if (a > 0) {
95 r.min.x <<= a;
96 r.min.y <<= a;
97 r.max.x <<= a;
98 r.max.y <<= a;
99 }
100 else if (a < 0) {
101 a = -a;
102 r.min.x >>= a;
103 r.min.y >>= a;
104 r.max.x >>= a;
105 r.max.y >>= a;
106 }
107 return r;
108 }
109
110 int
111 eqpt(Point p, Point q)
112 {
113 return p.x==q.x && p.y==q.y;
114 }
115
116 int
117 eqrect(Rectangle r, Rectangle s)
118 {
119 return r.min.x==s.min.x && r.max.x==s.max.x &&
120 r.min.y==s.min.y && r.max.y==s.max.y;
121 }
122
123 int
124 rectXrect(Rectangle r, Rectangle s)
125 {
126 return r.min.x<s.max.x && s.min.x<r.max.x &&
127 r.min.y<s.max.y && s.min.y<r.max.y;
128 }
129
130 int
131 rectinrect(Rectangle r, Rectangle s)
132 {
133 /* !ptinrect(r.min, s) in line for speed */
134 if(!(r.min.x>=s.min.x && r.min.x<s.max.x &&
135 r.min.y>=s.min.y && r.min.y<s.max.y))
136 return 0;
137 return r.max.x<=s.max.x && r.max.y<=s.max.y;
138 }
139
140 int
141 ptinrect(Point p, Rectangle r)
142 {
143 return p.x>=r.min.x && p.x<r.max.x &&
144 p.y>=r.min.y && p.y<r.max.y;
145 }
146
147 Rectangle
148 rcanon(Rectangle r)
149 {
150 int t;
151 if (r.max.x < r.min.x) {
152 t = r.min.x;
153 r.min.x = r.max.x;
154 r.max.x = t;
155 }
156 if (r.max.y < r.min.y) {
157 t = r.min.y;
158 r.min.y = r.max.y;
159 r.max.y = t;
160 }
161 return r;
162 }
163
164 Rectangle
165 Rect(int x1, int y1, int x2, int y2)
166 {
167 Rectangle r;
168
169 r.min.x = x1;
170 r.min.y = y1;
171 r.max.x = x2;
172 r.max.y = y2;
173 return r;
174 }
175
176 Rectangle
177 Rpt(Point p1, Point p2)
178 {
179 Rectangle r;
180
181 r.min = p1;
182 r.max = p2;
183 return r;
184 }
185
186 Point
187 Pt(int x, int y)
188 {
189 Point p;
190
191 p.x = x;
192 p.y = y;
193 return p;
194 }