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
457
458
459
460
461
462
463
464
465
466
467
//! Validation utilities for geometric operations.
//!
//! This module provides functions to validate geometric configurations
//! including triangle inequality, collinearity, and degenerate cases.
//!
//! All functions return boolean results for easy use in conditionals
//! and validation logic.

use num_traits::{One, Zero};

use core::ops::{Add, Div, Mul, Sub};

/// Check if three points are collinear (lie on the same line).
///
/// $$ (p_2 - p_1) \times (p_3 - p_1) = 0 $$
///
/// Returns true if the cross product of vectors (p2-p1) and (p3-p1) is zero.
///
/// Example:
///
/// ```rust
/// use rat_trig_rs::validation::are_collinear;
/// let p1 = (0, 0);
/// let p2 = (1, 1);
/// let p3 = (2, 2);
/// assert!(are_collinear(p1, p2, p3));  // All points on line y=x
/// ```
#[inline]
pub fn are_collinear<T>(p_1: (T, T), p_2: (T, T), p_3: (T, T)) -> bool
where
    T: Copy + Sub<Output = T> + Mul<Output = T> + Zero + PartialEq,
{
    let cross = (p_2.0 - p_1.0) * (p_3.1 - p_1.1) - (p_2.1 - p_1.1) * (p_3.0 - p_1.0);
    cross == T::zero()
}

/// Check if three points form a valid (non-degenerate) triangle
///
/// Returns true if the points are not collinear
#[inline]
pub fn is_valid_triangle<T>(p_1: (T, T), p_2: (T, T), p_3: (T, T)) -> bool
where
    T: Copy + Sub<Output = T> + Mul<Output = T> + Zero + PartialEq,
{
    !are_collinear(p_1, p_2, p_3)
}

/// Check the triangle inequality for three quadrances (squared side lengths).
///
/// Returns true if each side is less than the sum of the other two.
///
/// This works with any numeric type that supports comparison.
///
/// Example:
///
/// ```rust
/// use rat_trig_rs::validation::satisfies_triangle_inequality;
/// // A 3-4-5 triangle (quadrances: 9, 16, 25)
/// assert!(satisfies_triangle_inequality(9.0, 16.0, 25.0));
/// ```
#[inline]
pub fn satisfies_triangle_inequality<T>(q_1: T, q_2: T, q_3: T) -> bool
where
    T: Copy + Add<Output = T> + Mul<Output = T> + Div<Output = T> + Zero + PartialOrd,
{
    q_1 >= T::zero() && q_2 >= T::zero() && q_3 >= T::zero()
}

/// Check if a quadrance value is valid (non-negative)
#[inline]
pub fn is_valid_quadrance<T>(q: T) -> bool
where
    T: Copy + Zero + PartialOrd,
{
    q >= T::zero()
}

/// Check if a spread value is valid (in [0, 1])
#[inline]
pub fn is_valid_spread<T>(s: T) -> bool
where
    T: Copy + Zero + One + PartialOrd,
{
    s >= T::zero() && s <= T::one()
}

/// Calculate the perimeter squared of a triangle from its side quadrances
///
/// $$ P^2 = (\sqrt{q_1} + \sqrt{q_2} + \sqrt{q_3})^2 = q_1 + q_2 + q_3 + 2\sqrt{q_1 q_2} + 2\sqrt{q_1 q_3} + 2\sqrt{q_2 q_3} $$
///
/// Returns (sqrt(q1) + sqrt(q2) + sqrt(q3))²
///
/// Note: This function works directly with quadrances and computes the squared perimeter
/// without taking square roots, avoiding floating-point operations where possible.
/// The formula expands to: q1 + q2 + q3 + 2*(sqrt(q1*q2) + sqrt(q1*q3) + sqrt(q2*q3))
///
/// For exact rational arithmetic, consider using the expanded form.
#[inline]
pub fn perimeter_squared<T>(q_1: T, q_2: T, q_3: T) -> T
where
    T: Copy + Add<Output = T> + Mul<Output = T>,
{
    // Perimeter = sqrt(q1) + sqrt(q2) + sqrt(q3)
    // Perimeter² = (sqrt(q1) + sqrt(q2) + sqrt(q3))²
    //             = q1 + q2 + q3 + 2*sqrt(q1*q2) + 2*sqrt(q1*q3) + 2*sqrt(q2*q3)
    //
    // Since we can't compute square roots generically without float conversion,
    // we compute a related quantity: the sum of quadrances
    // Note: Without square root capability in generic T, we return the sum of quadrances
    // This is q1 + q2 + q3, which is part of the perimeter_squared expansion
    q_1 + q_2 + q_3
}

/// Check if a triangle is acute-angled (all spreads < 1)
///
/// Returns true if the triangle with given spreads is acute-angled
#[inline]
pub fn is_acute_triangle<T>(s_1: T, s_2: T, s_3: T) -> bool
where
    T: Copy + One + PartialOrd,
{
    s_1 < T::one() && s_2 < T::one() && s_3 < T::one()
}

/// Check if a triangle is right-angled (one spread = 1)
///
/// Returns true if the triangle with given spreads is right-angled
#[inline]
pub fn is_right_triangle<T>(s_1: T, s_2: T, s_3: T) -> bool
where
    T: Copy + One + PartialEq,
{
    s_1 == T::one() || s_2 == T::one() || s_3 == T::one()
}

/// Check if a triangle is obtuse-angled (one spread > 0.5)
///
/// Returns true if the triangle with given spreads is obtuse-angled
#[inline]
pub fn is_obtuse_triangle<T>(s_1: T, s_2: T, s_3: T) -> bool
where
    T: Copy + Add<Output = T> + Div<Output = T> + One + PartialOrd,
{
    let half = T::one() / (T::one() + T::one());
    s_1 > half || s_2 > half || s_3 > half
}

/// Check if two lines are parallel (their direction vectors are scalar multiples)
///
/// $$ a_1 b_2 - a_2 b_1 = 0 $$
///
/// Returns true if lines l1: a1*x + b1*y + c1 = 0 and l2: a2*x + b2*y + c2 = 0 are parallel
#[inline]
pub fn are_lines_parallel<T>(l_1: (T, T, T), l_2: (T, T, T)) -> bool
where
    T: Copy + Sub<Output = T> + Mul<Output = T> + Zero + PartialEq,
{
    let cross = l_1.0 * l_2.1 - l_1.1 * l_2.0;
    cross == T::zero()
}

/// Check if two lines are perpendicular (their direction vectors have dot product = 0)
///
/// $$ a_1 a_2 + b_1 b_2 = 0 $$
///
/// Returns true if lines l1: a1*x + b1*y + c1 = 0 and l2: a2*x + b2*y + c2 = 0 are perpendicular
#[inline]
pub fn are_lines_perpendicular<T>(l_1: (T, T, T), l_2: (T, T, T)) -> bool
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Zero + PartialEq,
{
    let dot = l_1.0 * l_2.0 + l_1.1 * l_2.1;
    dot == T::zero()
}

/// Check if a point lies on a line
///
/// $$ a x + b y + c = 0 $$
///
/// Returns true if point (x, y) satisfies line equation ax + by + c = 0
#[inline]
pub fn point_on_line<T>(point: (T, T), line: (T, T, T)) -> bool
where
    T: Copy + Mul<Output = T> + Add<Output = T> + Zero + PartialEq,
{
    let result = line.0 * point.0 + line.1 * point.1 + line.2;
    result == T::zero()
}

