Created
March 4, 2017 22:35
-
-
Save fswalker/447fe3db55cbaeb2191c497642d98d3d to your computer and use it in GitHub Desktop.
This file contains 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(ex1_shapes). | |
-export([perimeter/1,test_perimeter/0,area/1,test_area/0,enclose/1,test_enclose/0,run_tests/0]). | |
%% Perimeter function and tests | |
perimeter({circle, R}) -> | |
{ok, math:pi() * 2 * R}; | |
perimeter({rectangle, {A, B}}) -> | |
{ok, 2 * A + 2 * B}; | |
perimeter({square, A}) -> | |
{ok, 4 * A}; | |
perimeter({triangle, {A,B,C}}) -> | |
{ok, A + B + C}; | |
perimeter(_) -> | |
{error, 'Unknown shape'}. | |
test_perimeter() -> | |
{ok, 6.283185307179586} = perimeter({circle, 1}), | |
{ok, 10} = perimeter({rectangle, {3,2}}), | |
{ok, 8} = perimeter({square, 2}), | |
{ok, 12} = perimeter({triangle, {4,4,4}}), | |
{ok, 12} = perimeter({triangle, {3,4,5}}), | |
{ok, 11} = perimeter({triangle, {2,4,5}}), | |
{error, 'Unknown shape'} = perimeter({trapezoid, {1,2,3,4}}), | |
'Tests passed'. | |
%% Area function and tests | |
area({circle, R}) -> | |
{ok, math:pi() * R * R}; | |
area({rectangle, {A, B}}) -> | |
{ok, A * B}; | |
area({square, A}) -> | |
{ok, A * A}; | |
area({triangle, {A,B,C}}) -> | |
% Using Heron's formula | |
{ok, P} = perimeter({triangle, {A,B,C}}), | |
S = P / 2, | |
AREA = math:sqrt(S * (S - A) * (S - B) * (S - C)), | |
{ok, AREA}; | |
area(_) -> | |
{error, 'Unknown shape'}. | |
test_area() -> | |
Pi = math:pi(), | |
{ok, Pi} = area({circle, 1}), | |
{ok, 6} = area({rectangle, {3,2}}), | |
{ok, 4} = area({square, 2}), | |
{ok, 6.928203230275509} = area({triangle, {4,4,4}}), | |
{ok, 6.0} = area({triangle, {3,4,5}}), | |
{ok, 3.799671038392666} = area({triangle, {2,4,5}}), | |
{error, 'Unknown shape'} = area({trapezoid, {1,2,3,4}}), | |
'Tests passed'. | |
%% Enclose function | |
%% Assumption - smallest rectangle means a rectangle with the smallest area | |
enclose({circle, R}) -> | |
A = 2 * R, | |
{ok, {square, A}}; | |
enclose({rectangle, {A, B}}) -> | |
{ok, {rectangle, {A, B}}}; | |
enclose({square, A}) -> | |
{ok, {square, A}}; | |
enclose({triangle, {A,B,C}}) -> | |
% For triangle there are three different enclosing rectangles with the same area | |
% The only exception is obtuse triangle, where all enclosing rectangles can have different area | |
% The safe choice is the longest side, because the whole triangle will fit into enclosing rectangle | |
{ok, AREA} = area({triangle, {A,B,C}}), | |
MAX = max(A, max(B,C)), | |
{ok, {rectangle, {MAX, ((2 * AREA) / MAX)}}}; | |
enclose(_) -> | |
{error, 'Unknown shape'}. | |
test_enclose() -> | |
{ok, {square, 2}} = enclose({circle, 1}), | |
{ok, {rectangle, {3,2}}} = enclose({rectangle, {3,2}}), | |
{ok, {square, 2}} = enclose({square, 2}), | |
{ok, {rectangle, {4,3.4641016151377544}}} = enclose({triangle, {4,4,4}}), | |
{ok, {rectangle, {5,2.4}}} = enclose({triangle, {3,4,5}}), | |
{ok, {rectangle, {5, 1.5198684153570663}}} = enclose({triangle, {2,4,5}}), | |
{error, 'Unknown shape'} = enclose({trapezoid, {1,2,3,4}}), | |
'Tests passed'. | |
% Run all tests | |
run_tests() -> | |
test_perimeter(), | |
test_area(), | |
test_enclose(), | |
'All tests passed'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need Erlang installed on your machine.
Save this gist in some folder
Open erl in command line in that folder
Compile the file:
c(ex1_shapes).
Run tests:
ex1_shapes:run_tests().