Search code examples
user-interfacematrixrustlinear-algebraegui

Egui display an editable 3x3 matrix?


I am trying to show the 9 cells of a 3x3 matrix using egui. I want a 3x3 grid that matches the entries in the matrix. So far I have this:

enter image description here

Where the two numbers are the (0,0) and (0, 1) entries in the matrix, but I want them side by side.

I got them with this snippet:

                let (_pos, mut mat) = dual_to_components(&mesh.verts.get_mut()[0].data);
                if ui.add(ne_gui::DragValue::new(&mut mat[(0,0)]).speed(0.01)).changed()
                {
                    update_covariance(&mut mesh.verts.get_mut()[0].data.position, &mat);
                }

                if ui.add(ne_gui::DragValue::new(&mut mat[(0,1)]).speed(0.01)).changed()
                {
                    mat[(1,0)] = mat[(0,1)];
                    update_covariance(&mut mesh.verts.get_mut()[0].data.position, &mat);
                }
            });

How can I get a properly formatted 3x3?


Solution

  • Using egui::Ui::horizontal looks like a reasonable option here?

    for row in 0..3 {
        ui.horizontal(|ui| {
            for col in 0..3 {
                ui.add(egui::DragValue::new(&mut mat[3 * row + col]).speed(0.01));
            }
        });
    }
    

    This code snippet gives something like this:

    enter image description here