in reply to Re: Average start time handling midnight
in thread Average start time handling midnight
Not that complicated, really, although it does seem a bit silly to do trigonometry where simple arithmetic would suffice.
#! /usr/bin/perl -wl my @times = qw( 17:00 19:00 11:00 13:00 23:00 01:00 10:30 13:00 19:40 01:20 16:00 02:00 ); # keep angles in range -pi .. pi sub _PI () { 2 * atan2(1, 0) } sub _TC () { 24 * 60 / (2 * _PI) } sub time2angle { map { my ($h, $m) = split /:/; _PI - (60 * $h + $m) / _TC } @_ } sub angle2time { map { my $m = int _TC * (_PI - $_); sprintf "%02d:%02d", $m / 60, $m % 60 } @_ } use List::Util qw( sum ); sub circ_avg { atan2 sum(map sin, @_), sum(map cos, @_) } for (; @times > 1; shift @times) { my @t = @times[0, 1]; print "@t => @{[angle2time circ_avg time2angle @t]}"; }
Thank you for the wikipedia link.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Average start time handling midnight
by pryrt (Abbot) on Jul 21, 2016 at 22:40 UTC |