rat_trig_rs/trigonom.rs
1use core::convert::From;
2/// Rational Trigonometry is a new approach to classical trigonometry, developed by Norman
3/// Wildberger, that aims to simplify and clarify the subject by using only rational numbers
4/// and operations, rather than irrational numbers and limits.
5///
6/// In traditional trigonometry, concepts such as the sine, cosine, and tangent of an angle
7/// are typically defined using circles and the unit circle in particular. These definitions
8/// involve irrational numbers and limits, which can make the subject more difficult to
9/// understand and work with.
10///
11/// In rational trigonometry, Wildberger replaces these circular definitions with ones based
12/// on lines and line segments, which allows for a more straightforward and intuitive approach.
13/// The fundamental concepts in rational trigonometry are the "quadaverage" and the "dilated
14/// directed angle," which are defined in terms of lines and line segments, rather than circles.
15///
16/// Rational trigonometry has been gaining popularity in recent years, as it provides a useful
17/// alternative to traditional trigonometry for certain applications, such as computer graphics,
18/// robotics, and physics. It can also be a helpful tool for students who struggle with the
19/// irrational numbers and limits used in traditional trigonometry.
20///
21/// In summary, Rational Trigonometry is a new approach to classical trigonometry that uses
22/// rational numbers and operations, rather than irrational numbers and limits, making it a more
23/// straightforward and intuitive subject to understand and work with.
24use core::ops::{Add, Mul, Sub};
25
26/// The function `archimedes` calculates the area of a triangle using Archimedes' formula with the
27/// lengths of the three sides provided as `Fraction<i64>` values.
28///
29/// Arguments:
30///
31/// * `q_1`: Represents the length of the first side of the triangle.
32/// * `q_2`: The parameters `q_1`, `q_2`, and `q_3` represent the lengths of the sides of a triangle. In
33/// the context of Archimedes' formula for the area of a triangle, `q_1`, `q_2`, and `q_3`
34/// * `q_3`: The parameter `q_3` represents the length of the third side of the triangle.
35///
36/// Returns:
37///
38/// The function `archimedes` returns the area of a triangle computed using Archimedes' formula, given
39/// the lengths of the 3 sides.
40///
41/// Example:
42///
43/// ```rust
44/// use num_rational::Rational32;
45/// use rat_trig_rs::trigonom::archimedes;
46/// let q_1 = Rational32::new(1, 2);
47/// let q_2 = Rational32::new(1, 4);
48/// let q_3 = Rational32::new(1, 6);
49/// let quadrea = archimedes(&q_1, &q_2, &q_3);
50/// assert_eq!(quadrea, Rational32::new(23, 144));
51/// ```
52#[inline]
53pub fn archimedes<T>(q_1: &T, q_2: &T, q_3: &T) -> T
54where
55 T: std::marker::Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + From<i32>,
56{
57 let temp = *q_1 + *q_2 - *q_3;
58 T::from(4) * *q_1 * *q_2 - temp * temp
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use num_rational::Ratio;
65 // use fractions::Fraction;
66
67 #[test]
68 fn test_archimedes2() {
69 let q_1: i64 = 1;
70 let q_2: i64 = 2;
71 let q_3: i64 = 3;
72 assert_eq!(archimedes(&q_1, &q_2, &q_3), 8);
73 }
74
75 #[test]
76 fn test_archimedes3() {
77 let q_1 = 1.0;
78 let q_2 = 2.0;
79 let q_3 = 3.0;
80 assert_eq!(archimedes(&q_1, &q_2, &q_3), 8.0);
81 }
82
83 #[test]
84 fn test_archimedes() {
85 let q_1 = Ratio::<i32>::new(1, 2);
86 let q_2 = Ratio::<i32>::new(1, 4);
87 let q_3 = Ratio::<i32>::new(1, 6);
88 assert_eq!(archimedes(&q_1, &q_2, &q_3), Ratio::<i32>::new(23, 144));
89 }
90
91 // #[test]
92 // fn test_archimedes4() {
93 // let q_1 = Fraction::<i64>::new(1, 2);
94 // let q_2 = Fraction::<i64>::new(1, 4);
95 // let q_3 = Fraction::<i64>::new(1, 6);
96 // assert_eq!(archimedes(&q_1, &q_2, &q_3), Fraction::<i64>::new(23, 144));
97 // }
98}