首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示haskell中的mandelbrot集合时出现逻辑错误

显示haskell中的mandelbrot集合时出现逻辑错误
EN

Stack Overflow用户
提问于 2016-04-03 01:54:00
回答 1查看 62关注 0票数 0

我创建了一个程序,它的目的是绘制一个分形集( manderbrot集),它不包含任何语法错误,但只出现一个像素,而不是集。

下面是我的程序:

代码语言:javascript
复制
module Main where

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Builder()
import Graphics.Rendering.Cairo as C
import Control.Monad (when)


main :: IO()
main = do
    _ <- initGUI
    builder <- builderNew
    builderAddFromFile builder "09-mandelbrot.ui"

    window   <- builderGetObject builder castToWindow "Figure de Mandelbrot"
    canvas <- builderGetObject builder castToDrawingArea "drawingarea1"
    _ <- onExpose canvas $ const (updateCanvas canvas) 

    widgetShowAll window
    mainGUI


updateCanvas :: DrawingArea -> IO Bool
updateCanvas canvas = do
  win <- widgetGetDrawWindow canvas
  (width, height) <- widgetGetSize canvas
  renderWithDrawable win $ example (fromIntegral width) (fromIntegral height)
  return True

k=100

mandelbrot :: Double -> Double -> Bool
mandelbrot a b = 
  let 
    mandelrec :: Double -> Double -> Int -> Bool 
    mandelrec x y i
      | (x * x + y * y > 4) = False
      | (i==k) && (x * x + y * y <= 4) = True
      | otherwise = mandelrec x' y' (i+1)
            where x' = x * x - y * y + a
                  y' = 2 * x * y + b
  in mandelrec 0 0 0

affiche :: (Double, Double) -> Render()
affiche (a, b) = when (mandelbrot a b) $ C.rectangle a b 1 1

colonnes w = [ t/w*4-2 | t<-[0..(w-1)] ]
lignes h = [ t/h*4-2 | t<-[0..(h-1)] ]


example :: Double -> Double -> C.Render ()
example width height = do
    setSourceRGB 0 0 0
    setLineWidth 1

    mapM_ affiche (zip (colonnes width) (lignes height))
    stroke

如果需要的话,我可以给你解释。我已经测试了lignes和colonnes函数,看起来没问题。

EN

回答 1

Stack Overflow用户

发布于 2016-04-03 04:28:43

好了,它现在起作用了。

我没有收集范围-2..2中的所有点,但只收集了那些放置在对角线上的点。

下面是一个有效的代码:

代码语言:javascript
复制
module Main where

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Builder()
import Graphics.Rendering.Cairo as C
import Control.Monad (when)


main :: IO()
main = do
    _ <- initGUI
    builder <- builderNew
    builderAddFromFile builder "09-mandelbrot.ui"

    window   <- builderGetObject builder castToWindow "Figure de Mandelbrot"
    canvas <- builderGetObject builder castToDrawingArea "drawingarea1"
    _ <- onExpose canvas $ const (updateCanvas canvas) 

    widgetShowAll window
    mainGUI


updateCanvas :: DrawingArea -> IO Bool
updateCanvas canvas = do
  win <- widgetGetDrawWindow canvas
  (width, height) <- widgetGetSize canvas
  renderWithDrawable win $ example (fromIntegral width) (fromIntegral height)
  return True

k :: Int
k=100

mandelbrot :: Double -> Double -> Bool
mandelbrot a b = 
  let 
    mandelrec :: Double -> Double -> Int -> Bool 
    mandelrec x y i
      | (x * x + y * y > 4) = False
      | (i==k) && (x * x + y * y <= 4) = True
      | otherwise = mandelrec x' y' (i+1)
            where x' = x * x - y * y + a
                  y' = 2 * x * y + b
  in mandelrec 0 0 0

affiche2 :: Double -> Double -> Render()
affiche2 a b = do
  C.rectangle a b 1 1
  stroke

affiche :: ((Double,Double), (Double,Double)) -> Render()
affiche ((a0,a), (b0,b)) = when (mandelbrot a b) $ affiche2 a0 b0

colonnes w = [ (t,t/w*4-2) | t<-[0..(w-1)] ]
lignes h = [ (t,t/h*4-2) | t<-[0..(h-1)] ]
points w h = [ (colonne,ligne)| colonne <- colonnes w,ligne <- lignes h]

example :: Double -> Double -> C.Render ()
example width height = do
    setSourceRGB 0 0 0
    setLineWidth 1

    mapM_ affiche (points width height)
    stroke
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36376842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档