如何在光泽度haskell中绘制棋盘?
我刚开始学习haskell gloss。我对它的功能有了一些了解。我想在哈斯克尔画一个棋盘。主要的问题是所有的东西都被画在中心。如果我使用了函数如何在光泽度haskell中绘制棋盘?,haskell,graphics,gloss,Haskell,Graphics,Gloss,我刚开始学习haskell gloss。我对它的功能有了一些了解。我想在哈斯克尔画一个棋盘。主要的问题是所有的东西都被画在中心。如果我使用了函数translate,电路板将在随机位置绘制。这可能是因为“平移”是从当前位置移动给定的距离,而不是移动到给定的精确点 gloss haskell中有没有一种方法可以让我们移动到一个特定的点,比如setTransform或translateTo。或者有没有函数告诉我们当前点的坐标 module Main where import Graphics.Glo
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)
modulemain其中
导入图形。光泽
导入库(someFunc)
blockSize::Float
块大小=50
董事会:[[Int]]
线路板=复制8(复制8 0)
drawTile::浮动->浮动->颜色->图片
drawTile a b col=转换a b$颜色col$矩形实心块大小blockSize
--绘图行::Int->picture
--拖船=
toInt::浮点->整数
toInt=圆形
getColor::Float->Float->Color
getColor i j=如果偶数$toInt((i*8)+j),则为红色,否则为蓝色
屏幕高度=700
屏幕宽度=1000
图画
drawing=pictures[drawTile(row*blockSize)(e*blockSize)(getColor row e)| row现有的光泽度函数无法拍摄任意图片并将其移动,使其左上角位于屏幕的左上角。光泽度中的所有现有转换函数都是相对的,因此无法进行“绝对”转换移动到一个特定的点
你能做的最好的事情就是安排好画图,使它的原点与左上角相匹配,然后将它向上和向左平移屏幕高度和宽度的一半
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
import Graphics.Gloss
--左上角为(0,0)的棋盘,宽度和高度为一个单位
chess=scale(1/8)(1/8)$pictures[正方形x y | x请分享您的尝试。@WillemVanOnsem我发布了我的代码。请检查。我无法复制这一点:如果我将getColor
替换为偶数$toInt(I+j)
,我得到@WillemVanOnsem,我想你可能不理解我的问题。棋盘的顺序不是问题。问题是它画在中间。我希望它从左上角开始。然后你也应该看看窗口大小,所以翻译(从整数屏幕宽度/2-8.5*块大小)(从整数屏幕高度/2-8.5*块大小)$pictures…
(这是针对9x9板的,因为您使用了[0..8]
,或者您可以使用翻译(fromIntegral screenWidth/2-7.5*blockSize)(fromIntegral screenHeight/2-7.5*blockSize)$pictures
针对8x8板。谢谢您的回答。两个附带问题,1)有没有比gloss更好的2d图形库(更好,我指的是更多功能)2)haskell适合制作2d应用程序吗?我认为图表
(请参阅)可能是我见过的最复杂的2d绘图软件包,但我很少使用它。