aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArne Dußin2020-11-21 01:08:06 +0100
committerArne Dußin2020-11-21 01:08:06 +0100
commit8785fda836fe3884bd51444fc55983fe135c6c9d (patch)
tree1c70160cb9e0611b50877bb3fe81caf691871356 /src
parentd7d90e8b3615db38d1af238ac9c8193c283ca156 (diff)
downloadgraf_karto-8785fda836fe3884bd51444fc55983fe135c6c9d.tar.gz
graf_karto-8785fda836fe3884bd51444fc55983fe135c6c9d.zip
Add unit tests for triangle
Diffstat (limited to 'src')
-rw-r--r--src/math/triangle.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/math/triangle.rs b/src/math/triangle.rs
index 53c7560..05e258d 100644
--- a/src/math/triangle.rs
+++ b/src/math/triangle.rs
@@ -30,6 +30,11 @@ impl<T: Scalar + Copy> Triangle<T> {
}
}
+ /// Get the corners immutably
+ pub fn corners(&self) -> &[Vec2<T>; 3] {
+ &self.corners
+ }
+
/// Create a new Triangle from a three-point slice, instead of the three points one after
/// another.
pub fn from_slice(corners: [Vec2<T>; 3]) -> Self
@@ -60,6 +65,21 @@ impl<T: Scalar + Copy> Triangle<T> {
}
}
+/// Convert a three-point-slice into a triangle
+impl<T: Scalar + Copy + ClosedSub + ClosedMul + PartialOrd + Zero> From<[Vec2<T>; 3]>
+ for Triangle<T>
+{
+ fn from(corners: [Vec2<T>; 3]) -> Self {
+ Self::new(corners[0], corners[1], corners[2])
+ }
+}
+/// Convert a triangle into a three-point-slice. The corners are in counterclockwise order.
+impl<T: Scalar + Copy> Into<[Vec2<T>; 3]> for Triangle<T> {
+ fn into(self) -> [Vec2<T>; 3] {
+ self.corners
+ }
+}
+
#[derive(PartialEq, Eq)]
pub(crate) enum TripletOrientation {
Clockwise,
@@ -132,6 +152,43 @@ mod test {
use std::f64::consts::PI;
#[test]
+ fn new() {
+ let a = Vec2::new(0., 0.);
+ let b = Vec2::new(0., 1.);
+ let c = Vec2::new(1., 1.);
+
+ // Create with counterclockwise order.
+ let triangle = Triangle::new(a, b, c);
+ assert_eq!(triangle.corners(), &[a, b, c]);
+
+ // Create with clockwise order.
+ let triangle = Triangle::new(a, c, b);
+ assert_eq!(triangle.corners(), &[a, b, c]);
+
+ // Create with different starting corner
+ let triangle = Triangle::from([b, c, a]);
+ assert_eq!(triangle.corners(), &[b, c, a]);
+
+ // Create with collinear corners
+ let triangle = Triangle::new(c, c, b);
+ assert_eq!(triangle.corners(), &[c, c, b]);
+ }
+
+ #[test]
+ fn contains_point() {
+ let a = Vec2::new(0., 0.);
+ let b = Vec2::new(-1., -1.);
+ let c = Vec2::new(-2., 0.);
+
+ let triangle = Triangle::new(a, b, c);
+
+ assert!(triangle.contains_point(b));
+ assert!(triangle.contains_point(Vec2::new(-0.5, -0.5)));
+ assert!(triangle.contains_point(Vec2::new(-1., -0.5)));
+ assert!(!triangle.contains_point(Vec2::new(-2., -2.)));
+ }
+
+ #[test]
fn triplet_angle() {
assert_eq!(
super::triplet_angle(Vec2::new(0., 0.), Vec2::new(0., -1.), Vec2::new(-1., -1.)),