1
2
3
4
5
6 package fixed
7
8 import (
9 "fmt"
10 )
11
12
13
14
15
16
17 func I(i int) Int26_6 {
18 return Int26_6(i << 6)
19 }
20
21
22
23
24
25
26
27 type Int26_6 int32
28
29
30
31
32 func (x Int26_6) String() string {
33 const shift, mask = 6, 1<<6 - 1
34 if x >= 0 {
35 return fmt.Sprintf("%d:%02d", int32(x>>shift), int32(x&mask))
36 }
37 x = -x
38 if x >= 0 {
39 return fmt.Sprintf("-%d:%02d", int32(x>>shift), int32(x&mask))
40 }
41 return "-33554432:00"
42 }
43
44
45
46
47 func (x Int26_6) Floor() int { return int((x + 0x00) >> 6) }
48
49
50
51
52 func (x Int26_6) Round() int { return int((x + 0x20) >> 6) }
53
54
55
56
57 func (x Int26_6) Ceil() int { return int((x + 0x3f) >> 6) }
58
59
60 func (x Int26_6) Mul(y Int26_6) Int26_6 {
61 return Int26_6((int64(x)*int64(y) + 1<<5) >> 6)
62 }
63
64
65
66
67
68
69
70 type Int52_12 int64
71
72
73
74
75
76 func (x Int52_12) String() string {
77 const shift, mask = 12, 1<<12 - 1
78 if x >= 0 {
79 return fmt.Sprintf("%d:%04d", int64(x>>shift), int64(x&mask))
80 }
81 x = -x
82 if x >= 0 {
83 return fmt.Sprintf("-%d:%04d", int64(x>>shift), int64(x&mask))
84 }
85 return "-2251799813685248:0000"
86 }
87
88
89
90
91 func (x Int52_12) Floor() int { return int((x + 0x000) >> 12) }
92
93
94
95
96 func (x Int52_12) Round() int { return int((x + 0x800) >> 12) }
97
98
99
100
101 func (x Int52_12) Ceil() int { return int((x + 0xfff) >> 12) }
102
103
104 func (x Int52_12) Mul(y Int52_12) Int52_12 {
105 const M, N = 52, 12
106 lo, hi := muli64(int64(x), int64(y))
107 ret := Int52_12(hi<<M | lo>>N)
108 ret += Int52_12((lo >> (N - 1)) & 1)
109 return ret
110 }
111
112
113
114
115
116
117 func muli64(u, v int64) (lo, hi uint64) {
118 const (
119 s = 32
120 mask = 1<<s - 1
121 )
122
123 u1 := uint64(u >> s)
124 u0 := uint64(u & mask)
125 v1 := uint64(v >> s)
126 v0 := uint64(v & mask)
127
128 w0 := u0 * v0
129 t := u1*v0 + w0>>s
130 w1 := t & mask
131 w2 := uint64(int64(t) >> s)
132 w1 += u0 * v1
133 return uint64(u) * uint64(v), u1*v1 + w2 + uint64(int64(w1)>>s)
134 }
135
136
137
138
139 func P(x, y int) Point26_6 {
140 return Point26_6{Int26_6(x << 6), Int26_6(y << 6)}
141 }
142
143
144
145
146 type Point26_6 struct {
147 X, Y Int26_6
148 }
149
150
151 func (p Point26_6) Add(q Point26_6) Point26_6 {
152 return Point26_6{p.X + q.X, p.Y + q.Y}
153 }
154
155
156 func (p Point26_6) Sub(q Point26_6) Point26_6 {
157 return Point26_6{p.X - q.X, p.Y - q.Y}
158 }
159
160
161 func (p Point26_6) Mul(k Int26_6) Point26_6 {
162 return Point26_6{p.X * k / 64, p.Y * k / 64}
163 }
164
165
166 func (p Point26_6) Div(k Int26_6) Point26_6 {
167 return Point26_6{p.X * 64 / k, p.Y * 64 / k}
168 }
169
170
171 func (p Point26_6) In(r Rectangle26_6) bool {
172 return r.Min.X <= p.X && p.X < r.Max.X && r.Min.Y <= p.Y && p.Y < r.Max.Y
173 }
174
175
176
177
178 type Point52_12 struct {
179 X, Y Int52_12
180 }
181
182
183 func (p Point52_12) Add(q Point52_12) Point52_12 {
184 return Point52_12{p.X + q.X, p.Y + q.Y}
185 }
186
187
188 func (p Point52_12) Sub(q Point52_12) Point52_12 {
189 return Point52_12{p.X - q.X, p.Y - q.Y}
190 }
191
192
193 func (p Point52_12) Mul(k Int52_12) Point52_12 {
194 return Point52_12{p.X * k / 4096, p.Y * k / 4096}
195 }
196
197
198 func (p Point52_12) Div(k Int52_12) Point52_12 {
199 return Point52_12{p.X * 4096 / k, p.Y * 4096 / k}
200 }
201
202
203 func (p Point52_12) In(r Rectangle52_12) bool {
204 return r.Min.X <= p.X && p.X < r.Max.X && r.Min.Y <= p.Y && p.Y < r.Max.Y
205 }
206
207
208
209
210
211
212
213
214
215 func R(minX, minY, maxX, maxY int) Rectangle26_6 {
216 if minX > maxX {
217 minX, maxX = maxX, minX
218 }
219 if minY > maxY {
220 minY, maxY = maxY, minY
221 }
222 return Rectangle26_6{
223 Point26_6{
224 Int26_6(minX << 6),
225 Int26_6(minY << 6),
226 },
227 Point26_6{
228 Int26_6(maxX << 6),
229 Int26_6(maxY << 6),
230 },
231 }
232 }
233
234
235
236
237
238
239 type Rectangle26_6 struct {
240 Min, Max Point26_6
241 }
242
243
244 func (r Rectangle26_6) Add(p Point26_6) Rectangle26_6 {
245 return Rectangle26_6{
246 Point26_6{r.Min.X + p.X, r.Min.Y + p.Y},
247 Point26_6{r.Max.X + p.X, r.Max.Y + p.Y},
248 }
249 }
250
251
252 func (r Rectangle26_6) Sub(p Point26_6) Rectangle26_6 {
253 return Rectangle26_6{
254 Point26_6{r.Min.X - p.X, r.Min.Y - p.Y},
255 Point26_6{r.Max.X - p.X, r.Max.Y - p.Y},
256 }
257 }
258
259
260
261 func (r Rectangle26_6) Intersect(s Rectangle26_6) Rectangle26_6 {
262 if r.Min.X < s.Min.X {
263 r.Min.X = s.Min.X
264 }
265 if r.Min.Y < s.Min.Y {
266 r.Min.Y = s.Min.Y
267 }
268 if r.Max.X > s.Max.X {
269 r.Max.X = s.Max.X
270 }
271 if r.Max.Y > s.Max.Y {
272 r.Max.Y = s.Max.Y
273 }
274
275
276
277
278 if r.Empty() {
279 return Rectangle26_6{}
280 }
281 return r
282 }
283
284
285 func (r Rectangle26_6) Union(s Rectangle26_6) Rectangle26_6 {
286 if r.Empty() {
287 return s
288 }
289 if s.Empty() {
290 return r
291 }
292 if r.Min.X > s.Min.X {
293 r.Min.X = s.Min.X
294 }
295 if r.Min.Y > s.Min.Y {
296 r.Min.Y = s.Min.Y
297 }
298 if r.Max.X < s.Max.X {
299 r.Max.X = s.Max.X
300 }
301 if r.Max.Y < s.Max.Y {
302 r.Max.Y = s.Max.Y
303 }
304 return r
305 }
306
307
308 func (r Rectangle26_6) Empty() bool {
309 return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
310 }
311
312
313 func (r Rectangle26_6) In(s Rectangle26_6) bool {
314 if r.Empty() {
315 return true
316 }
317
318
319 return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
320 s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
321 }
322
323
324
325
326
327
328 type Rectangle52_12 struct {
329 Min, Max Point52_12
330 }
331
332
333 func (r Rectangle52_12) Add(p Point52_12) Rectangle52_12 {
334 return Rectangle52_12{
335 Point52_12{r.Min.X + p.X, r.Min.Y + p.Y},
336 Point52_12{r.Max.X + p.X, r.Max.Y + p.Y},
337 }
338 }
339
340
341 func (r Rectangle52_12) Sub(p Point52_12) Rectangle52_12 {
342 return Rectangle52_12{
343 Point52_12{r.Min.X - p.X, r.Min.Y - p.Y},
344 Point52_12{r.Max.X - p.X, r.Max.Y - p.Y},
345 }
346 }
347
348
349
350 func (r Rectangle52_12) Intersect(s Rectangle52_12) Rectangle52_12 {
351 if r.Min.X < s.Min.X {
352 r.Min.X = s.Min.X
353 }
354 if r.Min.Y < s.Min.Y {
355 r.Min.Y = s.Min.Y
356 }
357 if r.Max.X > s.Max.X {
358 r.Max.X = s.Max.X
359 }
360 if r.Max.Y > s.Max.Y {
361 r.Max.Y = s.Max.Y
362 }
363
364
365
366
367 if r.Empty() {
368 return Rectangle52_12{}
369 }
370 return r
371 }
372
373
374 func (r Rectangle52_12) Union(s Rectangle52_12) Rectangle52_12 {
375 if r.Empty() {
376 return s
377 }
378 if s.Empty() {
379 return r
380 }
381 if r.Min.X > s.Min.X {
382 r.Min.X = s.Min.X
383 }
384 if r.Min.Y > s.Min.Y {
385 r.Min.Y = s.Min.Y
386 }
387 if r.Max.X < s.Max.X {
388 r.Max.X = s.Max.X
389 }
390 if r.Max.Y < s.Max.Y {
391 r.Max.Y = s.Max.Y
392 }
393 return r
394 }
395
396
397 func (r Rectangle52_12) Empty() bool {
398 return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
399 }
400
401
402 func (r Rectangle52_12) In(s Rectangle52_12) bool {
403 if r.Empty() {
404 return true
405 }
406
407
408 return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
409 s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
410 }
411
View as plain text