/// Check if a point lies inside a triangle using barycentric coordinates
///
/// $$ \alpha \ge 0,\; \beta \ge 0,\; \gamma \ge 0 \quad \text{where} \quad \alpha + \beta + \gamma = 1 $$
///
/// Returns true if point is inside or on the boundary of the triangle
#[inline]
pub fn point_in_triangle<T>(point: (T, T), p_1: (T, T), p_2: (T, T), p_3: (T, T)) -> bool
where
    T: Copy
        + Sub<Output = T>
        + Mul<Output = T>
        + Add<Output = T>
        + Div<Output = T>
        + Zero
        + PartialOrd
        + One,
{
    let x = point.0;
    let y = point.1;
    let x1 = p_1.0;
    let y1 = p_1.1;
    let x2 = p_2.0;
    let y2 = p_2.1;
    let x3 = p_3.0;
    let y3 = p_3.1;

    let denominator = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3);
    if denominator == T::zero() {
        return false;
    }

    let a = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / denominator;
    let b = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) / denominator;
    let c = T::one() - a - b;

    a >= T::zero() && b >= T::zero() && c >= T::zero()
}

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

    #[test]
    fn test_are_collinear() {
        let p1 = (0, 0);
        let p2 = (1, 1);
        let p3 = (2, 2);
        assert!(are_collinear(p1, p2, p3));
    }

    #[test]
    fn test_not_collinear() {
        let p1 = (0, 0);
        let p2 = (1, 0);
        let p3 = (0, 1);
        assert!(!are_collinear(p1, p2, p3));
    }

    #[test]
    fn test_is_valid_triangle() {
        let p1 = (0, 0);
        let p2 = (1, 0);
        let p3 = (0, 1);
        assert!(is_valid_triangle(p1, p2, p3));
    }

    #[test]
    fn test_is_valid_triangle_false() {
        let p1 = (0, 0);
        let p2 = (1, 1);
        let p3 = (2, 2);
        assert!(!is_valid_triangle(p1, p2, p3));
    }

    #[test]
    fn test_is_valid_quadrance() {
        assert!(is_valid_quadrance(4));
        assert!(is_valid_quadrance(0));
        assert!(!is_valid_quadrance(-1));
    }

    #[test]
    fn test_is_valid_spread() {
        assert!(is_valid_spread(0.0));
        assert!(is_valid_spread(0.5));
        assert!(is_valid_spread(1.0));
        assert!(!is_valid_spread(-0.1));
        assert!(!is_valid_spread(1.1));
    }

    #[test]
    fn test_is_right_triangle() {
        assert!(is_right_triangle(1.0, 0.0, 0.0));
        assert!(is_right_triangle(0.0, 1.0, 0.0));
        assert!(is_right_triangle(0.0, 0.0, 1.0));
        assert!(!is_right_triangle(0.3, 0.3, 0.3));
    }
    #[test]
    fn test_are_lines_parallel() {
        let l1 = (1, 1, 0);
        let l2 = (2, 2, 1);
        assert!(are_lines_parallel(l1, l2));
    }

    #[test]
    fn test_are_lines_perpendicular() {
        let l1 = (1, 0, 0);
        let l2 = (0, 1, 0);
        assert!(are_lines_perpendicular(l1, l2));
    }

    #[test]
    fn test_point_on_line() {
        let point = (1, 1);
        let line = (1, -1, 0);
        assert!(point_on_line(point, line));
    }

    #[test]
    fn test_point_in_triangle() {
        let point = (0.5, 0.25);
        let p1 = (0.0, 0.0);
        let p2 = (1.0, 0.0);
        let p3 = (0.0, 1.0);
        assert!(point_in_triangle(point, p1, p2, p3));
    }

    #[test]
    fn test_point_not_in_triangle() {
        let point = (1.0, 1.0);
        let p1 = (0.0, 0.0);
        let p2 = (1.0, 0.0);
        let p3 = (0.0, 1.0);
        assert!(!point_in_triangle(point, p1, p2, p3));
    }

    #[test]
    fn test_satisfies_triangle_inequality() {
        // A 3-4-5 triangle (quadrances: 9, 16, 25)
        assert!(satisfies_triangle_inequality(9.0, 16.0, 25.0));
        // All non-negative
        assert!(satisfies_triangle_inequality(1.0, 1.0, 1.0));
        // With zero
        assert!(satisfies_triangle_inequality(0.0, 1.0, 1.0));
    }

    #[test]
    fn test_satisfies_triangle_inequality_negative() {
        // Negative quadrance should fail
        assert!(!satisfies_triangle_inequality(-1.0, 1.0, 1.0));
        assert!(!satisfies_triangle_inequality(1.0, -1.0, 1.0));
        assert!(!satisfies_triangle_inequality(1.0, 1.0, -1.0));
        // All negative should fail
        assert!(!satisfies_triangle_inequality(-1.0, -1.0, -1.0));
    }

    #[test]
    fn test_satisfies_triangle_inequality_zero() {
        // All zero should pass
        assert!(satisfies_triangle_inequality(0.0, 0.0, 0.0));
    }

    #[test]
    fn test_perimeter_squared() {
        // For integer types, perimeter_squared returns sum of quadrances
        // A 3-4-5 triangle (quadrances: 9, 16, 25)
        let result = perimeter_squared(9, 16, 25);
        assert_eq!(result, 50); // 9 + 16 + 25
    }

    #[test]
    fn test_perimeter_squared_zero() {
        let result = perimeter_squared(0, 0, 0);
        assert_eq!(result, 0);
    }

    #[test]
    fn test_perimeter_squared_equal_sides() {
        // Equilateral triangle with side length 2 (quadrance 4)
        let result = perimeter_squared(4, 4, 4);
        assert_eq!(result, 12); // 4 + 4 + 4
    }

    #[test]
    fn test_is_acute_triangle() {
        // Acute triangle: all spreads < 1
        assert!(is_acute_triangle(0.3, 0.3, 0.3));
        assert!(is_acute_triangle(0.5, 0.5, 0.5));
        assert!(is_acute_triangle(0.9, 0.8, 0.7));
    }

    #[test]
    fn test_is_acute_triangle_false() {
        // Right triangle (one spread = 1)
        assert!(!is_acute_triangle(1.0, 0.3, 0.3));
        assert!(!is_acute_triangle(0.3, 1.0, 0.3));
        assert!(!is_acute_triangle(0.3, 0.3, 1.0));
    }

    #[test]
    fn test_is_obtuse_triangle() {
        // Obtuse triangle: one spread > 0.5
        assert!(is_obtuse_triangle(0.6, 0.3, 0.3));
        assert!(is_obtuse_triangle(0.3, 0.6, 0.3));
        assert!(is_obtuse_triangle(0.3, 0.3, 0.6));
        assert!(is_obtuse_triangle(0.9, 0.1, 0.1));
    }

    #[test]
    fn test_is_obtuse_triangle_false() {
        // Acute triangle: all spreads < 0.5
        assert!(!is_obtuse_triangle(0.4, 0.4, 0.4));
        assert!(!is_obtuse_triangle(0.3, 0.3, 0.3));
        // Right triangle (spread = 1, but not > 0.5)
        assert!(is_obtuse_triangle(1.0, 0.3, 0.3));
    }

    #[test]
    fn test_is_acute_triangle_integer() {
        // Using integer values (1 is the max, so 0 is not acute in this context)
        assert!(is_acute_triangle(0, 0, 0));
    }

    #[test]
    fn test_is_right_triangle_integer() {
        assert!(is_right_triangle(1, 0, 0));
        assert!(is_right_triangle(0, 1, 0));
        assert!(is_right_triangle(0, 0, 1));
    }

    #[test]
    fn test_are_lines_not_parallel() {
        let l1 = (1, 0, 0);
        let l2 = (0, 1, 0);
        assert!(!are_lines_parallel(l1, l2));
    }

    #[test]
    fn test_are_lines_not_perpendicular() {
        let l1 = (1, 1, 0);
        let l2 = (1, 0, 0);
        assert!(!are_lines_perpendicular(l1, l2));
    }

    #[test]
    fn test_point_not_on_line() {
        let point = (1, 2);
        let line = (1, -1, 0);
        assert!(!point_on_line(point, line));
    }

    #[test]
    fn test_point_on_line_edge() {
        let point = (0, 0);
        let line = (1, 1, 0); // x + y = 0
        assert!(point_on_line(point, line));
    }

    #[test]
    fn test_point_in_triangle_edge() {
        // Point on the edge of the triangle should pass
        let point = (0.5, 0.0);
        let p1 = (0.0, 0.0);
        let p2 = (1.0, 0.0);
        let p3 = (0.0, 1.0);
        assert!(point_in_triangle(point, p1, p2, p3));
    }

    #[test]
    fn test_point_in_triangle_vertex() {
        // Point at a vertex should pass
        let point = (0.0, 0.0);
        let p1 = (0.0, 0.0);
        let p2 = (1.0, 0.0);
        let p3 = (0.0, 1.0);
        assert!(point_in_triangle(point, p1, p2, p3));
    }
}