Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Haskell中绘制线?_Haskell - Fatal编程技术网

如何在Haskell中绘制线?

如何在Haskell中绘制线?,haskell,Haskell,我必须编写一个函数drawLines,它创建定义线的视图。第一个参数是定义结果视图大小的元组(列、行)。左上角有一个坐标(0,0)。第二个参数是行列表 overlay :: [Colour] -> Colour shouldIPaintMany :: Point -> [Line] -> Colour shouldIPaintMany point = overlay . fmap (shouldIPaint point) 有打印功能: pt :: Result ->

我必须编写一个函数drawLines,它创建定义线的视图。第一个参数是定义结果视图大小的元组(列、行)。左上角有一个坐标(0,0)。第二个参数是行列表

overlay :: [Colour] -> Colour

shouldIPaintMany :: Point -> [Line] -> Colour
shouldIPaintMany point = overlay . fmap (shouldIPaint point)
有打印功能:

pt :: Result -> IO ()
pt x = putStr (concat (map (++"\n") x))
功能:

drawLines :: (Int,Int) -> [Line] -> Result
表示点和线的数据类型:

data Point = Point Int Int
data Line = Line Point Point
例如:

Prelude>pt(drawLines (31,15) [Line (Point x y) (Point 15 7)|(x,y)<-concat [[(x,y)|y<-[0,7,14]]|x<-[0,15,30]]])

 ##.............#.............##
 ..##...........#...........##..
 ....##.........#.........##....
 .....###.......#.......###.....
 ........##.....#.....##........
 ..........###..#..###..........
 .............#####.............
 ###############################
 .............#####.............
 ..........###..#..###..........
 ........##.....#.....##........
 .....###.......#.......###.....
 ....##.........#.........##....
 ..##...........#...........##..
 ##.............#.............##
Prelude>pt(drawLines(31,15)[Line(Point x y)(Point 15 7)|(x,y)这称为“光栅化”。有一个简单的技巧可以让库(如光栅化向量图像)对其进行光栅化,我将让您了解它。这样您就可以轻松地编写代码了

诀窍在于:

  • 定义一个函数,用于确定具有给定坐标的点应绘制为黑色还是白色
  • 将此函数映射到整个网格
  • 例如,假设您有一条线
    线(点12)(点34)
    。您必须能够回答以下问题:
    点23
    应该是黑色的吗?给定的线是否经过足够靠近给定点的位置才能被涂成黑色

    shouldIPaint :: Point -> Line -> Colour
    
    现在,如果有两条线,且其中一条足够近,则可以绘制任意数量的线,依此类推

    overlay :: [Colour] -> Colour
    
    shouldIPaintMany :: Point -> [Line] -> Colour
    shouldIPaintMany point = overlay . fmap (shouldIPaint point)
    
    要了解视图是点的集合,您需要确定每个点的此函数值。为此,您可以首先创建一个视图,其中每个点都将其坐标作为值,然后将坐标转换为颜色

    initialView :: Point -> View Point
    
    solution :: Point -> [Line] -> View Colour
    solution point lines = fmap (flip shouldIPaintMany lines) (initialView point)
    
    完成了!这里的美妙之处在于,可以直接将此解决方案扩展到三维空间中的绘制,或者绘制圆和线,等等


    我真的不希望你能自己填好缺失的部分。到现在为止,你可能有很多问题,但这些都是具体的尖锐的问题。其中一些是关于数学的,另一些是关于哈斯克尔的。如果你想成功,你应该写下这些尖锐的问题,然后试着回答,一个简单的回答d如果你不能,那么就在堆栈溢出上或在上询问他们。你可以在这个答案下面给我写一条评论,我会告诉你你是否走上了正确的道路。你也可能不理解我最初提供的解决方案,但你真的需要坚持下去,并问我是否有什么不合理的地方尽管你尽了最大的努力