Advertisement
sek1pa

Concave_polygon

Jan 19th, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.39 KB | None | 0 0
  1. -module(solution).
  2. -export([main/0]).
  3.  
  4. rotate({A0,A1},{B0,B1},{C0,C1}) ->
  5.         (B0-A0)*(C1-B1)-(B1-A1)*(C0-B0).
  6.  
  7. sort_list(StartPoint,All) ->
  8.     lists:sort(fun(X,Y)-> Rot=rotate(StartPoint,X,Y), if Rot=<0 -> true; Rot>0 -> false end end,All).
  9.  
  10. find_start_point([{A,B}|[]]) -> {A,B};
  11. find_start_point([{A,B},{C,D}|Points]) ->
  12.     if (A < C orelse B < D) -> find_start_point([{A,B}|Points]);
  13.        (A >= C orelse B >= D) -> find_start_point([{C,D}|Points])
  14.        end.
  15.  
  16. main() ->
  17.     {ok,File}=file:open("test4",read),
  18.     {ok,Prom}=file:read_line(File),
  19.     [Count,_]=string:split(Prom,"\n"),
  20.     {ok,Bin}=file:read_file("test4"),
  21.     [First|Pairs]=binary:split(Bin,[<<"\n">>],[global]),
  22.     Data=parse_bin(Pairs,[]),
  23.     file:close(File),
  24.     main(lists:reverse(Data)).
  25.  
  26. main(Points) ->
  27.     StartPoint=find_start_point(Points),
  28.     io:format("~s~n",[concave(sort_list(StartPoint,Points))]).
  29.  
  30. parse_bin([],List) -> List;
  31. parse_bin([Pair|Tail],List) ->
  32.     if Pair == <<>> ->
  33.         parse_bin([],List);
  34.        Pair /= <<>> ->
  35.         [A,B]=binary:split(Pair,[<<" ">>],[global]),
  36.         parse_bin(Tail,[{binary_to_integer(A),binary_to_integer(B)}|List])
  37.     end.
  38.  
  39. concave([{_,_},{_,_}|Tail]) -> no.
  40. concave([{A,B},{C,D},{E,F}|Tail]) ->
  41.     Rot=rotate({A,B},{C,D},{E,F}),
  42.     if Rot>0  -> yes;
  43.        Rot<0  -> concave([{C,D},{E,F}|Tail]);      
  44.        Rot ==0 -> concave([{C,D},{E,F}|Tail])
  45.     end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement