Skip to content

Instantly share code, notes, and snippets.

@georgyangelov
Last active November 11, 2017 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgyangelov/cb7014a42adc832f92c09d2ae77d24a1 to your computer and use it in GitHub Desktop.
Save georgyangelov/cb7014a42adc832f92c09d2ae77d24a1 to your computer and use it in GitHub Desktop.
Rust homework 2 polynomial visualizer

Рисувач на полиноми

https://fmi.rust-lang.bg/tasks/3

Инструкции

  1. Копирайте main.rs в проекта.
  2. Сменете импортите.
  3. Дефинирайте метод pub fn apply(&self, x: f64) -> f64 в Polynomial.
  4. Сложете lodepng към зависимостите в Cargo.toml.
[dependencies]
lodepng = "2.0.5"
  1. Пуснете с cargo run или cargo run --release. Второто пуска оптимизациите и се изпълнява по-бързо.
  2. Ще се появи картинка out.png в папката на проекта.

Може да смените x_range и y_range - това са координатите, които да изобрази на картинката.

extern crate lodepng;
extern crate homework_2; // Change this here
use std::ops::Range;
use std::ptr;
use homework_2::Polynomial; // Change this here
fn main() {
let mut image = Image::new(500, 500);
let polynomial_one = Polynomial::from(vec![1.0, 0.0, 0.0, 0.0]);
let polynomial_two = Polynomial::from(vec![0.0, 1.0, 0.0, 0.0]);
let polynomial_three = Polynomial::from(vec![1.0, 1.0, 0.0, 0.0]);
image.draw(&polynomial_one, Color(255, 0, 0));
image.draw(&polynomial_two, Color(0, 255, 0));
image.draw(&polynomial_three, Color(180, 180, 0));
image.write_png_file("out.png").expect("Could not save file");
}
#[repr(packed)]
#[derive(Debug, Clone, Copy)]
struct Color(u8, u8, u8);
struct Image {
pub width: usize,
pub height: usize,
pub bitmap: Vec<Color>,
}
trait Drawable {
fn draw_on(&self, &mut Image, color: Color);
}
impl Image {
pub fn new(width: usize, height: usize) -> Self {
let mut bitmap = Vec::<Color>::with_capacity(width * height);
unsafe {
bitmap.set_len(width * height);
ptr::write_bytes(bitmap.as_mut_ptr(), 255, width * height);
}
Self { width, height, bitmap }
}
pub fn draw(&mut self, figure: &Drawable, color: Color) {
figure.draw_on(self, color);
}
pub fn set_pixel(&mut self, x: usize, y: usize, color: Color) {
if x >= self.width || y >= self.height {
return;
}
self.bitmap[x + y * self.width] = color;
}
pub fn write_png_file(&self, file_name: &str) -> Result<(), lodepng::Error> {
lodepng::encode24_file(file_name, &self.bitmap, self.width, self.height)
}
}
fn convert_linear_coordinates(x: f64, from: &Range<f64>, to: &Range<f64>) -> f64 {
let x_fraction = (x - from.start) / (from.end - from.start);
x_fraction * (to.end - to.start) + to.start
}
impl Drawable for Polynomial {
fn draw_on(&self, image: &mut Image, color: Color) {
let x_range = Range { start: -1_f64, end: 1_f64 };
let x_pixel_range = Range { start: 0.0, end: image.width as f64 };
let y_range = Range { start: -1_f64, end: 1_f64 };
let y_pixel_range = Range { start: image.height as f64, end: 0.0 };
let step = (x_range.end - x_range.start) / (image.width + image.height) as f64;
let mut x = x_range.start;
while x <= x_range.end {
let y = self.apply(x);
let x_pixel = convert_linear_coordinates(x, &x_range, &x_pixel_range);
let y_pixel = convert_linear_coordinates(y, &y_range, &y_pixel_range);
if x_pixel >= 0.0 && y_pixel >= 0.0 {
image.set_pixel(x_pixel as usize, y_pixel as usize, color);
}
x += step;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment