1

我刚开始使用haskell光泽。我对它的功能有所了解。我正在尝试在 haskell 中绘制棋盘。主要问题是一切都被绘制在中心。如果我使用该功能translate,则板正在随机位置绘制。这可能是因为 translate 从当前位置给定的距离移动到给定的确切点。

光泽 haskell 有没有办法让我们可以移动到一个特定的点,比如setTransformor translateTo。或者是否有任何函数可以告诉我们当前所在点的坐标。

module Main where

import Graphics.Gloss
import Lib (someFunc)

blockSize :: Float
blockSize = 50

board :: [[Int]]
board = replicate 8 (replicate 8 0)

drawTile :: Float -> Float -> Color -> Picture
drawTile a b col = translate a b $ color col $ rectangleSolid blockSize blockSize

-- drawRow :: Int -> Pictur
-- drawRow =

toInt :: Float -> Integer
toInt = round

getColor :: Float -> Float -> Color
getColor i j = if even $ toInt ((i * 8) + j) then red else blue

screenHeight = 700

screenWidth = 1000

drawing :: Picture
drawing = pictures [drawTile (row * blockSize) (e * blockSize) (getColor row e) | row <- [0 .. 8], e <- [0 .. 8]]

-- moveToStart = viewPortTranslate

main :: IO ()
main = display (InWindow (show board) (screenWidth, screenHeight) (10, 10)) white (translate 0 0 drawing)

编辑: 我不想通过使用一些数学技巧来解决这个特定问题。我想知道的是我怎样才能翻译到一个特定的位置。就像我做someFunc 0 0的时候应该去0 0右上角。

如果不可能,请告诉获取当前变换点的方法。

4

1 回答 1

1

没有现有的 Gloss 函数可以拍摄任意图片并将其移动,因此其左上角位于屏幕的左上角。Gloss 中所有现有的转换函数都是相对的,因此无法将“绝对”移动到特定点。

您可能做的最好的事情是安排绘制您的图片,使其原点匹配其左上角,然后将其向上和向左平移屏幕高度和宽度的一半。

import Graphics.Gloss

-- chess board with top-left corner at (0,0), one unit in width and height
chess = scale (1/8) (1/8) $ pictures [square x y | x <- [0..7], y <- [0..8]]
  where square x y =
          Color (if even (x+y) then red else black) $
          translate (fromIntegral x+0.5) (-fromIntegral y -0.5) $ rectangleSolid 1 1

main = display (InWindow "Layout" (1000,700) (10,10)) white $
  -- scale to a 700x700 square (with origin still at top-level corner)
  -- then translate origin from default position at center of window
  -- to top-left corner, by moving half the window width and height
  translate (-500) 350 $ scale 700 700 chess
于 2020-12-29T21:04:24.643 回答