From 4b68aa0ec5b88cc8939812bf9e5ab80b853359b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Sun, 9 Sep 2018 22:18:16 +0000 Subject: [PATCH 1/8] Minesweeper: Changed shading to light --- src/minesweeper/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/minesweeper/main.rs b/src/minesweeper/main.rs index d65c303..a786659 100644 --- a/src/minesweeper/main.rs +++ b/src/minesweeper/main.rs @@ -37,7 +37,7 @@ const FLAGGED: &'static str = "F"; /// The string printed for mines in the game over revealing. const MINE: &'static str = "*"; /// The string printed for concealed cells. -const CONCEALED: &'static str = "▒"; +const CONCEALED: &'static str = "░"; /// The game over screen. const GAME_OVER: &'static str = "╔═════════════════╗\n\r\ -- GitLab From de384adc263e1abd7c4a116b1bc044f2cc18ec02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Tue, 11 Sep 2018 21:27:11 +0000 Subject: [PATCH 2/8] Initial commit for POM --- Cargo.toml | 4 ++ src/pom/main.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/pom/main.rs diff --git a/Cargo.toml b/Cargo.toml index c7f10f8..0e43b7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,10 @@ path = "src/ice/main.rs" name = "h4xx3r" path = "src/h4xx3r/main.rs" +[[bin]] +name = "pom" +path = "src/pom/main.rs" + [lib] name = "rusthello_lib" path = "src/rusthello/lib.rs" diff --git a/src/pom/main.rs b/src/pom/main.rs new file mode 100644 index 0000000..d57eac4 --- /dev/null +++ b/src/pom/main.rs @@ -0,0 +1,97 @@ +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +// vim: et:ts=4:sts=4:sw=4 + +// This file is a part of Redox OS games, which is distributed under terms of +// MIT license. +// +// Copyright (c) 2018 Árni Dagur +// +// Based on BSD games phase of the moon. + +/// Phase of the moon. Calculates the current phase of the moon based on +/// routines from 'Practical Astronomy with Your Calculator or Spreadsheet' by +/// Duffet-Smith. The 4th edition of the book is available online for free at +/// https://archive.org/ +// Comments give the section from the book that particular piece of code was +// adapted from. + +use std::f64::consts::PI; + +// We define an epoch on which we shall base our calculations; here it is +// 2010 January 0.0 +const EPSILON_g: f64 = 279.447208f64; // The Sun's mean ecliptic long at epoch. +const RHO_g: f64 = 283.112438f64; // The longitude of the Sun at perigee. +const ECC: f64 = 0.016705f64; // Eccintricity of the Sun-Earth orbit. +const FRAC_360_TROP_YEAR: f64 = 0.9856473563866f64; // 360 divided by 365.242191 + +const L_0: f64 = 91.929335f64; // Moon's mean longitude at the epoch +const P_0: f64 = 130.143076f64; // Moon's mean longitude of the perigee at epoch +const N_0: f64 = 291.682547f64; // Moon's mean longitude of the node at epoch + +fn potm(days: f64) -> f64 { + // Section 46: Calculating the position of the sun + let n = adj360(FRAC_360_TROP_YEAR * days); + // We calulate: + // (a) The true solar anomoly in an ellipse + let M_sol = adj360(n + EPSILON_g - RHO_g); + // (b) The longitude of the sun + let Lambda_sol = adj360(n + 360.0 / PI * ECC + * M_sol.to_radians().sin() + EPSILON_g); + + // Section 65: Calculating the Moon's position + // TODO: Switch to more accurate MoonPos2 model instead of MoonPos1 + // We calculate: + // (a) the Sun's ecliptic longitude Lambda_sol, and mean anomaly M_sol, + // by the method given in section 46. (Done above) + // (b) the Moon's mean longitude, l + let l = adj360(13.1763966f64 * days + L_0); + // (c) the Moon's mean anomaly, M_m + let M_m = adj360(l - 0.1114041f64 * days - P_0); + // (d) the ascending node's mean longitude, N + let N_m = adj360(N_0 - 0.0529539f64 * days); + // Next we calculate the corrections for: + // (a) Evection + let E_v = 1.2739 * (2.0 * (l - Lambda_sol) - M_m).to_radians().sin(); + // (b) The annual equation + let A_e = 0.1858 * M_sol.to_radians().sin(); + // (c) And a 'third' correction + let A_3 = 0.37 * M_sol.to_radians().sin(); + // Applying these corrections gives the Moon's corrected anomaly + let M_m_prime = M_m - E_v - A_e - A_3; + // Correction for the equation of the centre: + let E_c = 6.2886 * M_m_prime.to_radians().sin(); + // Another correction term must be calculated: + let A_4 = 0.214 * (2.0 * M_m_prime).to_radians().sin(); + // We can now find the Moon's corrected longitude, l_prime + let l_prime = l + E_v + E_c - A_e + A_4; + // The final correction to apply to the Moon's longitude is the variation + let V = 0.6583 * (2.0 * (l_prime - Lambda_sol)).to_radians().sin(); + // So the Moon's true orbital longitude is: + let l_2prime = l_prime + V; + + // Section 67: The phases of the Moon + // Calculate the 'age' of the moon. + let D = l_2prime - Lambda_sol; + // The Moon's phase, F, on the scale from 0 to 100, is given by: + 0.5 * (1.0 - D.to_radians().cos()) +} + +/// Adjusts value so 0 <= deg <= 360 +fn adj360(mut deg: f64) -> f64 { + loop { + if deg < 0.0 { + deg += 360.0; + } else if deg > 360.0 { + deg -= 360.0; + } else { + break; + } + } + deg +} + +fn main() { + println!("{:?}", potm(0.0)) +} + -- GitLab From d12c9c082ecc97560d28dc76e0828391467a85b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Wed, 12 Sep 2018 01:14:35 +0000 Subject: [PATCH 3/8] Add argument parsing to pom --- Cargo.lock | 60 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/pom/main.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 131 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a2a634..eb3b0b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,16 @@ name = "bitflags" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "coco" version = "0.1.1" @@ -47,6 +57,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "games-for-redox" version = "0.1.0" dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "extra 0.1.0 (git+https://gitlab.redox-os.org/redox-os/libextra.git)", "libgo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "liner 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -83,6 +94,19 @@ dependencies = [ "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-integer" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "num_cpus" version = "1.7.0" @@ -157,13 +181,43 @@ dependencies = [ "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-width" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c06169f5beb7e31c7c67ebf5540b8b472d23e3eade3b2ec7d1f5b504a85f91bd" "checksum either 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cbee135e9245416869bf52bd6ccc9b59e2482651510784e089b874272f02a252" "checksum extra 0.1.0 (git+https://gitlab.redox-os.org/redox-os/libextra.git)" = "" @@ -174,6 +228,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)" = "56cce3130fd040c28df6f495c8492e5ec5808fb4c9093c310df02b0c8f030148" "checksum libgo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "99a64661b7d5c8b2ccb779e3fb0ced098426bb9bfbded934aa76ef227300e0cf" "checksum liner 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f9e406164c25b420480023985bdf65cef366855666ad4cb12cd3eaee82dcb399" +"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" +"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" "checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d" "checksum rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "61efcbcd9fa8d8fbb07c84e34a8af18a1ff177b449689ad38a6e9457ecc7b2ae" "checksum rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b614fe08b6665cb9a231d07ac1364b0ef3cb3698f1239ee0c4c3a88a524f54c8" @@ -183,4 +239,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum reversi 0.5.0 (git+https://github.com/EGhiorzi/reversi/)" = "" "checksum scopeguard 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c79eb2c3ac4bc2507cda80e7f3ac5b88bd8eae4c0914d5663e6a8933994be918" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 0e43b7c..e435081 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ liner = "0.1" rand = "0.3" rayon = "0.8" termion = "1.5.1" +chrono = "0.4" [dependencies.extra] git = "https://gitlab.redox-os.org/redox-os/libextra.git" diff --git a/src/pom/main.rs b/src/pom/main.rs index d57eac4..2ba1a83 100644 --- a/src/pom/main.rs +++ b/src/pom/main.rs @@ -1,13 +1,20 @@ -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] -// vim: et:ts=4:sts=4:sw=4 - // This file is a part of Redox OS games, which is distributed under terms of // MIT license. // // Copyright (c) 2018 Árni Dagur // -// Based on BSD games phase of the moon. +// vim: et:ts=4:sts=4:sw=4 +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] + +extern crate chrono; +use chrono::{Local, DateTime, TimeZone}; +use chrono::offset; + +use std::env; +use std::process; +use std::io::{self, Write}; +use std::f64::consts::PI; /// Phase of the moon. Calculates the current phase of the moon based on /// routines from 'Practical Astronomy with Your Calculator or Spreadsheet' by @@ -16,7 +23,16 @@ // Comments give the section from the book that particular piece of code was // adapted from. -use std::f64::consts::PI; +const HELP: &'static str = r#" +pom ~ Phase of the Moon + +flags: + -h | --help ~ this help message. + -dt | --datetime ~ specify datetime in "YY-MM-DD HH:MM:SS" format + +author: + Árni Dagur +"#; // We define an epoch on which we shall base our calculations; here it is // 2010 January 0.0 @@ -29,6 +45,8 @@ const L_0: f64 = 91.929335f64; // Moon's mean longitude at the epoch const P_0: f64 = 130.143076f64; // Moon's mean longitude of the perigee at epoch const N_0: f64 = 291.682547f64; // Moon's mean longitude of the node at epoch +/// Calculate the phase of the moon given a certain number of days away from the +/// epoch January 2010. fn potm(days: f64) -> f64 { // Section 46: Calculating the position of the sun let n = adj360(FRAC_360_TROP_YEAR * days); @@ -92,6 +110,51 @@ fn adj360(mut deg: f64) -> f64 { } fn main() { - println!("{:?}", potm(0.0)) + let mut args = env::args().skip(1); + println!("{}", potm(0.5)); + println!("{:?}", args); + + let stdout = io::stdout(); + let mut stdout = stdout.lock(); + let stderr = io::stderr(); + let mut stderr = stderr.lock(); + + let mut datetime: DateTime = Local::now(); + + loop { + // Read the arguments. + let arg = if let Some(x) = args.next() { + x + } else { + break; + }; + + match arg.as_str() { + "-h" | "--help" => { + stdout.write(HELP.as_bytes()).unwrap(); + stdout.flush().unwrap(); + process::exit(0); + }, + "-dt" | "--datetime" => { + datetime = offset::Local.datetime_from_str( + &args.next().unwrap_or_else(|| { + stderr.write(b"No datetime given.\n").unwrap(); + stderr.flush().unwrap(); + process::exit(1); + }), + &"%Y-%m-%d %H:%M:%S" + ).unwrap_or_else(|_| { + stderr.write(b"Invalid datetime given.\n").unwrap(); + stderr.flush().unwrap(); + process::exit(1); + }); + }, + _ => { + stderr.write(b"Unknown argument.\n").unwrap(); + stderr.flush().unwrap(); + process::exit(1); + } + } + } } -- GitLab From 295666583691c553b9e8493fb16f931e426b7c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Wed, 12 Sep 2018 22:01:46 +0000 Subject: [PATCH 4/8] Implement the printing of the phase of the moon --- src/pom/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/pom/main.rs b/src/pom/main.rs index 2ba1a83..35ea45f 100644 --- a/src/pom/main.rs +++ b/src/pom/main.rs @@ -8,7 +8,7 @@ #![allow(non_upper_case_globals)] extern crate chrono; -use chrono::{Local, DateTime, TimeZone}; +use chrono::{Local, DateTime, TimeZone, Utc}; use chrono::offset; use std::env; @@ -27,15 +27,17 @@ const HELP: &'static str = r#" pom ~ Phase of the Moon flags: - -h | --help ~ this help message. - -dt | --datetime ~ specify datetime in "YY-MM-DD HH:MM:SS" format + -h | --help ~ this help message. + -dt | --datetime ~ specify datetime in "YY-MM-DD HH:MM:SS" format + -p | --percentage ~ only print the percentage author: Árni Dagur "#; // We define an epoch on which we shall base our calculations; here it is -// 2010 January 0.0 +// 2010 January 0.0, equivilent to the midnight between 30. and 31. december of +// 2009 (see section 3 for details). const EPSILON_g: f64 = 279.447208f64; // The Sun's mean ecliptic long at epoch. const RHO_g: f64 = 283.112438f64; // The longitude of the Sun at perigee. const ECC: f64 = 0.016705f64; // Eccintricity of the Sun-Earth orbit. @@ -92,7 +94,7 @@ fn potm(days: f64) -> f64 { // Calculate the 'age' of the moon. let D = l_2prime - Lambda_sol; // The Moon's phase, F, on the scale from 0 to 100, is given by: - 0.5 * (1.0 - D.to_radians().cos()) + 50.0 * (1.0 - D.to_radians().cos()) } /// Adjusts value so 0 <= deg <= 360 @@ -110,17 +112,16 @@ fn adj360(mut deg: f64) -> f64 { } fn main() { - let mut args = env::args().skip(1); - println!("{}", potm(0.5)); - println!("{:?}", args); - let stdout = io::stdout(); let mut stdout = stdout.lock(); let stderr = io::stderr(); let mut stderr = stderr.lock(); + let epoch: DateTime = offset::Utc.ymd(2009, 12, 31).and_hms(0, 0, 0); let mut datetime: DateTime = Local::now(); + let mut args = env::args().skip(1); + let mut percentage_only = false; loop { // Read the arguments. let arg = if let Some(x) = args.next() { @@ -149,6 +150,9 @@ fn main() { process::exit(1); }); }, + "-p" | "--percentage" => { + percentage_only = true; + }, _ => { stderr.write(b"Unknown argument.\n").unwrap(); stderr.flush().unwrap(); @@ -156,5 +160,42 @@ fn main() { } } } -} + let seconds = datetime.signed_duration_since(epoch).num_seconds(); + let days = seconds as f64 / 86400.0; + // Why add 0.05 ? + // let today = potm(days) + 0.5; + let today = potm(days); + + if percentage_only { + println!("{}", today); + process::exit(0); + } + + stdout.write(b"The moon is").unwrap(); + if today.round() == 100.0 { + stdout.write(b"full\n").unwrap(); + } else if today.round() == 0.0 { + stdout.write(b"new\n").unwrap(); + } else { + let tomorrow = potm(days + 1.0); + if today.round() == 50.0 { + if tomorrow > today { + stdout.write(b"at the first quarter\n").unwrap(); + } else { + stdout.write(b"at the last quarter\n").unwrap(); + } + } else { + if tomorrow > today { + stdout.write(b" waxing ").unwrap(); + } else { + stdout.write(b" waning ").unwrap(); + } + if today > 50.0 { + println!("gibbous {}% of full", today) + } else { + println!("crescent {}% of full", today) + } + } + } +} -- GitLab From 7b6a37f05d94dcbd99d5b876f54ecbdbb8361dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Thu, 13 Sep 2018 01:10:46 +0000 Subject: [PATCH 5/8] Made formatting more pretty This involved two changes: - Making all text title case. - Rounded percentage values --- src/pom/main.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/pom/main.rs b/src/pom/main.rs index 35ea45f..a20898d 100644 --- a/src/pom/main.rs +++ b/src/pom/main.rs @@ -168,33 +168,33 @@ fn main() { let today = potm(days); if percentage_only { - println!("{}", today); + println!("{:.2}", today); process::exit(0); } - stdout.write(b"The moon is").unwrap(); + stdout.write(b"The Moon is").unwrap(); if today.round() == 100.0 { - stdout.write(b"full\n").unwrap(); + stdout.write(b"Full\n").unwrap(); } else if today.round() == 0.0 { - stdout.write(b"new\n").unwrap(); + stdout.write(b"New\n").unwrap(); } else { let tomorrow = potm(days + 1.0); if today.round() == 50.0 { if tomorrow > today { - stdout.write(b"at the first quarter\n").unwrap(); + stdout.write(b"at the First Quarter\n").unwrap(); } else { - stdout.write(b"at the last quarter\n").unwrap(); + stdout.write(b"at the Last Quarter\n").unwrap(); } } else { if tomorrow > today { - stdout.write(b" waxing ").unwrap(); + stdout.write(b" Waxing ").unwrap(); } else { - stdout.write(b" waning ").unwrap(); + stdout.write(b" Waning ").unwrap(); } if today > 50.0 { - println!("gibbous {}% of full", today) + println!("Gibbous {:.2}% of Full", today) } else { - println!("crescent {}% of full", today) + println!("Crescent {:.2}% of Full", today) } } } -- GitLab From b16992ca94a63b12ddd776728d7b83de0d6240e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Thu, 13 Sep 2018 01:15:57 +0000 Subject: [PATCH 6/8] Removed unused code and comments --- src/pom/main.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/pom/main.rs b/src/pom/main.rs index a20898d..b917c6a 100644 --- a/src/pom/main.rs +++ b/src/pom/main.rs @@ -45,7 +45,6 @@ const FRAC_360_TROP_YEAR: f64 = 0.9856473563866f64; // 360 divided by 365.242191 const L_0: f64 = 91.929335f64; // Moon's mean longitude at the epoch const P_0: f64 = 130.143076f64; // Moon's mean longitude of the perigee at epoch -const N_0: f64 = 291.682547f64; // Moon's mean longitude of the node at epoch /// Calculate the phase of the moon given a certain number of days away from the /// epoch January 2010. @@ -68,8 +67,6 @@ fn potm(days: f64) -> f64 { let l = adj360(13.1763966f64 * days + L_0); // (c) the Moon's mean anomaly, M_m let M_m = adj360(l - 0.1114041f64 * days - P_0); - // (d) the ascending node's mean longitude, N - let N_m = adj360(N_0 - 0.0529539f64 * days); // Next we calculate the corrections for: // (a) Evection let E_v = 1.2739 * (2.0 * (l - Lambda_sol) - M_m).to_radians().sin(); @@ -163,8 +160,6 @@ fn main() { let seconds = datetime.signed_duration_since(epoch).num_seconds(); let days = seconds as f64 / 86400.0; - // Why add 0.05 ? - // let today = potm(days) + 0.5; let today = potm(days); if percentage_only { -- GitLab From 26b8f5fd85efe13943c875cc916b9a94a2275175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Thu, 13 Sep 2018 01:26:37 +0000 Subject: [PATCH 7/8] Round out formatting before release This includes: - Remove unintended newline on top of help message. - Add brackets around percentage to increase visibility. --- src/pom/main.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pom/main.rs b/src/pom/main.rs index b917c6a..c994af4 100644 --- a/src/pom/main.rs +++ b/src/pom/main.rs @@ -23,8 +23,7 @@ use std::f64::consts::PI; // Comments give the section from the book that particular piece of code was // adapted from. -const HELP: &'static str = r#" -pom ~ Phase of the Moon +const HELP: &'static str = r#"pom ~ Phase of the Moon flags: -h | --help ~ this help message. @@ -187,9 +186,9 @@ fn main() { stdout.write(b" Waning ").unwrap(); } if today > 50.0 { - println!("Gibbous {:.2}% of Full", today) + println!("Gibbous ({:.2}% of Full)", today) } else { - println!("Crescent {:.2}% of Full", today) + println!("Crescent ({:.2}% of Full)", today) } } } -- GitLab From 998dc8e8f9fb14c267e131d956aa65d9217c8490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Sun, 16 Sep 2018 18:14:39 +0000 Subject: [PATCH 8/8] Add some unit tests to pom --- src/pom/main.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/pom/main.rs b/src/pom/main.rs index c994af4..d1503d9 100644 --- a/src/pom/main.rs +++ b/src/pom/main.rs @@ -193,3 +193,38 @@ fn main() { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_potm_on_epoch() { + assert_eq!(potm(0.0).round(), 99.0); + } + + #[test] + fn test_potm_on_leap_day_2016_noon() { + assert_eq!(potm(2251.5).round(), 64.0); + } + + #[test] + fn test_potm_on_13_october_2004_0530am() { + assert_eq!(potm(-1904.770833333).round(), 1.0); + } + + #[test] + fn test_adj360_above_360() { + assert!((adj360(821.0) - 101.0).abs() < 1e-10); + } + + #[test] + fn test_adj360_below_0() { + assert!((adj360(-643.4) - 76.6).abs() < 1e-10); + } + + #[test] + fn test_adj360_between_0_and_360() { + assert!((adj360(168.0) - 168.0).abs() < 1e-10); + } +} -- GitLab