Parabola Plot in Rust

Using Rust to create a .png of the parabolic function: y = x2

Rust Code:

use plotters::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a drawing area of 800×600 pixels in a file called “parabola.p…


This content originally appeared on DEV Community and was authored by Ben Santora

Using Rust to create a .png of the parabolic function: y = x2

Parabola_Table

Rust Code:

use plotters::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a drawing area of 800x600 pixels in a file called "parabola.png"
    let root_area = BitMapBackend::new("parabola.png", (800, 600)).into_drawing_area();
    root_area.fill(&WHITE)?;

    // Set up chart builder, with labels on the axes
    let mut chart = ChartBuilder::on(&root_area)
        .caption("Parabola y = x^2", ("sans-serif", 40).into_font()) // Chart title
        .margin(10)
        .x_label_area_size(40)
        .y_label_area_size(40)
        .build_cartesian_2d(-3f32..3f32, 0f32..10f32)?;  // Smaller X and Y range

    // Configure the mesh (grid) and axis labels
    chart.configure_mesh()
        .x_labels(10)
        .y_labels(10)
        .disable_mesh()
        .draw()?;

    // Plot the parabola y = x^2
    chart.draw_series(LineSeries::new(
        (-30..=30).map(|x| {
            let x = x as f32 / 10.0;  // This gives x values between -3.0 and 3.0
            (x, x * x)  // y = x^2
        }),
        &BLUE,
    ))?;

    // Save the chart to the file
    root_area.present()?;

    println!("Plot has been saved to 'parabola.png'");
    Ok(())
}

This will create a .png plot of the function.

I'm using WSL / Linux - Tip: Use 'feh' to be able to view the image without leaving the terminal.

sudo apt install feh

Ben Santora - October 2024


This content originally appeared on DEV Community and was authored by Ben Santora


Print Share Comment Cite Upload Translate Updates
APA

Ben Santora | Sciencx (2024-10-27T11:08:55+00:00) Parabola Plot in Rust. Retrieved from https://www.scien.cx/2024/10/27/parabola-plot-in-rust/

MLA
" » Parabola Plot in Rust." Ben Santora | Sciencx - Sunday October 27, 2024, https://www.scien.cx/2024/10/27/parabola-plot-in-rust/
HARVARD
Ben Santora | Sciencx Sunday October 27, 2024 » Parabola Plot in Rust., viewed ,<https://www.scien.cx/2024/10/27/parabola-plot-in-rust/>
VANCOUVER
Ben Santora | Sciencx - » Parabola Plot in Rust. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/27/parabola-plot-in-rust/
CHICAGO
" » Parabola Plot in Rust." Ben Santora | Sciencx - Accessed . https://www.scien.cx/2024/10/27/parabola-plot-in-rust/
IEEE
" » Parabola Plot in Rust." Ben Santora | Sciencx [Online]. Available: https://www.scien.cx/2024/10/27/parabola-plot-in-rust/. [Accessed: ]
rf:citation
» Parabola Plot in Rust | Ben Santora | Sciencx | https://www.scien.cx/2024/10/27/parabola-plot-in-rust/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.