rat-trig-rs 0.1.3

Rational Trigometry in Rust
Documentation
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/// Module containing const versions of trigonometric functions for specific concrete types.
/// These functions can be used in const contexts with concrete numeric types.
/// Calculate quadrance (square of distance) between two points with i64 coordinates
///
/// $$ Q(p_1, p_2) = (x_2 - x_1)^2 + (y_2 - y_1)^2 $$
#[inline]
pub const fn quadrance_i64(p_1: (i64, i64), p_2: (i64, i64)) -> i64 {
    let dx = p_1.0 - p_2.0;
    let dy = p_1.1 - p_2.1;
    dx * dx + dy * dy
}

/// Calculate Archimedes formula (quadrea) for a triangle with i64 side lengths
///
/// $$ \text{quadrea}(q_1, q_2, q_3) = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2 $$
#[inline]
pub const fn archimedes_i64(q_1: i64, q_2: i64, q_3: i64) -> i64 {
    let temp = q_1 + q_2 - q_3;
    let four = 4;
    four * q_1 * q_2 - temp * temp
}

/// Calculate spread (square of sine) between two vectors with i64 coordinates
///
/// $$ s(v_1, v_2) = 1 - \frac{(v_1 \cdot v_2)^2}{|v_1|^2 |v_2|^2} $$
#[inline]
pub const fn spread_i64(v_1: (i64, i64), v_2: (i64, i64)) -> i64 {
    let dot_product = v_1.0 * v_2.0 + v_1.1 * v_2.1;
    let q_1 = quadrance_i64(v_1, (0, 0));
    let q_2 = quadrance_i64(v_2, (0, 0));
    let q_product = q_1 * q_2;
    if q_product == 0 {
        0
    } else {
        q_product - (dot_product * dot_product) / q_product
    }
}

/// Calculate cross product of two 2D vectors with i64 coordinates
///
/// $$ \text{cross}(v_1, v_2) = x_1 y_2 - y_1 x_2 $$
#[inline]
pub const fn cross_i64(v_1: (i64, i64), v_2: (i64, i64)) -> i64 {
    v_1.0 * v_2.1 - v_1.1 * v_2.0
}

/// Calculate quadrance (square of distance) between two points with i32 coordinates
///
/// $$ Q(p_1, p_2) = (x_2 - x_1)^2 + (y_2 - y_1)^2 $$
#[inline]
pub const fn quadrance_i32(p_1: (i32, i32), p_2: (i32, i32)) -> i32 {
    let dx = p_1.0 - p_2.0;
    let dy = p_1.1 - p_2.1;
    dx * dx + dy * dy
}

/// Calculate Archimedes formula (quadrea) for a triangle with i32 side lengths
///
/// $$ \text{quadrea}(q_1, q_2, q_3) = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2 $$
#[inline]
pub const fn archimedes_i32(q_1: i32, q_2: i32, q_3: i32) -> i32 {
    let temp = q_1 + q_2 - q_3;
    let four: i32 = 4;
    four * q_1 * q_2 - temp * temp
}

/// Calculate spread (square of sine) between two vectors with i32 coordinates
///
/// $$ s(v_1, v_2) = 1 - \frac{(v_1 \cdot v_2)^2}{|v_1|^2 |v_2|^2} $$
#[inline]
pub const fn spread_i32(v_1: (i32, i32), v_2: (i32, i32)) -> i32 {
    let dot_product = v_1.0 * v_2.0 + v_1.1 * v_2.1;
    let q_1 = quadrance_i32(v_1, (0, 0));
    let q_2 = quadrance_i32(v_2, (0, 0));
    let q_product = q_1 * q_2;
    if q_product == 0 {
        0
    } else {
        q_product - dot_product * dot_product / q_product
    }
}

/// Calculate cross product of two 2D vectors with i32 coordinates
///
/// $$ \text{cross}(v_1, v_2) = x_1 y_2 - y_1 x_2 $$
#[inline]
pub const fn cross_i32(v_1: (i32, i32), v_2: (i32, i32)) -> i32 {
    v_1.0 * v_2.1 - v_1.1 * v_2.0
}

/// Calculate quadrance (square of distance) between two points with u32 coordinates
///
/// $$ Q(p_1, p_2) = |x_2 - x_1|^2 + |y_2 - y_1|^2 $$
#[inline]
pub const fn quadrance_u32(p_1: (u32, u32), p_2: (u32, u32)) -> u32 {
    let dx = p_1.0.abs_diff(p_2.0);
    let dy = p_1.1.abs_diff(p_2.1);
    dx * dx + dy * dy
}

/// Calculate Archimedes formula (quadrea) for a triangle with u32 side lengths
///
/// $$ \text{quadrea}(q_1, q_2, q_3) = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2 $$
#[inline]
pub const fn archimedes_u32(q_1: u32, q_2: u32, q_3: u32) -> u32 {
    let temp = q_1 + q_2 - q_3;
    let four: u32 = 4;
    four * q_1 * q_2 - temp * temp
}

/// Calculate spread (square of sine) between two vectors with u32 coordinates
///
/// $$ s(v_1, v_2) = 1 - \frac{(v_1 \cdot v_2)^2}{|v_1|^2 |v_2|^2} $$
#[inline]
pub const fn spread_u32(v_1: (u32, u32), v_2: (u32, u32)) -> u32 {
    let dot_product = v_1.0 * v_2.0 + v_1.1 * v_2.1;
    let q_1 = quadrance_u32(v_1, (0, 0));
    let q_2 = quadrance_u32(v_2, (0, 0));
    let q_product = q_1 * q_2;
    if q_product == 0 {
        0
    } else {
        q_product - dot_product * dot_product / q_product
    }
}

/// Calculate cross product of two 2D vectors with u32 coordinates
///
/// $$ \text{cross}(v_1, v_2) = |x_1 y_2 - y_1 x_2| $$
///
/// Note: Uses wrapping subtraction since cross product can be negative.
/// For unsigned types, the result is the absolute value of the cross product.
#[inline]
pub const fn cross_u32(v_1: (u32, u32), v_2: (u32, u32)) -> u32 {
    let a = v_1.0 * v_2.1;
    let b = v_1.1 * v_2.0;
    a.abs_diff(b)
}

/// Calculate quadrance (square of distance) between two points with u64 coordinates
///
/// $$ Q(p_1, p_2) = |x_2 - x_1|^2 + |y_2 - y_1|^2 $$
#[inline]
pub const fn quadrance_u64(p_1: (u64, u64), p_2: (u64, u64)) -> u64 {
    let dx = p_1.0.abs_diff(p_2.0);
    let dy = p_1.1.abs_diff(p_2.1);
    dx * dx + dy * dy
}

/// Calculate Archimedes formula (quadrea) for a triangle with u64 side lengths
///
/// $$ \text{quadrea}(q_1, q_2, q_3) = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2 $$
#[inline]
pub const fn archimedes_u64(q_1: u64, q_2: u64, q_3: u64) -> u64 {
    let temp = q_1 + q_2 - q_3;
    let four: u64 = 4;
    four * q_1 * q_2 - temp * temp
}

/// Calculate spread (square of sine) between two vectors with u64 coordinates
///
/// $$ s(v_1, v_2) = 1 - \frac{(v_1 \cdot v_2)^2}{|v_1|^2 |v_2|^2} $$
#[inline]
pub const fn spread_u64(v_1: (u64, u64), v_2: (u64, u64)) -> u64 {
    let dot_product = v_1.0 * v_2.0 + v_1.1 * v_2.1;
    let q_1 = quadrance_u64(v_1, (0, 0));
    let q_2 = quadrance_u64(v_2, (0, 0));
    let q_product = q_1 * q_2;
    if q_product == 0 {
        0
    } else {
        q_product - dot_product * dot_product / q_product
    }
}

/// Calculate cross product of two 2D vectors with u64 coordinates
///
/// $$ \text{cross}(v_1, v_2) = |x_1 y_2 - y_1 x_2| $$
///
/// Note: Uses wrapping subtraction since cross product can be negative.
/// For unsigned types, the result is the absolute value of the cross product.
#[inline]
pub const fn cross_u64(v_1: (u64, u64), v_2: (u64, u64)) -> u64 {
    let a = v_1.0 * v_2.1;
    let b = v_1.1 * v_2.0;
    a.abs_diff(b)
}

/// Calculate quadrance (square of distance) between two points with i128 coordinates
///
/// $$ Q(p_1, p_2) = (x_2 - x_1)^2 + (y_2 - y_1)^2 $$
#[inline]
pub const fn quadrance_i128(p_1: (i128, i128), p_2: (i128, i128)) -> i128 {
    let dx = p_1.0 - p_2.0;
    let dy = p_1.1 - p_2.1;
    dx * dx + dy * dy
}

/// Calculate Archimedes formula (quadrea) for a triangle with i128 side lengths
///
/// $$ \text{quadrea}(q_1, q_2, q_3) = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2 $$
#[inline]
pub const fn archimedes_i128(q_1: i128, q_2: i128, q_3: i128) -> i128 {
    let temp = q_1 + q_2 - q_3;
    let four: i128 = 4;
    four * q_1 * q_2 - temp * temp
}

/// Calculate spread (square of sine) between two vectors with i128 coordinates
///
/// $$ s(v_1, v_2) = 1 - \frac{(v_1 \cdot v_2)^2}{|v_1|^2 |v_2|^2} $$
#[inline]
pub const fn spread_i128(v_1: (i128, i128), v_2: (i128, i128)) -> i128 {
    let dot_product = v_1.0 * v_2.0 + v_1.1 * v_2.1;
    let q_1 = quadrance_i128(v_1, (0, 0));
    let q_2 = quadrance_i128(v_2, (0, 0));
    let q_product = q_1 * q_2;
    if q_product == 0 {
        0
    } else {
        q_product - dot_product * dot_product / q_product
    }
}

/// Calculate cross product of two 2D vectors with i128 coordinates
///
/// $$ \text{cross}(v_1, v_2) = x_1 y_2 - y_1 x_2 $$
#[inline]
pub const fn cross_i128(v_1: (i128, i128), v_2: (i128, i128)) -> i128 {
    v_1.0 * v_2.1 - v_1.1 * v_2.0
}

/// Calculate quadrance (square of distance) between two points with u128 coordinates
///
/// $$ Q(p_1, p_2) = |x_2 - x_1|^2 + |y_2 - y_1|^2 $$
#[inline]
pub const fn quadrance_u128(p_1: (u128, u128), p_2: (u128, u128)) -> u128 {
    let dx = p_1.0.abs_diff(p_2.0);
    let dy = p_1.1.abs_diff(p_2.1);
    dx * dx + dy * dy
}

/// Calculate Archimedes formula (quadrea) for a triangle with u128 side lengths
///
/// $$ \text{quadrea}(q_1, q_2, q_3) = 4 q_1 q_2 - (q_1 + q_2 - q_3)^2 $$
#[inline]
pub const fn archimedes_u128(q_1: u128, q_2: u128, q_3: u128) -> u128 {
    let temp = q_1 + q_2 - q_3;
    let four: u128 = 4;
    four * q_1 * q_2 - temp * temp
}

/// Calculate spread (square of sine) between two vectors with u128 coordinates
///
/// $$ s(v_1, v_2) = 1 - \frac{(v_1 \cdot v_2)^2}{|v_1|^2 |v_2|^2} $$
#[inline]
pub const fn spread_u128(v_1: (u128, u128), v_2: (u128, u128)) -> u128 {
    let dot_product = v_1.0 * v_2.0 + v_1.1 * v_2.1;
    let q_1 = quadrance_u128(v_1, (0, 0));
    let q_2 = quadrance_u128(v_2, (0, 0));
    let q_product = q_1 * q_2;
    if q_product == 0 {
        0
    } else {
        q_product - dot_product * dot_product / q_product
    }
}

/// Calculate cross product of two 2D vectors with u128 coordinates
///
/// $$ \text{cross}(v_1, v_2) = |x_1 y_2 - y_1 x_2| $$
///
/// Note: Uses wrapping subtraction since cross product can be negative.
/// For unsigned types, the result is the absolute value of the cross product.
#[inline]
pub const fn cross_u128(v_1: (u128, u128), v_2: (u128, u128)) -> u128 {
    let a = v_1.0 * v_2.1;
    let b = v_1.1 * v_2.0;
    a.abs_diff(b)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quadrance_i64() {
        let p1 = (1, 1);
        let p2 = (4, 5);
        assert_eq!(quadrance_i64(p1, p2), 25);
    }

    #[test]
    fn test_archimedes_i64() {
        assert_eq!(archimedes_i64(1, 2, 3), 8);
    }

    #[test]
    fn test_spread_i64() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        let result = spread_i64(v1, v2);
        // For vectors (1,1) and (1,0):
        // dot = 1*1 + 1*0 = 1
        // q1 = 1^2 + 1^2 = 2
        // q2 = 1^2 + 0^2 = 1
        // spread = q1*q2 - dot^2/q1*q2 = 2 - 1/2 = 2 - 0 = 2 (integer division)
        // This is the numerator of the spread formula with integer division
        assert_eq!(result, 2);
    }

    #[test]
    fn test_cross_i64() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        assert_eq!(cross_i64(v1, v2), -1);
    }

    #[test]
    fn test_quadrance_i32() {
        let p1 = (1, 1);
        let p2 = (4, 5);
        assert_eq!(quadrance_i32(p1, p2), 25);
    }

    #[test]
    fn test_archimedes_i32() {
        assert_eq!(archimedes_i32(1, 2, 3), 8);
    }

    #[test]
    fn test_spread_i32() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        let result = spread_i32(v1, v2);
        assert_eq!(result, 2);
    }

    #[test]
    fn test_cross_i32() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        assert_eq!(cross_i32(v1, v2), -1);
    }

    #[test]
    fn test_quadrance_u32() {
        let p1 = (1, 1);
        let p2 = (4, 5);
        assert_eq!(quadrance_u32(p1, p2), 25);
    }

    #[test]
    fn test_archimedes_u32() {
        assert_eq!(archimedes_u32(1, 2, 3), 8);
    }

    #[test]
    fn test_spread_u32() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        let result = spread_u32(v1, v2);
        assert_eq!(result, 2);
    }

    #[test]
    fn test_cross_u32() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        // For unsigned types, cross returns absolute value
        assert_eq!(cross_u32(v1, v2), 1);
    }

    #[test]
    fn test_quadrance_u64() {
        let p1 = (1, 1);
        let p2 = (4, 5);
        assert_eq!(quadrance_u64(p1, p2), 25);
    }

    #[test]
    fn test_archimedes_u64() {
        assert_eq!(archimedes_u64(1, 2, 3), 8);
    }

    #[test]
    fn test_spread_u64() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        let result = spread_u64(v1, v2);
        assert_eq!(result, 2);
    }

    #[test]
    fn test_cross_u64() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        // For unsigned types, cross returns absolute value
        assert_eq!(cross_u64(v1, v2), 1);
    }

    #[test]
    fn test_quadrance_i128() {
        let p1 = (1, 1);
        let p2 = (4, 5);
        assert_eq!(quadrance_i128(p1, p2), 25);
    }

    #[test]
    fn test_archimedes_i128() {
        assert_eq!(archimedes_i128(1, 2, 3), 8);
    }

    #[test]
    fn test_spread_i128() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        let result = spread_i128(v1, v2);
        assert_eq!(result, 2);
    }

    #[test]
    fn test_cross_i128() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        assert_eq!(cross_i128(v1, v2), -1);
    }

    #[test]
    fn test_quadrance_u128() {
        let p1 = (1, 1);
        let p2 = (4, 5);
        assert_eq!(quadrance_u128(p1, p2), 25);
    }

    #[test]
    fn test_archimedes_u128() {
        assert_eq!(archimedes_u128(1, 2, 3), 8);
    }

    #[test]
    fn test_spread_u128() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        let result = spread_u128(v1, v2);
        assert_eq!(result, 2);
    }

    #[test]
    fn test_cross_u128() {
        let v1 = (1, 1);
        let v2 = (1, 0);
        // For unsigned types, cross returns absolute value
        assert_eq!(cross_u128(v1, v2), 1);
    }
}