qtruss 0.10.1

A simple finite-element solver for trusses.
Documentation
//! Describes a plottable truss element.

/// Color of maximum tensile stress.
const TENSION: (f32, f32, f32) = (212., 0., 0.);

/// Color of maximum compressive stress.
const COMPRESSION: (f32, f32, f32) = (24., 106., 237.);

/// Color of zero-load element.
const ZEROLOAD: (f32, f32, f32) = (194., 194., 194.);

#[derive(Clone, Copy, Debug)]
/// A plottable truss element.
pub struct PlottableElement {
    pub x1: f32,
    pub y1: f32,
    pub x2: f32,
    pub y2: f32,
    pub stress: f32,
    pub thickness: f32,
}

impl PlottableElement {
    /// Constructs a "zero" element.
    pub fn zero() -> Self {
        Self {
            x1: 0.0,
            y1: 0.0,
            x2: 0.0,
            y2: 0.0,
            stress: 0.0,
            thickness: 0.0,
        }
    }

    /// Returns an RGBA color suited for the stress applied.
    pub fn rgba(&self) -> (u8, u8, u8, u8) {
        if self.stress >= 0.0 {
            let r = (TENSION.0 - ZEROLOAD.0) * self.stress + ZEROLOAD.0;
            let g = (TENSION.1 - ZEROLOAD.1) * self.stress + ZEROLOAD.1;
            let b = (TENSION.2 - ZEROLOAD.2) * self.stress + ZEROLOAD.2;
            let a = 255.0 - 255.0 * (-50.0 * self.stress.abs()).exp();

            (r as u8, g as u8, b as u8, a as u8)
        } else {
            let r = (COMPRESSION.0 - ZEROLOAD.0) * -self.stress + ZEROLOAD.0;
            let g = (COMPRESSION.1 - ZEROLOAD.1) * -self.stress + ZEROLOAD.1;
            let b = (COMPRESSION.2 - ZEROLOAD.2) * -self.stress + ZEROLOAD.2;
            let a = 255.0 - 255.0 * (-50.0 * self.stress.abs()).exp();

            (r as u8, g as u8, b as u8, a as u8)
        }
    }
}