From cba5372b4d1b7df99feb6e7856e83b2fd4f78469 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 22 May 2022 06:06:36 +0200 Subject: [PATCH] [Refactor] Convert ring_width to a constant --- src/main.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index a101170..4317510 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ const HOUR_NAMES: [&str; 24] = [ const IMAGE_WIDTH: u32 = 700; const HOUR_NAME_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.019109; const OUTER_R: f32 = (IMAGE_WIDTH as f32) / 2.0 - 3.0 * HOUR_NAME_FONT_SIZE; +const RING_WIDTH: f32 = HOUR_NAME_FONT_SIZE * 3.0; enum Season { Spring, @@ -72,8 +73,8 @@ fn time_to_degrees(timestamp: i32, // should be time/timestamp seconds_to_degrees(timestamp) } -fn hour_name_path(ring_width: f32) -> Path { - let radius = OUTER_R - ring_width / 2.0; +fn hour_name_path() -> Path { + let radius = OUTER_R - RING_WIDTH / 2.0; let delta_x = radius * (15.0_f32.to_radians() / 2.0).sin(); let delta_y = radius * (1.0 - (15.0_f32.to_radians() / 2.0).cos()); let x1 = (IMAGE_WIDTH as f32) / 2.0 - delta_x; @@ -92,12 +93,7 @@ fn hour_name_path(ring_width: f32) -> Path { Path::new().set("id", "hour-name-path").set("d", path_data) } -fn hour_marker( - hour: i32, - is_current_hour: bool, - ring_width: f32, - utc_hour_font_size: f32, -) -> Group { +fn hour_marker(hour: i32, is_current_hour: bool, utc_hour_font_size: f32) -> Group { let season = match hour { 0..=5 => Season::Winter, 6..=11 => Season::Spring, @@ -110,23 +106,23 @@ fn hour_marker( let delta_x = OUTER_R * (15f32.to_radians() / 2.0).sin(); let delta_y = OUTER_R * (1.0 - (15f32.to_radians() / 2.0).cos()); - let s_delta_x = 0.0 - ring_width * (15f32.to_radians() / 2.0).sin(); - let s_delta_y = ring_width * (15f32.to_radians() / 2.0).cos(); + let s_delta_x = 0.0 - RING_WIDTH * (15f32.to_radians() / 2.0).sin(); + let s_delta_y = RING_WIDTH * (15f32.to_radians() / 2.0).cos(); - let i_delta_x = -2.0 * (OUTER_R - ring_width) * (15f32.to_radians() / 2.0).sin(); + let i_delta_x = -2.0 * (OUTER_R - RING_WIDTH) * (15f32.to_radians() / 2.0).sin(); let x1 = IMAGE_WIDTH as f32 / 2.0 - delta_x; let y1 = (IMAGE_WIDTH as f32 / 2.0 - OUTER_R) + delta_y; - let utc_hour_y = IMAGE_WIDTH as f32 / 2.0 - OUTER_R + ring_width + utc_hour_font_size; + let utc_hour_y = IMAGE_WIDTH as f32 / 2.0 - OUTER_R + RING_WIDTH + utc_hour_font_size; let path_data = PathData::new() .move_to((x1, y1)) .elliptical_arc_by((OUTER_R, OUTER_R, 15, 0, 1, 2.0 * delta_x, 0)) .line_by((s_delta_x, s_delta_y)) .elliptical_arc_by(( - OUTER_R - ring_width, - OUTER_R - ring_width, + OUTER_R - RING_WIDTH, + OUTER_R - RING_WIDTH, 15, 0, 0, @@ -269,9 +265,8 @@ fn gen_svg(config: &Option) -> Document { let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357; let utc_hour_font_size = IMAGE_WIDTH as f32 * 0.021462; - let ring_width = HOUR_NAME_FONT_SIZE * 3.0; let sun_radius = IMAGE_WIDTH as f32 * 0.0142871; - let marker_radius = OUTER_R - ring_width - 2.0 * utc_hour_font_size; + let marker_radius = OUTER_R - RING_WIDTH - 2.0 * utc_hour_font_size; let border = Rectangle::new() .set("x", 0i32) @@ -308,7 +303,7 @@ fn gen_svg(config: &Option) -> Document { #current-hour-name {font-weight: bold;}", ); - let definitions = Definitions::new().add(hour_name_path(ring_width)); + let definitions = Definitions::new().add(hour_name_path()); let mut local_clock = Group::new().set("id", "local-clock"); for hour in 0i32..24 { @@ -347,7 +342,6 @@ fn gen_svg(config: &Option) -> Document { seasonal_clock = seasonal_clock.add(hour_marker( hour, hour == utc_hour as i32, - ring_width, utc_hour_font_size, )); } @@ -526,7 +520,7 @@ fn gen_svg(config: &Option) -> Document { .set("x2", IMAGE_WIDTH / 2) .set( "y2", - IMAGE_WIDTH as f32 / 2.0 + OUTER_R - ring_width + HOUR_NAME_FONT_SIZE, + IMAGE_WIDTH as f32 / 2.0 + OUTER_R - RING_WIDTH + HOUR_NAME_FONT_SIZE, ); let current_box_width = (200f32 / 700f32) * IMAGE_WIDTH as f32;