From f57f03cd87d205d8304008d08c05f53c34b44435 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 7 Apr 2021 19:07:33 +0530 Subject: use floating angles --- src/app.rs | 50 +++++++++++--------------------------------------- src/bitmap.rs | 6 +++--- src/consts.rs | 1 + src/main.rs | 1 + 4 files changed, 16 insertions(+), 42 deletions(-) diff --git a/src/app.rs b/src/app.rs index f42b48c..e8c5260 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,9 +3,10 @@ use crate::{ brush::{Brush, CircleBrush, LineBrush}, cache::Cache, command::CommandBox, - consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH}, + consts::{colors::*, ANGLE, FONT_PATH, RC_PATH, STDLIB_PATH}, dither, error::AppError, + grid::Grid, guide::Guide, lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, message::Message, @@ -68,20 +69,6 @@ pub struct AppState<'ctx> { pub zoom: u8, } -pub struct Grid { - pub enabled: bool, - pub color: Color, -} - -impl Grid { - fn new() -> Self { - Self { - enabled: true, - color: GRID_COLOR, - } - } -} - // private actions on appstate impl<'ctx> AppState<'ctx> { fn pan>(&mut self, direction: P) { @@ -331,27 +318,6 @@ impl<'ctx> AppState<'ctx> { self.mode = Mode::Draw; } - fn draw_grid(&mut self) { - let cs = self.zoom as u32; - let (width, height) = (self.width(), self.height()); - let canvas = &mut self.canvas; - canvas.set_draw_color(self.grid.color); - for i in 1..width { - let x = (i * cs) as i32; - let y = (height * cs) as i32; - let start = self.start + Point::new(x, 0); - let end = self.start + Point::new(x, y); - canvas.draw_line(start, end).unwrap(); - } - for j in 1..height { - let x = (width * cs) as i32; - let y = (j * cs) as i32; - let start = self.start + Point::new(0, y); - let end = self.start + Point::new(x, y); - canvas.draw_line(start, end).unwrap(); - } - } - fn draw_statusline(&mut self) { let container = Container::new(Offset::Left(0), Offset::Bottom(40), &self.canvas) .width(Size::Max, &self.canvas) @@ -482,10 +448,15 @@ impl<'ctx> AppState<'ctx> { let size = self.zoom as u32 * (size as u32 + 5); if let (Some(from), Some(to)) = (start, pt) { let line = self.pixmap.get_line(from, to.into()); + let angle = positive_angle_with_x(from, to.into()); draw_text( &mut self.canvas, self.ttf_context, - format!("{}°", positive_angle_with_x(from, to.into())), + format!( + "{:.width$}°", + angle, + width = if (angle - ANGLE).abs() < 1e-3 { 3 } else { 0 } + ), PINK, (self.mouse.0 + size as i32, self.mouse.1 + size as i32), ); @@ -546,7 +517,7 @@ impl<'ctx> AppState<'ctx> { fn draw(&mut self) { let cs = self.zoom as u32; - let (width, _) = (self.width(), self.height()); + let (width, height) = (self.width(), self.height()); let start = self.start; let grid_enabled = self.grid.enabled; self.canvas.set_draw_color(WHITE); @@ -576,7 +547,8 @@ impl<'ctx> AppState<'ctx> { )) .unwrap(); if grid_enabled { - self.draw_grid(); + self.grid + .draw(&mut self.canvas, self.zoom, &self.start, width, height); } self.draw_guides(); self.draw_symmetry(); diff --git a/src/bitmap.rs b/src/bitmap.rs index 2f96a99..ba5c8f1 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -325,11 +325,11 @@ pub fn reflect_figure(figure: &[MapPoint], around: MapPoint) -> Vec { figure.iter().map(|pt| pt.reflect(around)).collect() } -pub fn positive_angle_with_x(start: MapPoint, end: MapPoint) -> u32 { +pub fn positive_angle_with_x(start: MapPoint, end: MapPoint) -> f64 { if end.x == start.x { - return 90; + return 90.; } let numer = (end.y as f64 - start.y as f64).abs(); let denum = (end.x as f64 - start.x as f64).abs(); - (numer / denum).atan().to_degrees() as u32 + (numer / denum).atan().to_degrees() } diff --git a/src/consts.rs b/src/consts.rs index 0b43f0a..7087073 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -11,3 +11,4 @@ pub mod colors { pub const FONT_PATH: &str = "./assets/NerdInput-Regular.ttf"; pub const STDLIB_PATH: &str = "./src/lisp/std.lisp"; pub const RC_PATH: &str = "/home/np/.config/sdl-tests"; +pub const ANGLE: f64 = 26.565; diff --git a/src/main.rs b/src/main.rs index 245ee83..220956a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod command; mod consts; mod dither; mod error; +mod grid; mod guide; mod lisp; mod message; -- cgit v1.2.3