diff --git a/src/lib.rs b/src/lib.rs index c29b6e5..4437be5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -358,10 +358,7 @@ fn extract_point_pair(pair: &CGA) -> (CGA, CGA) { (a,b) } -fn get_circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection { - if !circle_intersection(circle_1, circle_2) { - return CircleIntersection::None; - } +fn circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection { //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); @@ -370,21 +367,12 @@ fn get_circle_intersection(circle_1: &CGA, circle_2: &CGA) -> CircleIntersection 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 { - let circle_1 = grade_selection(circle_1, 4); - let circle_2 = grade_selection(circle_2, 4); - let meet = circle_1 & circle_2; - let sqr = &meet * &meet; - let mag = magnitude(&sqr); - if mag < ERROR { - true + let sqr = (&inter * &inter)[0]; + if sqr > ERROR { + let (a,b) = extract_point_pair(&inter); + CircleIntersection::TwoPoints(a,b) } else { - false + CircleIntersection::None //Technically could have one intersection, but that's not tested yet } } @@ -586,8 +574,9 @@ impl Game { draw_line(&self.context, &self.boundary); self.ship.draw(&self.context); + let line = 100.*self.ship.fixed_to_world(&self.ship.verts[0]) ^ self.ship.fixed_to_world(&self.ship.verts[1]) ^ CGA::e4(); //Scaled to keep above floating point error for asteroid in &self.asteroids { - let pair = get_circle_intersection(&asteroid.circle, &self.boundary); + let pair = circle_intersection(&asteroid.circle, &self.boundary); match pair { CircleIntersection::TwoPoints(a,b) => { draw_point(&self.context, &a); @@ -596,6 +585,15 @@ impl Game { _ => {} } asteroid.draw(&self.context); + let pair = circle_intersection(&asteroid.circle, &line); + match pair { + CircleIntersection::TwoPoints(a,b) => { + draw_point(&self.context, &a); + draw_point(&self.context, &b); + draw_line(&self.context, &line); + } + _ => {}, + } } }