com_croftsoft_core/math/geom/circle/
mod.rs

1// =============================================================================
2//! - Circle for the geometry module
3//!
4//! # Metadata
5//! - Copyright: © 2023 [`CroftSoft Inc`]
6//! - Author: [`David Wallace Croft`]
7//! - Java created: 2003-03-20
8//! - Java updated: 2003-04-13
9//! - Rust created: 2023-03-18
10//! - Rust updated: 2023-06-01
11//!
12//! # History
13//! - Adapted from the class in the Java-based [`CroftSoft Core Library`]
14//!   - com.croftsoft.core.math.geom.Circle
15//!
16//! [`CroftSoft Core Library`]: https://www.croftsoft.com/library/code/
17//! [`CroftSoft Inc`]: https://www.croftsoft.com/
18//! [`David Wallace Croft`]: https://www.croftsoft.com/people/david/
19// =============================================================================
20
21use super::point_2dd::Point2DD;
22
23#[derive(Clone, Copy, Debug, Default)]
24pub struct Circle {
25  pub center_x: f64,
26  pub center_y: f64,
27  pub radius: f64,
28}
29
30pub trait CircleAccessor {
31  fn contains(
32    &self,
33    x: f64,
34    y: f64,
35  ) -> bool;
36
37  fn get_center_point_2dd(&self) -> Point2DD;
38
39  fn get_center_x(&self) -> f64;
40
41  fn get_center_y(&self) -> f64;
42
43  fn get_radius(&self) -> f64;
44
45  fn intersects_circle(
46    &self,
47    other: &dyn CircleAccessor,
48  ) -> bool;
49
50  // TODO
51  // fn intersects_shape(&self, other_shape: dyn Shape) -> bool;
52}
53
54impl Circle {
55  pub fn new(other: &dyn CircleAccessor) -> Self {
56    Self {
57      center_x: other.get_center_x(),
58      center_y: other.get_center_y(),
59      radius: other.get_radius(),
60    }
61  }
62
63  pub fn set_center(
64    &mut self,
65    center_x: f64,
66    center_y: f64,
67  ) {
68    self.center_x = center_x;
69    self.center_y = center_y;
70  }
71
72  pub fn set_center_from_circle(
73    &mut self,
74    circle: &Circle,
75  ) {
76    self.center_x = circle.center_x;
77    self.center_y = circle.center_y;
78  }
79
80  pub fn set_center_from_point_2dd(
81    &mut self,
82    point_2dd: &Point2DD,
83  ) {
84    self.center_x = point_2dd.x;
85    self.center_y = point_2dd.y;
86  }
87}
88
89impl CircleAccessor for Circle {
90  fn contains(
91    &self,
92    x: f64,
93    y: f64,
94  ) -> bool {
95    let distance =
96      ((self.center_x - x).powi(2) + (self.center_y - y).powi(2)).sqrt();
97    distance <= self.radius
98  }
99
100  fn get_center_point_2dd(&self) -> Point2DD {
101    Point2DD {
102      x: self.center_x,
103      y: self.center_y,
104    }
105  }
106
107  fn get_center_x(&self) -> f64 {
108    self.center_x
109  }
110
111  fn get_center_y(&self) -> f64 {
112    self.center_y
113  }
114
115  fn get_radius(&self) -> f64 {
116    self.radius
117  }
118
119  fn intersects_circle(
120    &self,
121    other: &dyn CircleAccessor,
122  ) -> bool {
123    let distance = ((self.center_x - other.get_center_x()).powi(2)
124      + (self.center_y - other.get_center_y()).powi(2))
125    .sqrt();
126    distance <= self.radius + other.get_radius()
127  }
128}