Add markers with the solar noon and nadir (solar midnight)

This commit is contained in:
Gergely Polonkai 2022-05-18 05:23:44 +02:00
parent fc40b1a8db
commit 5513510ad1
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4

View File

@ -3,6 +3,7 @@ extern crate smithay_client_toolkit as sctk;
use std::time::SystemTime; use std::time::SystemTime;
use chrono::prelude::Local; use chrono::prelude::Local;
use chrono::TimeZone;
use chrono::Timelike; use chrono::Timelike;
use sctk::reexports::client::protocol::{wl_shm, wl_surface}; use sctk::reexports::client::protocol::{wl_shm, wl_surface};
use sctk::shm::AutoMemPool; use sctk::shm::AutoMemPool;
@ -101,11 +102,14 @@ fn get_moon_path(image_width: u32, radius: f32, moon_phase: f64) -> Path {
} }
fn gen_svg() -> Document { fn gen_svg() -> Document {
// These should be calculated
let local_timestamp = Local::now(); let local_timestamp = Local::now();
let utc_offset = local_timestamp.offset().local_minus_utc(); let utc_offset = local_timestamp.offset().local_minus_utc();
let local_time = local_timestamp.time().num_seconds_from_midnight() as i32; let local_time = local_timestamp.time().num_seconds_from_midnight() as i32;
// TODO: Make these configurable
let lat = 47.655235;
let long = 19.2868815;
// TODO: These should be calculated instead of hardcoded // TODO: These should be calculated instead of hardcoded
let image_width = 700u32; let image_width = 700u32;
@ -115,11 +119,23 @@ fn gen_svg() -> Document {
let moon_radius = image_width as f32 * 0.071428571; let moon_radius = image_width as f32 * 0.071428571;
let moon_phase = moon_illumination.phase * 28.0; let moon_phase = moon_illumination.phase * 28.0;
let sun_times = suncalc::get_times(unixtime, lat, long, None);
let noon = Local
.timestamp_millis(sun_times.solar_noon.0)
.time()
.num_seconds_from_midnight() as i32;
let midnight = Local
.timestamp_millis(sun_times.nadir.0)
.time()
.num_seconds_from_midnight() as i32;
let local_hour_font_size = 16.5; let local_hour_font_size = 16.5;
let utc_hour_font_size = image_width as f32 * 0.021462;
let hour_name_font_size = 13.37699; let hour_name_font_size = 13.37699;
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 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 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)
.set("y", 0i32) .set("y", 0i32)
@ -133,6 +149,7 @@ fn gen_svg() -> Document {
.local-hour {stroke: none; fill: rgb(238, 187, 85);} .local-hour {stroke: none; fill: rgb(238, 187, 85);}
.moon-background {stroke: rgb(170, 170, 170); stroke-width: 2px; fill: rgb(19, 17, 30);} .moon-background {stroke: rgb(170, 170, 170); stroke-width: 2px; fill: rgb(19, 17, 30);}
.moon {stroke: none; fill: rgb(170, 170, 170);} .moon {stroke: none; fill: rgb(170, 170, 170);}
.mid-marker {stroke: red;}
.dial {stroke-width: 2px; stroke: rgb(238, 187, 85);}", .dial {stroke-width: 2px; stroke: rgb(238, 187, 85);}",
); );
let mut local_clock = Group::new().set("id", "local-clock"); let mut local_clock = Group::new().set("id", "local-clock");
@ -190,6 +207,42 @@ fn gen_svg() -> Document {
.add(moon_circle) .add(moon_circle)
.add(get_moon_path(image_width, moon_radius, moon_phase)); .add(get_moon_path(image_width, moon_radius, moon_phase));
let noon_marker = Line::new()
.set("class", "mid-marker")
.set(
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(noon),
image_width / 2,
image_width / 2
),
)
.set("x1", image_width / 2)
.set(
"y1",
image_width as f32 / 2.0 + marker_radius - sun_radius as f32,
)
.set("x2", image_width / 2)
.set("y2", image_width as f32 / 2.0 + marker_radius);
let midnight_marker = Line::new()
.set("class", "mid-marker")
.set(
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(midnight),
image_width / 2,
image_width / 2
),
)
.set("x1", image_width / 2)
.set(
"y1",
image_width as f32 / 2.0 + marker_radius - sun_radius as f32,
)
.set("x2", image_width / 2)
.set("y2", image_width as f32 / 2.0 + marker_radius);
let dial = Line::new() let dial = Line::new()
.set("id", "dial") .set("id", "dial")
.set("class", "dial") .set("class", "dial")
@ -220,6 +273,8 @@ fn gen_svg() -> Document {
.add(local_clock) .add(local_clock)
.add(seasonal_clock) .add(seasonal_clock)
.add(moon_group) .add(moon_group)
.add(noon_marker)
.add(midnight_marker)
.add(dial) .add(dial)
} }