rustlinear-algebranalgebra

How to average two points in nalgebra?


I have a triangle ABC, and I want to generate triangle DEF.

Triangle DEF is created using the centre of all edges of ABC. Nalgebra doesn't seem to allow me to do add points together, only vectors.

use nalgebra::Point2;

fn get_def(a: Point2<f32>, b: Point2<f32>, c: Point2<f32>) -> [Point2<f32>; 3] {
    let d = (a + b) / 2.0; // error
    let e = (b + c) / 2.0; // error
    let f = (c + a) / 2.0; // error

    [d, e, f]
}

Triangles ABC and DEF.


Solution

  • Nalgebra has a function specifically for this, nalgebra::center.

    use nalgebra::{Point2, center};
    
    fn get_def(a: Point2<f32>, b: Point2<f32>, c: Point2<f32>) -> [Point2<f32>; 3] {
        let d = center(&a, &b);
        let e = center(&b, &c);
        let f = center(&c, &a;
    
        [d, e, f]
    }