competitive/geometry/
circle.rs1use super::{Approx, Ccwable, Complex, Float};
2
3#[derive(Clone, Debug, PartialEq)]
4pub struct Circle<T> {
5 c: Complex<T>,
6 r: T,
7}
8impl<T> Circle<T>
9where
10 T: Ccwable + Float,
11{
12 pub fn new(c: Complex<T>, r: T) -> Self {
13 Circle { c, r }
14 }
15 pub fn cross_circle(&self, other: &Self) -> Option<(Complex<T>, Complex<T>)> {
16 let d = (self.c - other.c).abs();
17 let rc = (d * d + self.r * self.r - other.r * other.r) / (d + d);
18 let rs2 = self.r * self.r - rc * rc;
19 if Approx(rs2) < Approx(T::zero()) {
20 return None;
21 }
22 let rs = rs2.abs().sqrt();
23 let diff = (other.c - self.c) / d;
24 Some((
25 self.c + diff * Complex::new(rc, rs),
26 self.c + diff * Complex::new(rc, -rs),
27 ))
28 }
29 pub fn contains_point(&self, p: Complex<T>) -> bool {
30 Approx((self.c - p).abs()) <= Approx(self.r)
31 }
32}