有没有办法将两组图片合并为一个?

时间:2019-05-20 10:25:45

标签: haskell gloss

我正在尝试显示由一组单元格填充的网格线,这些单元格填充了这些行中的正方形。

我已经尝试将它们像简单列表那样串联。

这是线的网格。单独显示正确。

grid = translate (fromIntegral width * (-0.5)) 
                 (fromIntegral height * (-0.5)) 
                 (pictures (concat [
                                    [line [(i * unitWidth, 0.0)
                                          ,(i * unitWidth, fromIntegral height)]
                                    ,line [(0.0, i * unitHeight)
                                          ,(fromIntegral width, i * unitHeight)]
                                    ]
                                   | i <- [1..gridDimension]]
                            )
                 )

这是在线条之间绘制的单位集,也可以正确显示。

units = pictures [translate ((x*unitWidth - unitWidth/2) + (fromIntegral width*(-0.5))) 
                            ((y*unitHeight - unitHeight/2) + (fromIntegral height*(-0.5)))
                            unit
                 | x <- [1..gridDimension], y <- [1..gridDimension]]

我的主要方法:

main = display window backgroundColor units

我可以在这个地方用网格交换单位,并且工作正常。 我也尝试过:

main = display window backgroundColor (units++grid)

它引发了以下错误:

40: error:
    • Couldn't match expected type ‘[a0]’ with actual type ‘Picture’
    • In the first argument of ‘(++)’, namely ‘grid’
      In the third argument of ‘display’, namely ‘(grid ++ units)’
      In the expression: display window backgroundColor (grid ++ units)
   |
10 | main = display window backgroundColor (grid++units)
   |                                        ^^^^

/home/georg/Desktop/THM/6_semester/funktionale_programmierung/my/app/Main.hs:10:40: error:
    • Couldn't match expected type ‘Picture’ with actual type ‘[a0]’
    • In the third argument of ‘display’, namely ‘(grid ++ units)’
      In the expression: display window backgroundColor (grid ++ units)
      In an equation for ‘main’:
          main = display window backgroundColor (grid ++ units)
   |
10 | main = display window backgroundColor (grid++units)
   |                                        ^^^^^^^^^^^

/home/georg/Desktop/THM/6_semester/funktionale_programmierung/my/app/Main.hs:10:46: error:
    • Couldn't match expected type ‘[a0]’ with actual type ‘Picture’
    • In the second argument of ‘(++)’, namely ‘units’
      In the third argument of ‘display’, namely ‘(grid ++ units)’
      In the expression: display window backgroundColor (grid ++ units)
   |
10 | main = display window backgroundColor (grid++units)
   |                                              ^^^^^

1 个答案:

答案 0 :(得分:2)

(++) :: [a] -> [a] -> [a]函数附加两个列表,Picture不是列表,因此您不能使用该函数。

不过,您可以在此处使用(<>) :: Semigroup m => m -> m -> m,因为PictureSemigroup的实例:

因此,我们可以这样写:

main = display window backgroundColor (units <> grid)

或者您可以再次使用pictures :: [Picture] -> Picture,并包含unitsgrid,例如:

main = display window backgroundColor (pictures [units, grid])