First attempt at converting drawing into matrix
This commit is contained in:
8
src/lib.rs
Normal file
8
src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub mod process_point;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Point {
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
pub new_line: bool,
|
||||
}
|
||||
20
src/main.rs
20
src/main.rs
@@ -3,21 +3,17 @@ use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt, Ge
|
||||
use gtk::cairo::{Context, Operator};
|
||||
use relm4::drawing::DrawHandler;
|
||||
|
||||
use hwocr::process_point::to_matrix;
|
||||
use hwocr::Point;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum AppInput {
|
||||
Increment,
|
||||
Input,
|
||||
NewLine((f64, f64)),
|
||||
AddPoint((f64, f64)),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Point {
|
||||
x: f64,
|
||||
y: f64,
|
||||
new_line: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AppModel {
|
||||
@@ -83,7 +79,7 @@ impl SimpleComponent for AppModel {
|
||||
gtk::Button {
|
||||
set_label: "Increment",
|
||||
connect_clicked[sender] => move |_| {
|
||||
sender.input(AppInput::Increment);
|
||||
sender.input(AppInput::Input);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -126,8 +122,12 @@ impl SimpleComponent for AppModel {
|
||||
let cx = self.handler.get_context();
|
||||
|
||||
match message {
|
||||
AppInput::Increment => {
|
||||
self.counter = self.counter.wrapping_add(1);
|
||||
AppInput::Input => {
|
||||
let mat = to_matrix(&self.points);
|
||||
for line in mat.iter() {
|
||||
line.iter().for_each(|x| print!("{}", x));
|
||||
println!();
|
||||
}
|
||||
}
|
||||
AppInput::AddPoint((x, y)) => {
|
||||
self.points.push(Point { x, y, new_line: false })
|
||||
|
||||
50
src/process_point.rs
Normal file
50
src/process_point.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use rten_tensor::NdTensor;
|
||||
|
||||
use crate::Point;
|
||||
|
||||
fn process(points: Vec<Point>) -> NdTensor<f32, 3> {
|
||||
|
||||
|
||||
NdTensor::zeros([1, 1, 1])
|
||||
}
|
||||
|
||||
pub fn to_matrix(points: &Vec<Point>) -> Vec<Box<[f64; 1000]>>{
|
||||
|
||||
let min_x = points.iter().min_by_key(|p| p.x as i32).unwrap().x;
|
||||
let min_y = points.iter().min_by_key(|p| p.y as i32).unwrap().y;
|
||||
let max_x = points.iter().max_by_key(|p| p.x as i32).unwrap().x;
|
||||
let max_y = points.iter().max_by_key(|p| p.y as i32).unwrap().y;
|
||||
|
||||
|
||||
let x_len = max_x - min_x;
|
||||
let y_len = max_y - min_y;
|
||||
let x_scale = 800.0 / x_len;
|
||||
let y_scale = (y_len / x_len) * x_scale;
|
||||
|
||||
let scaled_points = points.iter().map(|point| {
|
||||
let x_scaled = ((point.x - min_x) * x_scale) + 100.0;
|
||||
let y_scaled = ((point.y - min_y) * x_scale) + (100.0 * y_scale);
|
||||
|
||||
((x_scaled, y_scaled), point.new_line)
|
||||
});
|
||||
|
||||
let mut matrix: Vec<Box<[f64; 1000]>> = vec![Box::new([-0.0; 1000]); (y_len * y_scale) as u32 as usize];
|
||||
|
||||
for ((x, y), newline) in scaled_points {
|
||||
let start_x = x - 1.5;
|
||||
let end_x = x + 1.5;
|
||||
let start_y = y - 1.5;
|
||||
let end_y = y + 1.5;
|
||||
|
||||
matrix.iter_mut().enumerate().for_each(|(y, line)| line.iter_mut().for_each(|x| {
|
||||
if (start_x < *x && *x < end_x) && (start_y < (y as f64) && (y as f64) < end_y) {
|
||||
*x = 1.0;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
matrix
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user