Averages/Mean angle: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
No edit summary |
||
Line 1,609: | Line 1,609: | ||
The mean angle of [10, 20, 30] is: 20.0 degrees |
The mean angle of [10, 20, 30] is: 20.0 degrees |
||
>>> </lang> |
>>> </lang> |
||
=={{header|R}}== |
|||
<lang R> |
|||
deg2rad <- function(x) { |
|||
x * pi/180 |
|||
} |
|||
rad2deg <- function(x) { |
|||
x * 180/pi |
|||
} |
|||
deg2vec <- function(x) { |
|||
c(sin(deg2rad(x)), cos(deg2rad(x))) |
|||
} |
|||
vec2deg <- function(x) { |
|||
rad2deg(atan2(x[1], x[2])) |
|||
} |
|||
mean_vec <- function(x) { |
|||
y <- lapply(x, deg2vec) |
|||
Reduce(`+`, y)/length(y) |
|||
} |
|||
mean_deg <- function(x) { |
|||
res <- vec2deg(mean_vec(x)) |
|||
if (res < 0) { |
|||
360 + res |
|||
} else { |
|||
res |
|||
} |
|||
} |
|||
mean_deg(c(350, 10)) |
|||
mean_deg(c(90, 180, 270, 360)) |
|||
mean_deg(c(10, 20, 30)) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
360 |
|||
270 |
|||
20 |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |