From d7c4dea2cb96742ac71d6ffd20f2b82ad2007ffc Mon Sep 17 00:00:00 2001 From: Lily Iliana Luna Ylva Anderson Grigaitis Date: Wed, 27 Aug 2025 17:16:17 -0500 Subject: [PATCH] Code to extract the point pair from a bivector and get the intersection points of two circles. --- src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 64f103e..0754f87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -348,20 +348,42 @@ fn _get_hyperbolic_translation(vec: &CGA) -> CGA { t } -enum _CircleIntersection { +enum CircleIntersection { TwoPoints(CGA, CGA), - OnePoint(CGA), + //OnePoint(CGA), None, } -/*fn get_circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection { - if !circle_intersection(;//circle_1, circle_2) { +fn extract_point_pair(pair: &CGA) -> (CGA, CGA) { + let n_vec = CGA::e4() + CGA::e5(); + let pair = grade_selection(&pair, 2); //Examine only the bivector part + let f = pair.normalized(); + let p = CGA::new(0.5, SCALAR) + 0.5*&f; //Projection Operator + let p_rev = CGA::new(0.5, SCALAR) - 0.5*&f; //Projection Operator + let diff = &pair | &n_vec; //point A - point B (or a scalar multiple) + let a = -1. * &p_rev * &diff; + let b = 1. * &p * &diff; + (a,b) +} + +fn get_circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection { + if !circle_intersection(circle_1, circle_2) { return CircleIntersection::None; } - let n = CGA::e4() + CGA::e5(); -}*/ + //Assumes circles intersect in two places. + let circle_1 = grade_selection(circle_1, 3); //Explicitly check only trivector part + let circle_2 = grade_selection(circle_2, 3); + let i4 = CGA::e1245(); //xy plane in 3D CGA. + let ab = &circle_1 * &circle_2; + let ba = &circle_2 * &circle_1; + let comm = &ab - &ba; //Twice the commutator of circle_1 and circle_2. Unnormalized, but that isn't important. + let inter = &i4 * &comm; //The bivector of the pair of intersection points. + let (a,b) = extract_point_pair(&inter); + CircleIntersection::TwoPoints(a,b) +} -fn _circle_intersection(circle_1: &CGA, circle_2: &CGA) -> bool { + +fn circle_intersection(circle_1: &CGA, circle_2: &CGA) -> bool { let circle_1 = grade_selection(circle_1, 4); let circle_2 = grade_selection(circle_2, 4); let meet = circle_1 & circle_2; @@ -472,16 +494,16 @@ fn circle_from_coords(x: f64, y: f64, r: f64) -> CGA { let a = point_to_cga(a_x, a_y); let b = point_to_cga(b_x, b_y); let c = point_to_cga(c_x, c_y); - let circle = a^b^c^CGA::e3(); + let circle = a^b^c; circle } impl Asteroid { fn new() -> Asteroid { - let v = 0.1; + let v = 0.; let (v_x, v_y) = (v * 1.0_f64.cos(), v*1.0_f64.sin()); let vel = v_x*CGA::e15() + v_y*CGA::e25(); - let circle = circle_from_coords(0., 0., 0.); + let circle = circle_from_coords(0.9, 0., 0.1); Asteroid { circle, vel } } @@ -539,7 +561,7 @@ impl Game { orientation: CGA::new(1., SCALAR), }; - let boundary = circle_from_coords(0., 0., 0.99); + let boundary = circle_from_coords(0., 0., 0.9); Game { context: context, ship, @@ -561,6 +583,14 @@ impl Game { draw_line(&self.context, &self.boundary); self.ship.draw(&self.context); for asteroid in &self.asteroids { + let pair = get_circle_intersection(&asteroid.circle, &self.boundary); + match pair { + CircleIntersection::TwoPoints(a,b) => { + draw_point(&self.context, &a); + draw_point(&self.context, &b); + }, + _ => {} + } asteroid.draw(&self.context); } }