Last active
May 23, 2020 03:17
-
-
Save stormwatch/9d8a6ce8ba54d076f4416bd474512b46 to your computer and use it in GitHub Desktop.
2.8 Pulling it all together (second week assignment)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(two8). | |
-export([perimeter/1, area/1, enclose/1, bits/1, bits_tr/1]). | |
%% Shapes | |
%% Define a function perimeter/1 which takes a shape and returns the perimeter | |
%% of the shape. | |
%% Choose a suitable representation of triangles, and augment area/1 and | |
%% perimeter/1 to handle this case too. | |
%% we assume that the shape's data is represented as shown in | |
%% https://www.futurelearn.com/courses/functional-programming-erlang/3/steps/488100/ | |
%% {circle, {X, Y}, R} | |
%% {rectangle, {X, Y}, H, W} | |
%% with {X, Y} being the coordinates of the shape's centre. | |
%% Triangles are represented as {triangle, {X1, Y1}, {X2, Y2}, {X3, Y3}} | |
%% instead, where each tuple {Xn, Yn} corresponds to a vertex's coordinates. | |
distance({X1, Y1}, {X2, Y2}) -> | |
math:sqrt((((X2 - X1) * (X2 - X1)) + ((Y2 - Y1) * (Y2 - Y1)))). | |
perimeter({circle, _, R}) -> | |
2 * math:pi() * R; | |
perimeter({rectangle, _, H, W}) -> | |
2 * (H + W); | |
perimeter({triangle, {X1, Y1}, {X2, Y2}, {X3, Y3}}) -> | |
A = distance({X1, Y1}, {X2, Y2}), | |
B = distance({X2, Y2}, {X3, Y3}), | |
C = distance({X3, Y3}, {X1, Y1}), | |
A + B + C. | |
area({circle, _, R}) -> | |
math:pi() * R * R; | |
area({rectangle, _Centre, H, W}) -> | |
H * W; | |
area({triangle, {X1, Y1}, {X2, Y2}, {X3, Y3}}) -> | |
abs((X1 * (Y2 - Y3) + X2 * (Y3 - Y1) + X3 * (Y1 - Y2)) / 2). | |
%% Define a function enclose/1 that takes a shape and returns the smallest | |
%% enclosing rectangle of the shape. | |
enclose({circle, {X, Y}, R}) -> | |
D = R * 2, | |
{rectangle, {X, Y}, D, D}; | |
enclose({rectangle, _Centre, _H, _W} = Identity) -> | |
%% Is this idiomatic when we want to just return the identity? | |
Identity; | |
enclose({triangle, {X1, Y1}, {X2, Y2}, {X3, Y3}}) -> | |
Xmin = lists:min([X1, X2, X3]), | |
Xmax = lists:max([X1, X2, X3]), | |
Ymin = lists:min([Y1, Y2, Y3]), | |
Ymax = lists:max([Y1, Y2, Y3]), | |
{rectangle, {(Xmin + Xmax) / 2, (Ymin + Ymax) / 2}, Ymax - Ymin, Xmax - Xmin}. | |
%% Summing the bits | |
%% Define a function bits/1 that takes a positive integer N and returns the sum | |
%% of the bits in the binary representation. For example bits(7) is 3 and | |
%% bits(8) is 1. | |
bits(0) -> | |
0; | |
bits(1) -> | |
1; | |
bits(N) when N > 1 -> | |
bits(N div 2) + N rem 2. | |
bits_tr(N) when N >= 0 -> | |
bits_tr(N, 0). | |
bits_tr(0, Acc) -> | |
Acc; | |
bits_tr(N, Acc) -> | |
bits_tr(N div 2, Acc + N rem 2). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Comments
I learned that as of today there are 38494 triangle centre definitions!
No wonder then that I choosed to spare them; triangles are represented as {triangle, {X1, Y1}, {X2, Y2}, {X3, Y3}} instead, where each {Xn, Yn} marks vertex coordinates.
bits_tr/1 is preferred over the non-tail recursive bits/1 but I'd probably should compare them systematically like in https://ferd.ca/erlang-s-tail-recursion-is-not-a-silver-bullet.html
Also, there are for sure more efficient ways of computing Hamming Weights https://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer
Questions: