使用 Elixir

问题描述

我有一个整数列表,表示从左上角向下呈现的 2D 图形/网格(矩阵?),其中宽度 = w,高度 = h。

列表如下:

list = [4,5,2,4,3,6,7,8,...]

列表中的项目 > 0 表示墙壁,而 == 0 的项目表示开放空间。

我正在尝试编写一个函数,该函数遍历此列表并返回表示所有墙壁的最少矩形(左上角 x、y 和宽度/高度),目的是处理与该列表中的对象的碰撞墙壁。

对于 w = 4 h = 4 的列表:

square_list = [
        8,7
       ]

return_rectangles(square_list,w,h) 会返回:

[
   # Note: the y value increases positively as we move downward
   %{top_left_x: 0,top_left_y: 0,width: 4,height: 2},# first two rows (the 8s)
   %{top_left_x: 0,top_left_y: 3,height: 1}  # second row (the 7s)
]

我还必须处理垂直墙/不规则矩形行的情况,例如:

tall_list = [
        8,7
       ]

想知道我是否仍然应该追求这个列表的矩形抽象,或者我是否应该选择多边形

我首先将列表转换为包含索引的元组流并删除 0 值项目,认为项目的索引指示其在网格中的位置并且 0 值无关紧要(也许更容易计算开放空间(空间 = 0)而不是封闭空间,实际上......):

  def convert_list_to_indexed_tuples(list) do
    # Creates a stream with index from a given list,# outputing {item,index}
    with_indices = Stream.with_index(list)

    # remove zero values
    # returns a stream that as a list looks like [{item,index},...],e.g. [{8,112},...]
    Stream.take_while(with_indices,fn {item,_index} ->
      tile != 0
    end)
  end

然后我决定将这个发布在这里以众包一个解决方案,因为我已经很长时间没有完成图形数学了。也许我应该把这个列表当作一个矩阵?

任何建议将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)