【问题标题】:How to switch the picture in the update function of the package Graphics.Gloss.Interface.IO.Simulate?Graphics.Gloss.Interface.IO.Simulate包的更新功能中如何切换图片?
【发布时间】:2021-06-25 03:45:57
【问题描述】:

您好,我正在使用 Haskell gloss,创建二叉树的图片并模拟值的插入。

我能够创建一个draw 函数来创建树的图片,它工作正常。

问题在于创建模拟。在树上的每个值插入之后,我有一个包含所有树的列表,并希望update-函数在过去的某个时间在列表的i 位置选择树,并继续切换每个树生成的图片位置i 在列表中。

有没有办法做到这一点?

drawTree::(Show a)=>BTree a->Picture

updateTree :: ViewPort->Float->[BTree a]->[BTree a]
updateTree _ dt list=[list!!toInt dt]

main::IO()
main = do
  --receives all values to be inserted
  values <- getLine                    
  let list = read values :: [Int]      
  --temp is a list that stores all the tree generated in each insertion
  let temp =insertValues list Leaf                                 
  --stores the tree contained in the last inserction
  let tree = last temp
  --stores the tree after the first insertion                 
  let fir=first temp                   
  simulate window background fps [fir] drawTree updateTree

【问题讨论】:

    标签: haskell gloss


    【解决方案1】:

    simulate 的想法是每次调用updateTree 时都会将模拟移动一步。您的模拟状态不仅仅是树,它也是要放入树中的项目列表。 float 参数是进入模拟的时间,因此您可以根据需要为物体的运动设置动画。但是,对于第一次传递,您可能只希望这些值每帧出现在树中。

    所以你想要这样的东西:

    type SimState a = ([a], BTree a)   -- Or maybe "data" if you prefer.
    
    drawTree :: SimState a -> Picture
    
    updateTree :: ViewPort -> Float -> SimState a -> SimState a
    updateTree _ _ ([], t) = ([], t)  -- Static once everything is finished.
    updateTree _ _ (x:xs, t) = (xs, addNewBtreeNode t x)
    

    这会将项目x 从列表中移动到模拟的每一帧的树中。

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多