[Refactor] Convert outer_r to a constant

This commit is contained in:
Gergely Polonkai 2022-05-22 06:04:32 +02:00
parent dcde43831b
commit fd516083cf
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4

View File

@ -27,6 +27,7 @@ const HOUR_NAMES: [&str; 24] = [
]; ];
const IMAGE_WIDTH: u32 = 700; const IMAGE_WIDTH: u32 = 700;
const HOUR_NAME_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.019109; 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;
enum Season { enum Season {
Spring, Spring,
@ -71,8 +72,8 @@ fn time_to_degrees(timestamp: i32, // should be time/timestamp
seconds_to_degrees(timestamp) seconds_to_degrees(timestamp)
} }
fn hour_name_path(outer_r: f32, ring_width: f32) -> Path { fn hour_name_path(ring_width: f32) -> Path {
let radius = outer_r - ring_width / 2.0; let radius = OUTER_R - ring_width / 2.0;
let delta_x = radius * (15.0_f32.to_radians() / 2.0).sin(); 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 delta_y = radius * (1.0 - (15.0_f32.to_radians() / 2.0).cos());
let x1 = (IMAGE_WIDTH as f32) / 2.0 - delta_x; let x1 = (IMAGE_WIDTH as f32) / 2.0 - delta_x;
@ -94,7 +95,6 @@ fn hour_name_path(outer_r: f32, ring_width: f32) -> Path {
fn hour_marker( fn hour_marker(
hour: i32, hour: i32,
is_current_hour: bool, is_current_hour: bool,
outer_r: f32,
ring_width: f32, ring_width: f32,
utc_hour_font_size: f32, utc_hour_font_size: f32,
) -> Group { ) -> Group {
@ -107,26 +107,26 @@ fn hour_marker(
}; };
let rotation = hour * 15; let rotation = hour * 15;
let delta_x = outer_r * (15f32.to_radians() / 2.0).sin(); 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 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_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_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 x1 = IMAGE_WIDTH as f32 / 2.0 - delta_x;
let y1 = (IMAGE_WIDTH as f32 / 2.0 - outer_r) + delta_y; 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() let path_data = PathData::new()
.move_to((x1, y1)) .move_to((x1, y1))
.elliptical_arc_by((outer_r, outer_r, 15, 0, 1, 2.0 * delta_x, 0)) .elliptical_arc_by((OUTER_R, OUTER_R, 15, 0, 1, 2.0 * delta_x, 0))
.line_by((s_delta_x, s_delta_y)) .line_by((s_delta_x, s_delta_y))
.elliptical_arc_by(( .elliptical_arc_by((
outer_r - ring_width, OUTER_R - ring_width,
outer_r - ring_width, OUTER_R - ring_width,
15, 15,
0, 0,
0, 0,
@ -269,10 +269,9 @@ fn gen_svg(config: &Option<Config>) -> Document {
let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357; let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357;
let utc_hour_font_size = IMAGE_WIDTH as f32 * 0.021462; let utc_hour_font_size = IMAGE_WIDTH as f32 * 0.021462;
let outer_r = (IMAGE_WIDTH as f32) / 2.0 - 3.0 * HOUR_NAME_FONT_SIZE;
let ring_width = HOUR_NAME_FONT_SIZE * 3.0; let ring_width = HOUR_NAME_FONT_SIZE * 3.0;
let sun_radius = IMAGE_WIDTH as f32 * 0.0142871; 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() let border = Rectangle::new()
.set("x", 0i32) .set("x", 0i32)
@ -309,7 +308,7 @@ fn gen_svg(config: &Option<Config>) -> Document {
#current-hour-name {font-weight: bold;}", #current-hour-name {font-weight: bold;}",
); );
let definitions = Definitions::new().add(hour_name_path(outer_r, ring_width)); let definitions = Definitions::new().add(hour_name_path(ring_width));
let mut local_clock = Group::new().set("id", "local-clock"); let mut local_clock = Group::new().set("id", "local-clock");
for hour in 0i32..24 { for hour in 0i32..24 {
@ -326,7 +325,7 @@ fn gen_svg(config: &Option<Config>) -> Document {
.set("x", (IMAGE_WIDTH as f32) / 2.0) .set("x", (IMAGE_WIDTH as f32) / 2.0)
.set( .set(
"y", "y",
(IMAGE_WIDTH as f32) / 2.0 - outer_r - local_hour_font_size / 2.0, (IMAGE_WIDTH as f32) / 2.0 - OUTER_R - local_hour_font_size / 2.0,
) )
.set("text-anchor", "middle") .set("text-anchor", "middle")
.set("font-size", local_hour_font_size as f32) .set("font-size", local_hour_font_size as f32)
@ -348,7 +347,6 @@ fn gen_svg(config: &Option<Config>) -> Document {
seasonal_clock = seasonal_clock.add(hour_marker( seasonal_clock = seasonal_clock.add(hour_marker(
hour, hour,
hour == utc_hour as i32, hour == utc_hour as i32,
outer_r,
ring_width, ring_width,
utc_hour_font_size, utc_hour_font_size,
)); ));
@ -410,7 +408,7 @@ fn gen_svg(config: &Option<Config>) -> Document {
let sun_disc = Circle::new() let sun_disc = Circle::new()
.set("class", "sun") .set("class", "sun")
.set("cx", IMAGE_WIDTH / 2) .set("cx", IMAGE_WIDTH / 2)
.set("cy", IMAGE_WIDTH as f32 / 2.0 + outer_r / 2.0 + sun_radius) .set("cy", IMAGE_WIDTH as f32 / 2.0 + OUTER_R / 2.0 + sun_radius)
.set("r", sun_radius) .set("r", sun_radius)
.set( .set(
"transform", "transform",
@ -524,11 +522,11 @@ fn gen_svg(config: &Option<Config>) -> Document {
), ),
) )
.set("x1", IMAGE_WIDTH / 2) .set("x1", IMAGE_WIDTH / 2)
.set("y1", IMAGE_WIDTH as f32 / 2.0 + outer_r * 0.5) .set("y1", IMAGE_WIDTH as f32 / 2.0 + OUTER_R * 0.5)
.set("x2", IMAGE_WIDTH / 2) .set("x2", IMAGE_WIDTH / 2)
.set( .set(
"y2", "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; let current_box_width = (200f32 / 700f32) * IMAGE_WIDTH as f32;