graphics_shapes/intersection/
circle.rs1use crate::intersection::shared::{
2 ellipse_circle, line_circle, polygon_circle, rect_circle, triangle_circle,
3};
4use crate::prelude::*;
5
6impl IntersectsShape for Circle {
7 fn intersects_rect(&self, rect: &Rect) -> bool {
8 rect_circle(rect, self)
9 }
10
11 fn intersects_circle(&self, circle: &Circle) -> bool {
12 let max = circle.radius().max(self.radius());
13 let dist = circle.center().distance(self.center());
14 dist <= max
15 }
16
17 fn intersects_line(&self, line: &Line) -> bool {
18 line_circle(line, self)
19 }
20
21 fn intersects_triangle(&self, triangle: &Triangle) -> bool {
22 triangle_circle(triangle, self)
23 }
24
25 fn intersects_ellipse(&self, ellipse: &Ellipse) -> bool {
26 ellipse_circle(ellipse, self)
27 }
28
29 fn intersects_polygon(&self, polygon: &Polygon) -> bool {
30 polygon_circle(polygon, self)
31 }
32}
33
34#[cfg(test)]
35mod test {
36 use crate::prelude::*;
37
38 #[test]
39 fn lines_all_directions() {
40 let line_tl = Line::new((0, 0), (20, 20));
41 let line_br = Line::new((20, 20), (0, 0));
42 let line_bl = Line::new((0, 20), (20, 0));
43 let line_tr = Line::new((20, 0), (0, 20));
44 let circle = Circle::new((10, 10), 4);
45
46 assert!(circle.intersects_line(&line_tl));
47 assert!(circle.intersects_line(&line_tr));
48 assert!(circle.intersects_line(&line_bl));
49 assert!(circle.intersects_line(&line_br));
50 }
51
52 #[test]
53 fn poly_part() {
54 let line = Line::new((128, 126), (50, 204));
55 let circle = Circle::new((113, 135), 15);
56
57 assert!(circle.intersects_line(&line));
58 }
59}