User:Inthar/Code: Difference between revisions
mNo edit summary |
|||
| Line 1: | Line 1: | ||
== planegeometry.rs == | == planegeometry.rs == | ||
<syntaxhighlight lang="rs"> | <syntaxhighlight lang="rs">// plane_geometry.rs | ||
// plane_geometry.rs | |||
// v0.0 by inthar | // v0.0 by inthar | ||
// Geometry of points and lines in the plane, implemented with rational numbers. The implementation is janky, though. | // Geometry of points and lines in the plane, implemented with rational numbers. The implementation is janky, though. | ||
| Line 256: | Line 255: | ||
// Use the slope-intercept formula including the case where the slope is infinite. | // Use the slope-intercept formula including the case where the slope is infinite. | ||
pub fn are_collinear(p1: Self, p2: Self, p3: Self) -> bool { | pub fn are_collinear(p1: Self, p2: Self, p3: Self) -> bool { | ||
(p1.y - p3.y)*(p1.x - p2.x) == (p1.y-p2.y)*(p1.x - p3.x) | (p1.y - p3.y)*(p1.x - p2.x) == (p1.y - p2.y)*(p1.x - p3.x) | ||
} | } | ||
} | } | ||
| Line 608: | Line 607: | ||
j = j+1; | j = j+1; | ||
} | } | ||
} | } | ||
third_shapes | third_shapes | ||
| Line 972: | Line 969: | ||
Point::new(r64::new(1,2),r64::new(1,1)), | Point::new(r64::new(1,2),r64::new(1,1)), | ||
Point::new(r64::new(0,1),r64::new(1,1)) | Point::new(r64::new(0,1),r64::new(1,1)) | ||
])), "`left_polygon` is not | ])), "`left_polygon` is not correct"); | ||
assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![Point::new(r64::new(1,2),r64::new(0,1)), | assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![Point::new(r64::new(1,2),r64::new(0,1)), | ||
Point::new(r64::new(1,1),r64::new(0,1)), | Point::new(r64::new(1,1),r64::new(0,1)), | ||
Point::new(r64::new(1,1),r64::new(1,1)), | Point::new(r64::new(1,1),r64::new(1,1)), | ||
Point::new(r64::new(1,2),r64::new(1,1)) | Point::new(r64::new(1,2),r64::new(1,1)) | ||
])), "`right_polygon` is not | ])), "`right_polygon` is not correct"); | ||
} | } | ||
| Line 991: | Line 988: | ||
let (left_polygon, right_polygon) = square.subdivide(line); | let (left_polygon, right_polygon) = square.subdivide(line); | ||
assert_eq!(left_polygon, None, "`left_polygon` is not | assert_eq!(left_polygon, None, "`left_polygon` is not correct"); | ||
assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`right_polygon` is not | assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`right_polygon` is not correct"); | ||
let line2 = Line::from_slope_and_point(Slope::integer(1), Point::new(r64::new(1,1), r64::new(0,1))); | let line2 = Line::from_slope_and_point(Slope::integer(1), Point::new(r64::new(1,1), r64::new(0,1))); | ||
let (left_polygon, right_polygon) = square.subdivide(line2); | let (left_polygon, right_polygon) = square.subdivide(line2); | ||
assert_eq!(left_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`left_polygon` is not | assert_eq!(left_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`left_polygon` is not correct"); | ||
assert_eq!(right_polygon, None, "`right_polygon` is not | assert_eq!(right_polygon, None, "`right_polygon` is not correct"); | ||
} | } | ||
| Line 1,012: | Line 1,009: | ||
let (left_polygon, right_polygon) = square.subdivide(line); | let (left_polygon, right_polygon) = square.subdivide(line); | ||
assert_eq!(left_polygon, None, "`left_polygon` is not | assert_eq!(left_polygon, None, "`left_polygon` is not correct"); | ||
assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`right_polygon` is not | assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`right_polygon` is not correct"); | ||
let line = Line::from_slope_and_point(Slope::integer(0), Point::new(r64::new(1,1), r64::new(0,1))); | let line = Line::from_slope_and_point(Slope::integer(0), Point::new(r64::new(1,1), r64::new(0,1))); | ||
let (left_polygon, right_polygon) = square.subdivide(line); | let (left_polygon, right_polygon) = square.subdivide(line); | ||
assert_eq!(left_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`left_polygon` is not | assert_eq!(left_polygon, Some(ConvexPolygon::raw(vec![p3, p1, p4, p2])), "`left_polygon` is not correct"); | ||
assert_eq!(right_polygon, None, "`right_polygon` is not | assert_eq!(right_polygon, None, "`right_polygon` is not correct"); | ||
} | } | ||
| Line 1,036: | Line 1,033: | ||
Point::new(r64::new(1,2),r64::new(1,1)), | Point::new(r64::new(1,2),r64::new(1,1)), | ||
Point::new(r64::new(0,1),r64::new(1,1)), | Point::new(r64::new(0,1),r64::new(1,1)), | ||
])), "`left_polygon is not | ])), "`left_polygon is not correct"); | ||
assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![Point::new(r64::new(0,1),r64::new(0,1)), | assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![Point::new(r64::new(0,1),r64::new(0,1)), | ||
Point::new(r64::new(1,1),r64::new(0,1)), | Point::new(r64::new(1,1),r64::new(0,1)), | ||
Point::new(r64::new(1,1),r64::new(1,1)), | Point::new(r64::new(1,1),r64::new(1,1)), | ||
Point::new(r64::new(1,2),r64::new(1,1)) | Point::new(r64::new(1,2),r64::new(1,1)) | ||
])), "`right_polygon` is not | ])), "`right_polygon` is not correct"); | ||
} | } | ||
| Line 1,058: | Line 1,055: | ||
Point::new(r64::new(1,1),r64::new(1,1)), | Point::new(r64::new(1,1),r64::new(1,1)), | ||
Point::new(r64::new(0,1),r64::new(1,1)) | Point::new(r64::new(0,1),r64::new(1,1)) | ||
])), "`left_polygon` is not | ])), "`left_polygon` is not correct"); | ||
assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![Point::new(r64::new(0,1),r64::new(0,1)), | assert_eq!(right_polygon, Some(ConvexPolygon::raw(vec![Point::new(r64::new(0,1),r64::new(0,1)), | ||
Point::new(r64::new(1,1),r64::new(0,1)), | Point::new(r64::new(1,1),r64::new(0,1)), | ||
Point::new(r64::new(1,1),r64::new(1,1)) | Point::new(r64::new(1,1),r64::new(1,1)) | ||
])), "`right_polygon` is not | ])), "`right_polygon` is not correct"); | ||
} | } | ||