因此,我只是写了一个小片段来生成Mandelbrot分形,并想象我的惊喜,当它出来时都是丑陋和倾斜的(正如你在底部看到的那样).我很欣赏为什么会发生这种情况的方向.这是一次学习经历,我不是在寻找任何人为我做这件事,但我有点在调试它.违规代码是:
module Mandelbrot where
import Complex
import Image
main = writeFile "mb.ppm" $ imageMB 1000
mandelbrotPixel x y = mb (x:+y) (0:+0) 0
mb c x iter | magnitude x > 2 = iter
| iter >= 255 = 255
| otherwise = mb c (c+q^2) (iter+1)
where q = x -- Mandelbrot
-- q = (abs.realPart $ x) :+ (abs.imagPart $ x) --Burning Ship
argandPlane x0 x1 y0 y1 width height = [ (x,y) |
y <- [y1, y1 - dy .. y0], --traverse from
x <- [x0, x0 + dx .. x1] ] --top-left to bottom-right
where dx = (x1 - x0) / width
dy = (y1 - y0) / height
drawPicture :: (a -> b -> c) -> (c -> Colour) -> [(a, b)] -> Image
drawPicture function colourFunction = map (colourFunction . uncurry function)
imageMB s = createPPM s s
$ drawPicture mandelbrotPixel (replicate 3)
$ argandPlane (-1.8) (-1.7) (0.02) 0.055 s' s'
where s' = fromIntegral s
Run Code Online (Sandbox Code Playgroud)
图像代码(我相当自信)是:
module Image where
type Colour = [Int]
type Image = [Colour]
createPPM :: Int -> Int -> Image -> String
createPPM w h i = concat ["P3 ", show w, " ", show h, " 255\n",
unlines.map (unwords.map show) $ i]
Run Code Online (Sandbox Code Playgroud)
C. *_*ann 15
好吧,图像是倾斜的,因为尺寸是错误的,但这是显而易见的.您指定图像大小然后吐出像素列表,但每行的某些像素数不正确.
更具体地说,请注意图像几乎只包裹一次:换句话说,skew per line * height of the image = width of the image
.由于图像是方形的,这意味着您每行生成一个额外的像素 - 一个很好的旧的一个一个错误.
这种情况发生的显而易见的地方是当您生成坐标以进行迭代时.让我们尝试一下,看看它给了我们什么:
> length $ argandPlane (-2.5) (-2) 1.5 2 10 10
121
> 10 ^ 2
100
> 11 ^ 2
121
Run Code Online (Sandbox Code Playgroud)
所以.我怀疑错误是因为你正在计算增量作为实际距离除以像素大小,这会生成正确的间隔数,但是会产生额外的点.考虑从0.0到1.0的间隔.使用宽度为4的计算,我们得到:
> let x0 = 0.0
> let x1 = 1.0
> let width = 4.0
> let dx = (x1 - x0) / width
> dx
0.25
> let xs = [x0, x0 + dx .. x1]
> xs
[0.0, 0.25, 0.5, 0.75, 1.0]
> length xs
5
Run Code Online (Sandbox Code Playgroud)
因此,要获得正确的点数,只需在生成坐标时将大小减小1即可.