如何在光泽度 haskell 中绘制棋盘?
How to draw a chess board in gloss haskell?
我刚开始 haskell gloss。我了解了一些关于它的功能。我正在尝试在 haskell 中画棋盘。主要问题是一切都在中心绘制。如果我使用 translate
函数,则板子会在随机位置绘制。这可能是因为平移从当前位置移动给定的距离而不是给定的确切点。
在 gloss haskell 中有什么方法可以让我们移动到特定点,例如 setTransform
或 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
右上角。
如果不行请告知获取当前变换点的方法
没有现有的 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
我刚开始 haskell gloss。我了解了一些关于它的功能。我正在尝试在 haskell 中画棋盘。主要问题是一切都在中心绘制。如果我使用 translate
函数,则板子会在随机位置绘制。这可能是因为平移从当前位置移动给定的距离而不是给定的确切点。
在 gloss haskell 中有什么方法可以让我们移动到特定点,例如 setTransform
或 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
右上角。
如果不行请告知获取当前变换点的方法
没有现有的 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