简体   繁体   English

在Erlang中重叠2D形状

[英]Overlapping 2D shapes in erlang

Hello following my question before i have done my research and came up with this, 您好,在完成研究并提出这个问题之前,请先回答我的问题,

Basically i am trying to find whether a circle,circle...rectangle,rectangle or circle,rectangle overlap 基本上我正在尝试查找圆形,圆形...矩形,矩形或圆形,矩形重叠

Mathematically i have my 3 cases as pseudocoded algorithms,what i need help with is to implement this in erlang but i will only ask for circle,circle since i can just figure out the rest syntaxically. 在数学上,我有3种情况作为伪编码算法,我需要帮助的是在erlang中实现这一点,但是我只要求圈出,因为我可以从语法上找出其余的问题。

perim({circle, {X,Y}, R}) -> 
math:pi()*(R * 2);
perim({rectangle, {X,Y}, H, W})-> 
  (H + W) * 2.

overlap({_,{_,_},_,_},{_,{_,_},_,_}) ->
overlapping({_,{_,_},_,_},{_,{_,_},_,_}).


%overlap cases

%Circle,Circle
overlapping({circle, {X,Y}, R},{circle2, {X2,Y2}, R2}) ->
   circle1 = {{X,Y},R},
   circle2 = {{X2,Y2},R2},
   case (math:sqrt(abs(X)-abs(X2)) + math:sqrt(abs(Y)-abs(Y2))) < math:sqrt(abs(R)+abs(R2)) of
           true -> true;
           false -> false
    end.    

Thanks in advance 提前致谢

Two things: 两件事情:

  • Since you're representing circles as tuples of the form {circle, {X,Y}, R} , both arguments to the overlapping function should say circle . 由于您将圆形表示为{circle, {X,Y}, R}形式的元组,因此overlapping函数的两个参数都应为circle If you put circle2 for the second argument, it won't match your circle tuples, and you'll get a function_clause error. 如果将circle2用作第二个参数,则它将与您的圆元组不匹配,并且会出现function_clause错误。

  • Since you're already matching out the variables X , Y , R , X2 , Y2 and R2 in the function head, there is no need for the two lines below that. 由于您已经在函数头中匹配了变量XYRX2Y2R2 ,因此不需要下面的两行。 (In fact, those will always fail, since any word starting with a lower-case letter is an atom, and you're trying to match an atom against a tuple: that will give you a badmatch error.) (实际上,这些将始终失败,因为任何以小写字母开头的单词都是一个原子,并且您试图将一个原子与一个元组进行匹配:这将badmatch您出现不badmatch错误。)

So that would be: 因此将是:

overlapping({circle, {X,Y}, R},{circle, {X2,Y2}, R2}) ->
   case (math:sqrt(abs(X)-abs(X2)) + math:sqrt(abs(Y)-abs(Y2))) < math:sqrt(abs(R)+abs(R2)) of
           true -> true;
           false -> false
    end.    

Also, since all that case is doing is checking for true or false and returning the same value, you can simplify your code by removing it: 另外,由于所有case都是检查truefalse并返回相同的值,因此可以通过删除代码来简化代码:

overlapping({circle, {X,Y}, R},{circle, {X2,Y2}, R2}) ->
   (math:sqrt(abs(X)-abs(X2)) + math:sqrt(abs(Y)-abs(Y2))) < math:sqrt(abs(R)+abs(R2)).    

To learn more about pattern matching, you might find this chapter from "Learn You Some Erlang" useful. 要了解有关模式匹配的更多信息,您可能会发现“学习一些Erlang”中的这一章很有用。

Can't properly format your code snippet in the comment. 无法正确设置注释中的代码段格式。 Try this (with ';' after each case statement): 尝试以下操作(在每个case语句后加上“;”):

overlap({{circle,{X,Y},R},{rectangle,{X2,Y2},H,W}}) ->
   X_Axis = X - X2,
   Y_Axis = Y - Y2,
      case abs(X_Axis) >= (0.5*abs(W) + R) orelse abs(Y_Axis) >= (0.5*abs(H) + R) of
        true -> ((X == X2) andalso (Y-Y2));
        true -> ((abs(X_Axis) < 0.5*W) andalso (abs(Y_Axis) < (0.5*H + R)));
        true -> ((abs(X_Axis) < 0.5*W + R) andalso (abs(Y_Axis) < 0.5*H));
        false -> false  
      end.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM