Created
December 10, 2011 13:26
-
-
Save Deepwalker/1455158 to your computer and use it in GitHub Desktop.
rects intersection
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
% | |
% Rect library - work with rects | |
% | |
-module(rect). | |
-export([intersect/2, normalize/1]). | |
intersect(R1, R2) -> | |
{{X1, Y1}, {X2, Y2}} = R1, | |
{{OX1, OY1}, {OX2, OY2}} = R2, | |
intersect_proections({X1, X2}, {OX1, OX2}) orelse intersect_proections({Y1, Y2}, {OY1, OY2}). | |
intersect_proections({X1, X2}, {OX1, OX2}) -> | |
case lists:sort([{X1, recta}, {X2, recta}, {OX1, rectb}, {OX2, rectb}]) of | |
[{_, recta}, {_, recta}|_] -> false; | |
[{_, rectb}, {_, rectb}|_] -> false; | |
_ -> true | |
end. | |
normalize({{X1, Y1}, {X2, Y2}}) -> | |
{ | |
{min(X1, X2), min(Y1, Y2)}, | |
{max(X1, X2), max(Y1, Y2)} | |
}. | |
%% =================================================================== | |
%% testing | |
%% =================================================================== | |
-ifdef(TEST). | |
-include_lib("eunit/include/eunit.hrl"). | |
-define(R1, {{1, 1}, {3, 3}}). | |
-define(R2, {{2, 2}, {3, 3}}). | |
-define(R3, {{2, 5}, {3, 3}}). | |
-define(R3n, {{2, 3}, {3, 5}}). | |
rects_test() -> | |
?assertEqual(normalize(?R3), ?R3n), | |
?assertEqual(intersect(?R1, ?R2), true). | |
-endif. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment