Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(solution).
- -export([main/0]).
- rotate({A0,A1},{B0,B1},{C0,C1}) ->
- if(C0<A0 andalso C0<B0) -> ((B0-A0)*(C1-B1)-(B1-A1)*(C0-B0));
- true -> (B0-A0)*(C1-B1)-(B1-A1)*(C0-B0)
- end.
- find_start_point([{A,B}|[]]) -> {A,B};
- find_start_point([{A,B},{C,D}|Points]) ->
- if (A < C orelse B < D) -> find_start_point([{A,B}|Points]);
- (A >= C orelse B >= D) -> find_start_point([{C,D}|Points])
- end.
- sort_list(StartPoint,All) ->
- lists:sort(fun(X,Y)-> Rot=rotate(StartPoint,X,Y), if Rot<0 -> false; Rot>0 -> true; Rot==0 -> false end end,All).
- main() ->
- {ok,[Count]}=io:fread("","~d"),
- {PPoints,StartPoint}=read_data(Count,[]),
- Prom=lists:delete(StartPoint,PPoints),
- Points=sort_list(StartPoint,Prom),
- SortedData=lists:delete(lists:nth(1,Points),Points),
- %%io:format("SortedList: ~p~nsecond: ~p~n",[SortedData,lists:nth(1,SortedData)]),
- marvis(SortedData,[lists:nth(1,Points),StartPoint]).
- read_data(Count,Acc) when Count /=0 ->
- {ok,[A,B]}=io:fread("","~d~d"),
- read_data(Count-1,[{A,B}|Acc]);
- read_data(Count,Acc) when Count==0 ->
- {lists:reverse(Acc),find_start_point(Acc)}.
- marvis([],[{Ax,Ay},{Bx,By}|Tail]) ->
- io:format("~p~p~p~n",[{Ax,Ay},{Bx,By},Tail]),
- Perim=perim({Ax,Ay},lists:last(Tail)),
- perimetr(lists:reverse([{Ax,Ay},{Bx,By}|Tail]),Perim);
- marvis([{Fx,Fy}|List],[{Ax,Ay},{Bx,By}|Tail]) ->
- Rot=rotate({Bx,By},{Ax,Ay},{Fx,Fy}),
- if Rot =< 0 -> %%io:format("prom >0 : ~p~p~p~n",[{Fx,Fy},{Bx,By},Tail]),
- marvis(List, [{Fx,Fy},{Bx,By}|Tail]);
- Rot > 0 -> %%io:format("prom =<0 : ~p~p~p~p~n",[{Fx,Fy},{Ax,Ay},{Bx,By},Tail]),
- marvis(List, [{Fx,Fy},{Ax,Ay},{Bx,By}|Tail])
- end.
- perimetr([{_,_}],Perim) ->
- io:format("~p~n",[Perim]);
- perimetr([{Ax,Ay},{Bx,By}|Tail],Perim) ->
- N = perim({Ax,Ay},{Bx,By}),
- perimetr([{Bx,By}|Tail],Perim+N).
- perim({Ax,Ay},{Bx,By}) ->
- math:sqrt(math:pow(Bx-Ax,2)+math:pow(By-Ay,2)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement